Make Https call using HttpClient

If the server only supports higher TLS version like TLS 1.2 only, it will still fail unless your client PC is configured to use higher TLS version by default. To overcome this problem, add the following in your code: System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; Modifying your code example, it would be HttpClient httpClient … Read more

HttpClient.GetAsync(…) never returns when using await/async

You are misusing the API. Here’s the situation: in ASP.NET, only one thread can handle a request at a time. You can do some parallel processing if necessary (borrowing additional threads from the thread pool), but only one thread would have the request context (the additional threads do not have the request context). This is … Read more

.NET HttpClient. How to POST string value?

using System; using System.Collections.Generic; using System.Net.Http; class Program { static void Main(string[] args) { Task.Run(() => MainAsync()); Console.ReadLine(); } static async Task MainAsync() { using (var client = new HttpClient()) { client.BaseAddress = new Uri(“http://localhost:6740”); var content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>(“”, “login”) }); var result = await client.PostAsync(“/api/Membership/exists”, content); string resultContent = … Read more

What is the overhead of creating a new HttpClient per call in a WebAPI client?

HttpClient has been designed to be re-used for multiple calls. Even across multiple threads. The HttpClientHandler has Credentials and Cookies that are intended to be re-used across calls. Having a new HttpClient instance requires re-setting up all of that stuff. Also, the DefaultRequestHeaders property contains properties that are intended for multiple calls. Having to reset … Read more

C# HttpClient 4.5 multipart/form-data upload

my result looks like this: public static async Task<string> Upload(byte[] image) { using (var client = new HttpClient()) { using (var content = new MultipartFormDataContent(“Upload—-” + DateTime.Now.ToString(CultureInfo.InvariantCulture))) { content.Add(new StreamContent(new MemoryStream(image)), “bilddatei”, “upload.jpg”); using ( var message = await client.PostAsync(“http://www.directupload.net/index.php?mode=upload”, content)) { var input = await message.Content.ReadAsStringAsync(); return !string.IsNullOrWhiteSpace(input) ? Regex.Match(input, @”http://\w*\.directupload\.net/images/\d*/\w*\.[a-z]{3}”).Value : null; } … Read more

How do you set the Content-Type header for an HttpClient request?

The content type is a header of the content, not of the request, which is why this is failing. AddWithoutValidation as suggested by Robert Levy may work, but you can also set the content type when creating the request content itself (note that the code snippet adds application/json in two places-for Accept and Content-Type headers): … Read more

Do HttpClient and HttpClientHandler have to be disposed between requests?

The general consensus is that you do not (should not) need to dispose of HttpClient. Many people who are intimately involved in the way it works have stated this. See Darrel Miller’s blog post and a related SO post: HttpClient crawling results in memory leak for reference. I’d also strongly suggest that you read the … Read more