WebClient set headers

If the header already exists: client.Headers.Set(“Content-Type”, “image/jpeg”); if its a new header: client.Headers.Add(“Content-Type”, “image/jpeg”); Also, there is a chance that you are getting an error because you are trying to set the headers too late. Post your exception so we can let you know. Update Looks like there are some weird restrictions on the “Content-Type” … Read more

Is there a way to do a PUT with WebClient?

There are overloads for UploadString that let you specify the method. For example, this one takes a Uri, a string for the method, and a string for the data. using (var webClient = new WebClient()) { webClient.UploadString(apiUrl, WebRequestMethods.Http.Put, // or simply use “PUT” JsonConvert.SerializeObject(payload)) }

Best practices for using ServerCertificateValidationCallback

An acceptable (safe) methodology working in .NET 4.5+ is to use HttpWebRequest.ServerCertificateValidationCallback. Assigning that callback on a specific instance of request will change the validation logic just for the request, not influencing other requests. var request = (HttpWebRequest)WebRequest.Create(“https://…”); request.ServerCertificateValidationCallback += (sender, cert, chain, error) => { return cert.GetCertHashString() == “xxxxxxxxxxxxxxxx”; };

Authenticate and request a user’s timeline with Twitter API 1.1 oAuth

Here is what I did to get this working in a simple example. I had to generate an oAuth consumer key and secret from Twitter at: https://dev.twitter.com/apps/new I deserialized the authentication object first to get the token and type back in order to authenticate the timeline call. The timeline call simply reads the json as … Read more

The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF, In WinForms?

First, adding an app.config file is just as easy as adding any other file, How to: Add an Application Configuration File to a C# Project Then you just have to add that code snippet above to that new app.config. Another way of setting that property via code, avoiding the need for an app.config is shown … Read more

Automatically decompress gzip response via WebClient.DownloadData

WebClient uses HttpWebRequest under the covers. And HttpWebRequest supports gzip/deflate decompression. See HttpWebRequest AutomaticDecompression property However, WebClient class does not expose this property directly. So you will have to derive from it to set the property on the underlying HttpWebRequest. class MyWebClient : WebClient { protected override WebRequest GetWebRequest(Uri address) { HttpWebRequest request = base.GetWebRequest(address) … Read more

WebClient Unicode – Which UTF8?

They’re identical. UTF8Encoding inherits Encoding. Therefore, you can access all of the static members declared by Encoding through the UTF8Encoding qualifier. In fact, you can even write ASCIIEncoding.UTF8, and it will still work. It will compile to identical IL, even in debug mode. I would recommend using Encoding.UTF8, as it shows what’s going on more … Read more

ASP.NET Controller: An asynchronous module or handler completed while an asynchronous operation was still pending

In Async Void, ASP.Net, and Count of Outstanding Operations, Stephan Cleary explains the root of this error: Historically, ASP.NET has supported clean asynchronous operations since .NET 2.0 via the Event-based Asynchronous Pattern (EAP), in which asynchronous components notify the SynchronizationContext of their starting and completing. What is happening is that you’re firing DownloadAsync inside your … Read more

WebClient + HTTPS Issues

The shortest notation of the code to allow all certificates is actually: ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; And works well for this error. Needless to say that you should provide an implementation which actually checks the certificate and decides based on the certificate information if the communication is safe. For test purposes, use … Read more