为什么WebHtt prequest.ResponseUri不返回正确的网址,在C#正确、网址、WebHtt、prequest

2023-09-03 08:20:00 作者:、年少不知秋衫薄

下面是我的code从浏览器中,通过获取网址HttpWebRequest的和HttpWebResponse。

Here is my code for getting url from browser through "HttpWebRequest" and "HttpWebResponse".

{
    string link="http://g.microsoftonline.com/0BXPS00id-id/1250";

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(link);

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    string responseurl = response.ResponseUri.ToString();

    Console.WriteLine(responseurl);
}

当我执行上述code中的URL返回的是

when i executed the above code the url return is

https://mocp.microsoftonline.com/Site/Error.aspx?Err=NotSupported

但是当我手动浏览器中打开的链接重定向到

but when i manually open the link in browser it redirects to

https://mocp.microsoftonline.com/Site/Support.aspx

为什么response.ResponseUri.ToString()返回错误的URL?

why the response.ResponseUri.ToString() returning wrong url?

您可以请提供解决方案呢?

Can you please provide solution for this?

推荐答案

通过request.UserAgent属性,你可以充当所支持的浏览器。 对我来说,下面的工作

Through the request.UserAgent property you can act as a browser that's supported. The following worked for me

string link = "http://g.microsoftonline.com/0BXPS00id-id/1250";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(link);
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

string responseurl = response.ResponseUri.ToString();
Console.WriteLine(responseurl)

我不是一个专家在用户代理值。我优雅地从snurched为用户代理上述值 http://stackoverflow.com/a/702110/455904