dotnet-httpclient

C# HttpClient An existing connection was forcibly closed by the remote host

。_饼干妹妹 提交于 2019-12-05 08:29:32
问题 I'm working on an integration with Alternative Payments using their hosted page integration. Their C# SDK does not have this integration available at the moment, but as you can see it's pretty simple and I made a small class to send the post request and get the JSON response. I tested the json object I'm sending on PostMan and cURL and both work, also the authentication header, so I think they are not the problem. Here is the constructor of my class: public AlternativePaymentsCli(string

How to prevent HttpClient from sending the Connection header

…衆ロ難τιáo~ 提交于 2019-12-05 07:54:50
HttpClient includes this as 'Keep-Alive' by default. I've been able to set it to 'Close' using httpClient.DefaultRequestHeaders.ConnectionClose = true; but how do I omit it altogether? I've seen this asked before and to my knowledge no one ( not even the great Jon Skeet ) has come up with a way to avoid sending the Connection header in the HttpClient or HttpWebRequest worlds without completely reinventing the wheel. It's happening somewhere very deep down in the weeds. ConnectionClose is nullable, but setting it to null on both HttpClient.DefaultRequestHeaders AND HttpRequestMethod.Headers

HttpClient PostAsync posting null content

断了今生、忘了曾经 提交于 2019-12-05 07:32:51
I have one api calling another. and here is my code which seems to cause ModelState.IsValid = false on the other side of world. var baseUri = new Uri("http://localhost:5001/"): _httpClient.BaseAdress = baseUri; var data = new StringContent(content: model.Tostring(), encoding: Encoding.UTF8, mediaType: "application/json"); var response = await _httpClient.PostAsync("api/product", data); watching Post([FromBody]Product product) on the api being called I just see product=null . changing to Post([FromBody]object product) also shows null . calling the api from Postman works perfectly fine. which

How to call PUT method from Web Api using HttpClient?

你说的曾经没有我的故事 提交于 2019-12-05 06:30:52
I want to call Api function (1st) . from 2nd Api function using HttpClient . But I always get 404 Error. 1st Api Function ( EndPoint : http : // localhost : xxxxx /api/Test/) public HttpResponseMessage Put(int id, int accountId, byte[] content) [...] 2nd Api function public HttpResponseMessage Put(int id, int aid, byte[] filecontent) { WebRequestHandler handler = new WebRequestHandler() { AllowAutoRedirect = false, UseProxy = false }; using (HttpClient client = new HttpClient(handler)) { client.BaseAddress = new Uri("http://localhost:xxxxx/"); // Add an Accept header for JSON format. client

Should I cache and reuse HttpClient created from HttpClientFactory?

↘锁芯ラ 提交于 2019-12-05 05:38:34
We can read here YOU'RE USING HTTPCLIENT WRONG AND IT IS DESTABILIZING YOUR SOFTWARE that we should not create and dispose HttpClient for each http request. Instead, it should be cached and reused (e.g as Singleton in DI container). As well in official .NET documentation for HttpClient : HttpClient is intended to be instantiated once and re-used throughout the life of an application. Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads. This will result in SocketException errors. Below is an example using HttpClient correctly. The

Progress bar for HttpClient uploading

旧时模样 提交于 2019-12-05 05:17:35
I want to run asynchronous uploads with a progress bar in WPF (and preferably use PCL for code reuse in Xamarin and SL too.) I've been trying to use System.Net.HttpClient. Unfortunately, PutAsync doesn't provide any progress notifications. (I know that it does for Windows.Web.Http.HttpClient, but that's not available for WPF, nor in the PCL). For downloading, its fairly easy to implement your own progress bar, as described here . You just pass the ResponseHeadersRead option, which makes the stream available as soon as the headers are returned, and then you read it in chunk by chunk,

Why Wikipedia returns 301 response code with certain URL?

安稳与你 提交于 2019-12-05 04:27:21
问题 Some requests with special character in this with the french accents. var client = new HttpClient(); var data0 = await client.GetAsync("http://fr.wikipedia.org/wiki/Monastère_d'Arkadi"); This simple code yields: StatusCode: 301, ReasonPhrase: 'Moved Permanently', Version: 1.1 Any ideas what is happening? Downloading the French article of New York works in fact. I´ve even try to encode the the name but nothing works. 回答1: Wikipedia sends an HTTP 301 indicating that the permanent home of http:/

The request message was already sent. Cannot send the same request message multiple times

こ雲淡風輕ζ 提交于 2019-12-05 04:01:16
Is there anything wrong with my code here? I keep getting this error: System.InvalidOperationException: The request message was already sent. Cannot send the same request message multiple times. My HttpRequestMessage is inside a Func so I figured I get a brand new request each time I pass in func(). public async Task<HttpResponseMessage> GetAsync(HttpRequestMessage request) { return await RequestAsync(() => request); } public async Task<HttpResponseMessage> RequestAsync(Func<HttpRequestMessage> func) { var response = await ProcessRequestAsync(func); if (response.StatusCode == HttpStatusCode

Read headers from HttpResponseMessage before Content is 100% complete

佐手、 提交于 2019-12-05 01:41:04
How do I access the response headers, before the entire response has been streamed back? How do I read the stream as it arrives? Is HttpClient my best choice for such granular control of receiving the http response? Here's a snip that might illustrate my question: using (var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)) { var streamTask = response.Content.ReadAsStreamAsync(); //how do I check if headers portion has completed? //Does HttpCompletionOption.ResponseHeadersRead guarantee that? //pseudocode while (!(all headers have been received)) /

C#: HttpClient, The server committed a protocol violation. Section=ResponseStatusLine

放肆的年华 提交于 2019-12-05 01:35:03
I'm using the HttpClient class to communicate to a web service in my WPF application. When I make consecutive GET requests on the same connection, everything works fine. However, when I make consecutive PUT/PATCH requests on the same connection, the first request executes accurately and I receive a response but the second request does not include the body in the request and I receive the infamous error of "The server committed a protocol violation. Section=ResponseStatusLine". My requests do complete successfully if I manually close the connection after every request by adding Connection: