为什么WebHtt prequest.ResponseUri不返回正确的网址,在C# [英] why WebHttpRequest.ResponseUri not returning correct url in C#

查看:109
本文介绍了为什么WebHtt prequest.ResponseUri不返回正确的网址,在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的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

这篇关于为什么WebHtt prequest.ResponseUri不返回正确的网址,在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆