dotnet-httpclient

HttpClient with infinite time out throws time out exception

爱⌒轻易说出口 提交于 2019-12-03 15:49:31
My HttpClient uses digest authentication to connect to the server and expects search queries in response. These search queries can come in any time so the client is expected to leave the connection open at all times. The connection is made using the following code: public static async void ListenForSearchQueries(int resourceId) { var url = $"xxx/yyy/{resourceId}/waitForSearchRequest?token=abc"; var httpHandler = new HttpClientHandler { PreAuthenticate = true }; using (var digestAuthMessageHandler = new DigestAuthMessageHandler(httpHandler, "user", "password")) using (var client = new

Is it advantageous to use ConfigureAwait(false) in a library that directly returns a Task from a call to another library?

邮差的信 提交于 2019-12-03 12:52:06
Follow-up to this question . I have a library with many async methods that thinly wrap HttpClient . Effectively they just do some setup and directly return the Task returned from the HttpClient call: public Task DoThingAsyc() { // do some setup return httpClient.DoThingAsync(); } I'm pondering whether to add ConfigureAwait(false) to these calls. The prevailing wisdom seems to be "yes, always do that in libraries." But in this case, it would introduce some (perhaps negligible) overhead, because ConfigureAwait returns a ConfiguredTaskAwaitable which would need to be wrapped back into a Task in

What is the best way to compress a request to asp.net core 2 site using HttpClient?

丶灬走出姿态 提交于 2019-12-03 12:45:28
问题 I am sending request that can be significantly big(~1Mb) and I am seeing a large delay betweeen when I make the request and when asp.net core logs that it is handling the request. I think I can cut down this time by compressing the request to asp using gzip. Below is the fairly straight forward way that I am making requests without compression. What is the proper way to implement Gzip request compression on the client requesting side, and once I implement it on the client, what do I need to

Using custom SSL client certificates System.Net.HttpClient on Mono

北城以北 提交于 2019-12-03 12:38:03
I'm using Microsoft HTTP Client Libraries from NuGet and I'm basically trying to allow TLS authentication in HttpClient using X509Certificate2 certificates. I have tried creating the client like this: WebRequestHandler certHandler = new WebRequestHandler () { ClientCertificateOptions = ClientCertificateOption.Manual, UseDefaultCredentials = false }; certHandler.ClientCertificates.Add (this.ClientCertificate); HttpClient client = new HttpClient (certHandler); However certHandler.ClientCertificates is failing because this getter is not implemented in Mono , so I get a NotImplementedException

Calling external HTTP service using HttpClient from a Web API Action

雨燕双飞 提交于 2019-12-03 10:48:04
问题 I am calling an external service using HttpClient from within an ASP.Net MVC 4 Web Api project running on .Net Framework 4.5 The sample code is as follows (ignore the return values as this is sample code to test calling an external service): public class ValuesController : ApiController { static string _address = "http://api.worldbank.org/countries?format=json"; private string result; // GET api/values public IEnumerable<string> Get() { GetResponse(); return new string[] { result, "value2" };

How do I set a response cookie on HttpReponseMessage?

独自空忆成欢 提交于 2019-12-03 09:59:43
I would like to create a demo login service in web api and need to set a cookie on the response. How do I do that? Or are there any better way to do authorization? Add a reference to System.Net.Http.Formatting.dll and use the AddCookies extension method defined in the HttpResponseHeadersExtensions class. Here is a blog post describing this approach , and the MSDN topic . If that assembly isn't an option for you, here's my older answer from before this was an option: Older answer follows I prefer an approach that stays within the realm of HttpResponseMessage without bleeding into the

Dispose of Injected HttpClient

十年热恋 提交于 2019-12-03 09:08:50
Our MVC application calls a WebAPI action using HttpClient. I decided to inject the HttpClient using StructureMap and override dispose in the controller public HomeController(HttpClient httpClient) { _httpClient = httpClient; } protected override void Dispose(bool disposing) { if (disposing && _httpClient != null) { _httpClient.Dispose(); } base.Dispose(disposing); } The StructureMap ObjectInitialize basically looks like this.. x.For<HttpClient>().Use(() => new HttpClient() { BaseAddress = "my/uri/"}); When I build this, CodeAnalysis complains "Dispose objects before losing scope" and points

Connection was closed error between HttpClient and ASP.NET Core 2.0 webservice

浪子不回头ぞ 提交于 2019-12-03 07:54:52
I have a ASP.NET Core 2.0 webservice running on IIS. One of controller's methods looks more or less like this: [HttpGet()] public IActionResult Test() { // do some db updates and get data var result = DoSomeStuff(); // serialize data to byte array var output = Serialize(result); return File(output, "application/octet-stream"); } It does some database updates, query records from a table, serialize data and send them as a response. Data are sent in binary format. I'm using MessagePack-CSharp as a serializer. Then I have client application that communicates with this webservice. It's .NET

Make http client synchronous: wait for response

笑着哭i 提交于 2019-12-03 04:46:29
问题 I have some file to upload and some of the files failed because the post is asynchronous and not synchronous.. I'm trying to make this call as synchronized call.. I want to wait for the response. How can I make this call as synchronous? static async Task<JObect> Upload(string key, string url, string sourceFile, string targetFormat) { using (HttpClientHandler handler = new HttpClientHandler { Credentials = new NetworkCredential(key, "") }) using (HttpClient client = new HttpClient(handler)) {

What is the best way to compress a request to asp.net core 2 site using HttpClient?

左心房为你撑大大i 提交于 2019-12-03 03:12:00
I am sending request that can be significantly big(~1Mb) and I am seeing a large delay betweeen when I make the request and when asp.net core logs that it is handling the request. I think I can cut down this time by compressing the request to asp using gzip. Below is the fairly straight forward way that I am making requests without compression. What is the proper way to implement Gzip request compression on the client requesting side, and once I implement it on the client, what do I need to do for the server side? using (HttpResponseMessage response = client.PostAsync("Controller/Action",