dotnet-httpclient

How to consume HttpClient from F#?

ぃ、小莉子 提交于 2019-12-05 00:40:57
I'm new to F# and stuck in understanding async in F# from the perspective of a C# developer. Say having the following snippet in C#: var httpClient = new HttpClient(); var response = await httpClient.GetAsync(url); response.EnsureSuccessStatusCode(); string content = await response.Content.ReadAsStringAsync(); How to write the same in F#? Here is a function that should do what you're looking for (note that you'll have to wrap the code in an asynchronous computation expression in order to use the let! syntax): let getAsync (url:string) = async { let httpClient = new System.Net.Http.HttpClient()

Using HttpClient for Asynchronous File Downloads

自古美人都是妖i 提交于 2019-12-04 21:47:35
问题 I have a service which returns a csv file to a POST request. I would like to download said file using asynchronous techniques. While I can get the file, my code has a couple of outstanding problems and questions: 1) Is this really asynchronous? 2) Is there a way to know the length of the content even though it is being sent in chunked format? Think progress bars). 3) How can I best monitor progress in order to hold off the program exit until all work is complete. using System; using System.IO

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

谁都会走 提交于 2019-12-04 20:36:04
问题 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

WP8 HttpClient.PostAsync never returns result

烂漫一生 提交于 2019-12-04 19:05:15
I have a Windows Phone 8 app where I am calling await HttpClient.PostAsync and it never returns a result. It just sits there and hangs. If I run the exact same code from a console app, it returns the result almost immediately. All of the code doing the work resides in a portable class library. I would appreciate any help you may be able to give. All of the other issues I have found on this state to use await client.PostAsync, which I am already doing. The code in my class library is as such: public class Authenticator { private const string ApiBaseUrl = "http://api.fitbit.com"; private const

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

怎甘沉沦 提交于 2019-12-04 17:55:05
问题 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

checking Internet connection with HttpClient

谁说我不能喝 提交于 2019-12-04 16:48:36
I am having difficulties to understand on how the bellow code could handle occasional internet connection loss. Ideally I would like to pause the app, once the connection is lost, and resume when it is up again. Is there any guideline on how to do it? HttpClientHandler clientHandler = new HttpClientHandler(); clientHandler.UseDefaultCredentials = true; HttpClient client = new HttpClient(clientHandler) { MaxResponseContentBufferSize = 1000000 }; HttpResponseMessage response = await client.GetAsync(Url, ct); The following example is not a direct solution, but it is an example I built to show how

How to authenticate WPF Client request to ASP .NET WebAPI 2

折月煮酒 提交于 2019-12-04 09:30:56
问题 I just created an ASP .NET MVC 5 Web API project and added the Entity Framework model and other things to get it working with ASP. NET Identity. Now I need to create a simple authenticated request to the standard method of that API out there from the WPF Client app. ASP .NET MVC 5 Web API code [Authorize] [RoutePrefix("api/Account")] public class AccountController : ApiController // GET api/Account/UserInfo [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)] [Route("UserInfo")]

Properly Abort or Cancel PostAsync

纵饮孤独 提交于 2019-12-04 06:45:27
Is there a guaranteed way to cancel a post when using HttpClient? I currently have a call do PostAsync that I am attempting to cancel using a cancellationToken, but it appears that it does not actually abort/stop the operation. I still can see that the image I am uploading is posted properly. Am I doing something wrong here or is it possible that HttpClient is not processing the cancellation token until after the upload? var sc = new StreamContent(uploadFile.Data); content.Add(sc, uploadFile.FieldName, uploadFile.FileName); var request = new HttpRequestMessage { RequestUri = new Uri

How to call web API under specific user permission?

陌路散爱 提交于 2019-12-04 05:49:13
问题 I have a function that allows the end user to execute a Workflow (containing many APIs) or schedule it to run as a background job. Example: User1 creates Workflow1 , which contains 3 APIs ( Api1 , Api2 , Api3 ), and configures it to run at 9AM every day. I use HttpClient to call each API like this: var client = new HttpClient { BaseAddress = new Uri("http://localhost/") }; client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage

HttpClient on WebApi is extremely slow

不打扰是莪最后的温柔 提交于 2019-12-04 04:46:39
问题 I'm implementing a Proxy for my application using ASP.NET WebApi (ApiController) and using HttpClient to make the request with my authorization header. It works fine, but it's extremely slow. Below is the main code, then the Global initialization (with DefaultConnectionLimit) and web.config related piece. As you can see, I'm already using a static/shared HttpClient object with no Proxy and HttpCompletionOption.ResponseHeadersRead on the actual request. This WebApi endpoint is called in