Trying to run multiple HTTP requests in parallel, but being limited by Windows (registry)

It is matter of ServicePoint. Which provides connection management for HTTP connections. The default maximum number of concurrent connections allowed by a ServicePoint object is 2. So if you need to increase it you can use ServicePointManager.DefaultConnectionLimit property. Just check the link in MSDN there you can see a sample. And set the value you … Read more

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

.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