Add client certificate to .NET Core HttpClient

I ran a fresh install for my platform (Linux Mint 17.3) following these steps: .NET Tutorial – Hello World in 5 minutes. I created a new console application targeting the netcoreapp1.0 framework, was able to submit a client certificate; however, I did receive “SSL connect error” (CURLE_SSL_CONNECT_ERROR 35) while testing, even though I used a … Read more

Deserialize JSON to Array or List with HTTPClient .ReadAsAsync using .NET 4.0 Task pattern

Instead of handcranking your models try using something like the Json2csharp.com website. Paste In an example JSON response, the fuller the better and then pull in the resultant generated classes. This, at least, takes away some moving parts, will get you the shape of the JSON in csharp giving the serialiser an easier time and … Read more

HttpClient single instance with different authentication headers

If your headers are usually going to be the same then you can set the DefaultRequestHeaders. But you don’t need to use that property to specify headers. As you’ve determined, that just wouldn’t work if you’re going to have multiple threads using the same client. Changes to the default headers made on one thread would … Read more

How to post data using HttpClient?

You need to use: await client.PostAsync(uri, content); Something like that: var comment = “hello world”; var questionId = 1; var formContent = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>(“comment”, comment), new KeyValuePair<string, string>(“questionId”, questionId) }); var myHttpClient = new HttpClient(); var response = await myHttpClient.PostAsync(uri.ToString(), formContent); And if you need to get the response after post, … Read more

How do I get StatusCode from HttpRequestException?

Status code was passed as part of a string to HttpRequestException so that you cannot recover it from such exceptions alone. The design of System.Net.Http requires you to access HttpResponseMessage.StatusCode instead of waiting for the exception. http://msdn.microsoft.com/en-us/library/system.net.http.httpresponsemessage(v=vs.110).aspx If you are now following the Microsoft guide, make sure you understand clearly why it asks you to … Read more

Async call with await in HttpClient never returns

Check out this answer to my question which seems to be very similar. Something to try: call ConfigureAwait(false) on the Task returned by GetStreamAsync(). E.g. var result = await httpClient.GetStreamAsync(“weeklyplan”) .ConfigureAwait(continueOnCapturedContext:false); Whether or not this is useful depends on how your code above is being called – in my case calling the async method using … Read more

Is async HttpClient from .Net 4.5 a bad choice for intensive load applications?

Besides the tests mentioned in the question, I recently created some new ones involving much fewer HTTP calls (5000 compared to 1 million previously) but on requests that took much longer to execute (500 milliseconds compared to around 1 millisecond previously). Both tester applications, the synchronously multithreaded one (based on HttpWebRequest) and asynchronous I/O one … Read more

Struggling trying to get cookie out of response with HttpClient in .net 4.5

To add cookies to a request, populate the cookie container before the request with CookieContainer.Add(uri, cookie). After the request is made the cookie container will automatically be populated with all the cookies from the response. You can then call GetCookies() to retreive them. CookieContainer cookies = new CookieContainer(); HttpClientHandler handler = new HttpClientHandler(); handler.CookieContainer = … Read more

How do I pass an object to HttpClient.PostAsync and serialize as a JSON body?

The straight up answer to your question is: No. The signature for the PostAsync method is as follows: public Task PostAsync(Uri requestUri, HttpContent content) So, while you can pass an object to PostAsync it must be of type HttpContent and your anonymous type does not meet that criteria. However, there are ways to accomplish what … Read more