Re-Send HttpRequestMessage – Exception

I wrote the following extension method to clone the request. public static HttpRequestMessage Clone(this HttpRequestMessage req) { HttpRequestMessage clone = new HttpRequestMessage(req.Method, req.RequestUri); clone.Content = req.Content; clone.Version = req.Version; foreach (KeyValuePair<string, object> prop in req.Properties) { clone.Properties.Add(prop); } foreach (KeyValuePair<string, IEnumerable<string>> header in req.Headers) { clone.Headers.TryAddWithoutValidation(header.Key, header.Value); } return clone; }

When or if to Dispose HttpResponseMessage when calling ReadAsStreamAsync?

So it seems like the calling code needs to know about and take ownership of the response message as well as the stream, or I leave the response message undisposed and let the finalizer deal with it. Neither option feels right. In this specific case, there are no finalizers. Neither HttpResponseMessage or HttpRequestMessage implement a … 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

How to send a file and form data with HttpClient in C#

Here’s code I’m using to post form information and a csv file using (var httpClient = new HttpClient()) { var surveyBytes = ConvertToByteArray(surveyResponse); httpClient.DefaultRequestHeaders.Add(“X-API-TOKEN”, _apiToken); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(“application/json”)); var byteArrayContent = new ByteArrayContent(surveyBytes); byteArrayContent.Headers.ContentType = MediaTypeHeaderValue.Parse(“text/csv”); var response = await httpClient.PostAsync(_importUrl, new MultipartFormDataContent { {new StringContent(surveyId), “\”surveyId\””}, {byteArrayContent, “\”file\””, “\”feedback.csv\””} }); return response; } This is … Read more

Post byte array to Web API server using HttpClient

WebAPI v2.1 and beyond supports BSON (Binary JSON) out of the box, and even has a MediaTypeFormatter included for it. This means you can post your entire message in binary format. If you want to use it, you’ll need to set it in WebApiConfig: public static class WebApiConfig { public static void Register(HttpConfiguration config) { … Read more

PATCH Async requests with Windows.Web.Http.HttpClient class

I found how to do a “custom” PATCH request with the previous System.Net.Http.HttpClient class here, and then fiddled with until I made it work in the Windows.Web.Http.HttpClient class, like so: public async Task<HttpResponseMessage> PatchAsync(HttpClient client, Uri requestUri, IHttpContent iContent) { var method = new HttpMethod(“PATCH”); var request = new HttpRequestMessage(method, requestUri) { Content = iContent … Read more

Why use HttpClient for Synchronous Connection

but what i am doing is purely synchronous You could use HttpClient for synchronous requests just fine: using (var client = new HttpClient()) { var response = client.GetAsync(“http://google.com”).Result; if (response.IsSuccessStatusCode) { var responseContent = response.Content; // by calling .Result you are synchronously reading the result string responseString = responseContent.ReadAsStringAsync().Result; Console.WriteLine(responseString); } } As far as … Read more

ASP.NET WebApi: how to perform a multipart post with file upload using WebApi HttpClient

After much trial and error, here’s code that actually works: using (var client = new HttpClient()) { using (var content = new MultipartFormDataContent()) { var values = new[] { new KeyValuePair<string, string>(“Foo”, “Bar”), new KeyValuePair<string, string>(“More”, “Less”), }; foreach (var keyValuePair in values) { content.Add(new StringContent(keyValuePair.Value), keyValuePair.Key); } var fileContent = new ByteArrayContent(System.IO.File.ReadAllBytes(fileName)); fileContent.Headers.ContentDisposition = … Read more