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.

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

C# – How to make a HTTP call

You’ve got some extra stuff in there if you’re really just trying to call a website. All you should need is: WebRequest webRequest = WebRequest.Create(“http://ussbazesspre004:9002/DREADD?” + fileName); WebResponse webResp = webRequest.GetResponse(); If you don’t want to wait for a response, you may look at BeginGetResponse to make it asynchronous .

How to fill forms and submit with Webclient in C#

You should probably be using HttpWebRequest for this. Here’s a simple example: var strId = UserId_TextBox.Text; var strName = Name_TextBox.Text; var encoding=new ASCIIEncoding(); var postData=”userid=”+strId; postData += (“&username=”+strName); byte[] data = encoding.GetBytes(postData); var myRequest = (HttpWebRequest)WebRequest.Create(“http://localhost/MyIdentity/Default.aspx”); myRequest.Method = “POST”; myRequest.ContentType=”application/x-www-form-urlencoded”; myRequest.ContentLength = data.Length; var newStream=myRequest.GetRequestStream(); newStream.Write(data,0,data.Length); newStream.Close(); var response = myRequest.GetResponse(); var responseStream = response.GetResponseStream(); … Read more