Tuesday 15 December 2015

Download string from Url in C# return 404, where as Url working on browser

When working with, routing or redirection WebClient return 404 while downloading string in C#.

Causes: If we don't specify User-Agent in httprequest it will return 404 for web page.


Solution:
       
WebClient client = new WebClient();
client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0");
downloadString = client.DownloadString(url);