Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Winforms WebClient返回404错误,但此url在WebBrowser.Navigate方法中有效_Winforms_C# 4.0_Uri_Webclient - Fatal编程技术网

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."));
显示:


请注意,缺少尾随时间。

感谢您宝贵的回复。请查看此图像[图像]的可能副本