WebClient 403 Forbidden

Just add a simple line before you make your download: string url = … string fileName = … WebClient wb = new WebClient(); wb.Headers.Add(“User-Agent: Other”); //that is the simple line! wb.DownloadFile(url, fileName); That’s it.

WebClient – The remote server returned an error: (403) Forbidden

I’ve just tried it with Fiddler running to see the response and it returns the following notice with the status code. Scripts should use an informative User-Agent string with contact information, or they may be IP-blocked without notice. This works. WebClient webClient = new WebClient(); webClient.Headers.Add(“user-agent”, “Only a test!”); string content = webClient.DownloadString(“http://he.wikisource.org/wiki/%D7%A9%D7%95%D7%9C%D7%97%D7%9F_%D7%A2%D7%A8%D7%95%D7%9A_%D7%90%D7%95%D7%A8%D7%97_%D7%97%D7%99%D7%99%D7%9D_%D7%90_%D7%90”);

Get Content-Disposition parameters

If you are working with .NET 4.5 or later, consider using the System.Net.Mime.ContentDisposition class: string cpString = wc.ResponseHeaders[“Content-Disposition”]; ContentDisposition contentDisposition = new ContentDisposition(cpString); string filename = contentDisposition.FileName; StringDictionary parameters = contentDisposition.Parameters; // You have got parameters now Edit: otherwise, you need to parse Content-Disposition header according to it’s specification. Here is a simple class that … Read more

HEAD with WebClient?

You are right WebClient does not support this. You can use HttpWebRequest and set the method to HEAD if you want this functionality: System.Net.WebRequest request = System.Net.WebRequest.Create(uri); request.Method = “HEAD”; request.GetResponse();

Maximum concurrent requests for WebClient, HttpWebRequest, and HttpClient

Yes, there is a limit. The default connection limit is 2 concurrent connections per remote host. This can be overridden. For example, I believe that ASP.NET by default overrides the default to be 10 connections per remote host. From https://msdn.microsoft.com/en-us/library/7af54za5.aspx: The number of connections between a client and server can have a dramatic impact on … Read more

Accept Cookies in WebClient?

Usage : CookieContainer cookieJar = new CookieContainer(); cookieJar.Add(new Cookie(“my_cookie”, “cookie_value”, “/”, “mysite”)); CookieAwareWebClient client = new CookieAwareWebClient(cookieJar); string response = client.DownloadString(“http://example.com/response_with_cookie_only.php”); public class CookieAwareWebClient : WebClient { public CookieContainer CookieContainer { get; set; } public Uri Uri { get; set; } public CookieAwareWebClient() : this(new CookieContainer()) { } public CookieAwareWebClient(CookieContainer cookies) { this.CookieContainer = cookies; … Read more

How to get status code from webclient?

You can check if the error is of type WebException and then inspect the response code; if (e.Error.GetType().Name == “WebException”) { WebException we = (WebException)e.Error; HttpWebResponse response = (System.Net.HttpWebResponse)we.Response; if (response.StatusCode==HttpStatusCode.NotFound) System.Diagnostics.Debug.WriteLine(“Not found!”); } or try { // send request } catch (WebException e) { // check e.Status as above etc.. }