Winforms WebClient返回404错误,但此url在WebBrowser.Navigate方法中有效
我尝试使用Winforms WebClient返回404错误,但此url在WebBrowser.Navigate方法中有效,winforms,c#-4.0,uri,webclient,Winforms,C# 4.0,Uri,Webclient,我尝试使用WebClient下载html文件 这是我的代码: public string GetWebData(string url) { string html = string.Empty; using (WebClient client = new WebClient()) { Uri innUri = null; Uri.TryCreate(url, UriKind.RelativeOrAb
WebClient
下载html文件
这是我的代码:
public string GetWebData(string url)
{
string html = string.Empty;
using (WebClient client = new WebClient())
{
Uri innUri = null;
Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out innUri);
try
{
client.Headers.Add("Accept-Language", " en-US");
client.Headers.Add("Accept-Encoding", "gzip, deflate");
client.Headers.Add("Accept", " text/html, application/xhtml+xml, */*");
client.Headers.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");
using (StreamReader str = new StreamReader(client.OpenRead(innUri)))
{
html = str.ReadToEnd();
}
}
catch (WebException we)
{
throw we;
}
return html;
}
}
URL是http://www.paginegialle.it/roma-rm/abbigliamento/alberto-aspesi-c
但在IE9、Firefox和Chrome浏览器中,我可以毫无问题地导航到这个URL。我用小提琴来解决这个问题
我发现在WebClient.Request
之后,URL发生了更改-请参见下图:
实际url:http://www.paginegialle.it/roma-rm/abbigliamento/alberto-aspesi-c.
请看区别。我删除了url末尾的点。但它在浏览器(IE9、Firefox、Chrome)中不起作用。如何将实际url更改为此url
请帮助我。这一行之后的网址是否仍然正确:
Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out innUri);
我猜它适用于其他网站?我认为您在.NET URI对象中发现了一个很酷的bug
MessageBox.Show(new Uri("http://example.com/bug/here."));
显示:
请注意,缺少尾随时间。感谢您宝贵的回复。请查看此图像[图像]的可能副本