dotnet-httpclient

How to clone a HttpRequestMessage when the original request has Content?

早过忘川 提交于 2019-12-18 03:29:09
问题 I'm trying to clone a request using the method outlined in this answer: https://stackoverflow.com/a/18014515/406322 However, I get an ObjectDisposedException, if the original request has content. How can you reliably clone a HttpRequestMessage? 回答1: This should do the trick: public static async Task<HttpRequestMessage> CloneHttpRequestMessageAsync(HttpRequestMessage req) { HttpRequestMessage clone = new HttpRequestMessage(req.Method, req.RequestUri); // Copy the request's content (via a

HttpClient: How to upload multiple files at once

烂漫一生 提交于 2019-12-17 22:39:18
问题 I am trying to upload muliple files using System.Net.Http.HttpClient. using (var content = new MultipartFormDataContent()) { content.Add(new StreamContent(imageStream), "image", "image.jpg"); content.Add(new StreamContent(signatureStream), "signature", "image.jpg.sig"); var response = await httpClient.PostAsync(_profileImageUploadUri, content); response.EnsureSuccessStatusCode(); } this only sends mulipart/form-data, but i expected multipart/mixed somewhere in the post. UPDATE: Ok, i got

HttpClientFactory.Create vs new HttpClient

不想你离开。 提交于 2019-12-17 21:53:27
问题 I am curious what is the purpose of the HttpClientFactory class. There is no description of why it exists on MSDN (see link). There are Create methods with more specialized arguments, but mostly I wonder what is the difference between the call with no parameters and the normal constructor. var httpClient = HttpClientFactory.Create(); VS var httpClient = new HttpClient(); In most examples I see the use of new HttpClient() , without any using statements, even though the HttpClient class derives

HttpClient Singleton Implementation in ASP.NET MVC

痞子三分冷 提交于 2019-12-17 19:02:11
问题 After reading this blog post and thisofficial note on www.asp.net: HttpClient is intended to be instantiated once and re-used throughout the life of an application. Especially in server applications, creating a new HttpClient instance for every request will exhaust the number of sockets available under heavy loads. This will result in SocketException errors. I discovered that our code was disposing the HttpClient on each call. I'm updating our code so that we reuse the HttClient, but I'm

How do I get StatusCode from HttpRequestException?

旧巷老猫 提交于 2019-12-17 18:45:03
问题 I'm probably missing something obvious here. I'm using HttpClient which throws HttpRequestException that contains StatusCode in the Message string. How can I access that StatusCode ? Edit : More info, I wrote this question in rush. I'm using HttpClient to access another API within my WebApi project. Yes, I know why I'm calling EnsureSuccessStatusCode() . I want to propagate some errors downstream such as 404 and 403. All I wanted was to consistently transform HttpRequestException into

Distinguish timeout from user cancellation

北城余情 提交于 2019-12-17 18:08:10
问题 HttpClient has a builtin timeout feature (despite being all asynchronous, i.e. timeouts could be considered orthogonal to the http request functionality and thus be handled by generic asynchronous utilities, but that aside) and when the timeout kicks in, it'll throw a TaskCanceledException (wrapped in an AggregateException ). The TCE contains a CancellationToken that equals CancellationToken.None . Now if I provide HttpClient with a CancellationToken of my own and use that to cancel the

HttpClient: The uri string is too long

南楼画角 提交于 2019-12-17 16:33:53
问题 Given the following attempt to post data to a web service that generates PDF files, PDF rocket ( which is awesome by the way ). I get the error Invalid URI: The uri string is too long Why would anyone impose an arbitrary limit on POST ed data? using (var client = new HttpClient()) { // Build the conversion options var options = new Dictionary<string, string> { { "value", html }, { "apikey", ConfigurationManager.AppSettings["pdf:key"] }, { "MarginLeft", "10" }, { "MarginRight", "10" } }; //

How to implement progress reporting for Portable HttpClient

非 Y 不嫁゛ 提交于 2019-12-17 15:45:07
问题 I'm writing a library with intentions to use it in desktop (.Net 4.0 and up), phone (WP 7.5 and up) and Windows Store (Windows 8 and up) apps. The library has the capability to download files from the Internet using Portable HttpClient library, and report the progress of the download. I search around here and the rest of the internet for documentations and code sample/guidelines on how to implement the progress reporting, and this search led me to nowhere. Does anyone has an article,

Re-Send HttpRequestMessage - Exception

对着背影说爱祢 提交于 2019-12-17 15:38:52
问题 I want to send the exact same request more than once, for example: HttpClient client = new HttpClient(); HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get, "http://example.com"); await client.SendAsync(req, HttpCompletionOption.ResponseContentRead); await client.SendAsync(req, HttpCompletionOption.ResponseContentRead); Sending the request for a second time will throw an exception with the message: The request message was already sent. Cannot send the same request message multiple

Async call with await in HttpClient never returns

≡放荡痞女 提交于 2019-12-17 08:17:15
问题 I have a call I am making from inside a xaml-based, C# metro application on the Win8 CP; this call simply hits a web service and returns JSON data. HttpMessageHandler handler = new HttpClientHandler(); HttpClient httpClient = new HttpClient(handler); httpClient.BaseAddress = new Uri("http://192.168.1.101/api/"); var result = await httpClient.GetStreamAsync("weeklyplan"); DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(WeeklyPlanData[])); return (WeeklyPlanData[])ser