1

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.

enter image description here

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.

enter image description here

enter image description here

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.

Ragesh P Raju
  • 3,879
  • 14
  • 101
  • 136
  • 2
    possible duplicate of [HttpWebRequest to URL with dot at the end](http://stackoverflow.com/questions/856885/httpwebrequest-to-url-with-dot-at-the-end) – EricLaw Nov 07 '12 at 23:27

2 Answers2

0

is the web address still correct after this line:

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

I'm guessing it works for other sites?

Florian
  • 1,827
  • 4
  • 30
  • 62
  • Thank you for your valueable reply. Please see this image [image] https://blufiles.storage.live.com/y1pZ77S7y4AIiEoW6NKKrBx1oKRVXoKoZ7u04P2IWZrauutal7L2Lj4Mg/Url%20Change%20%20After%20url%20conversion.png.jpg?psid=1&ck=0&ex=720 – Ragesh P Raju Nov 07 '12 at 06:12
0

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.

EricLaw
  • 56,563
  • 7
  • 151
  • 196