简体   繁体   中英

WebClient returns 404 error, but this url works in the WebBrowser.Navigate method

I try to download a html file using WebClient .

This is my code:

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;
        }
    }

The URL is http://www.paginegialle.it/roma-rm/abbigliamento/alberto-aspesi-c .

在此处输入图像描述

But I can navigate to this URL in IE9 and Firefox and Chrome browsers without problem. I use Fiddler to solve this problem.

I find the URL has changed after the WebClient.Request - see the image below:

Actual url: http://www.paginegialle.it/roma-rm/abbigliamento/alberto-aspesi-c.

在此处输入图像描述

在此处输入图像描述

Please see the difference. I remove the dot in at the end of the url. But it's not working in the browsers (IE9, Firefox, Chrome). How to change the actual url to this url?

Please help me.

Thank you very much Eric Law. I use this link for solve my issue :

HttpWebRequest to URL with dot at the end

Thank you very much for solve my headache.

is the web address still correct after this line:

     Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out innUri);

I'm guessing it works for other sites?

I think you've found a cool bug in the .NET URI object.

MessageBox.Show(new Uri("http://example.com/bug/here."));

shows:

http://example.com/bug/here

Note that the trailing period is missing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM