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.