dotnet-httpclient

Refresh Token using Static HttpClient

馋奶兔 提交于 2020-01-02 06:36:03
问题 Using VS 2017 .Net 4.5.2 I have the following class public static class MyHttpClient { //fields private static Lazy<Task<HttpClient>> _Client = new Lazy<Task<HttpClient>>(async () => { var client = new HttpClient(); await InitClient(client).ConfigureAwait(false); return client; }); //properties public static Task<HttpClient> ClientTask => _Client.Value; //methods private static async Task InitClient(HttpClient client) { //resey headers client.DefaultRequestHeaders.Clear(); //Set base URL, NOT

How to ignor self signed certificates with System.Net.Http.HttpClient in Universal Windows App

拟墨画扇 提交于 2020-01-02 04:09:26
问题 I am creating a Portable Class Library which means I must use System.Net.Http.HttpClient to call my web APIs as far as I understand. The challenge is that for my Universal Windows App, I cannot figure out how to ignore the error that is returned due to the fact the fact the API server can have a self signed certificate. Any suggestions would be greatly appreciated. UPDATE: I cannot import any certificates as this will be an application that runs on various devices in various organizations and

How to consume HttpClient from F#?

匆匆过客 提交于 2020-01-02 01:05:10
问题 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#? 回答1: 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

New HttpClient proxy settings issues

走远了吗. 提交于 2020-01-01 18:17:28
问题 I'm trying to rewrite old network authentication logic according to new fw 4.5 features such as HttpClient and await/async and i experience unexpected latency (around 15s) between request and response. My guess is that happens because client tries to find/use proxy from IE like it was with old HttpRequest/WebClient. Here's the code : public static async Task<AuthResult> GetDataFromServiceAsync(string url, string login, string password) { Debug.WriteLine("[" + DateTime.Now + "]

How to use HttpClient to send a Request from a specific IP address? C#

霸气de小男生 提交于 2019-12-30 12:08:26
问题 I have multiple IPs on the server and would like to chose which one of those I want to use when using HttpClient class to get/post data from an API. (or to even send requests at the same time but utilise the 2 IPs and not just one) I have seen some examples using HttpWebRequest (here) that utilises delegate but I would like to carry on using HttpClient implementation. 回答1: [ This will be a hacky code, because there is no method/property to access the ServicePoint ] You can use reflection to

Paralell.ForEach with HttpClient and ContinueWith

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-30 06:56:18
问题 I have a method that attempts to download data from several URLs in Parallel, and return an IEnumerable of Deserialized types The method looks like this: public IEnumerable<TContent> DownloadContentFromUrls(IEnumerable<string> urls) { var list = new List<TContent>(); Parallel.ForEach(urls, url => { lock (list) { _httpClient.GetAsync(url).ContinueWith(request => { var response = request.Result; //todo ensure success? response.Content.ReadAsStringAsync().ContinueWith(text => { var results =

How to use HttpClientHandler with HttpClientFactory in .NET Core

半城伤御伤魂 提交于 2019-12-30 05:44:08
问题 I want to use the HttpClientFactory that is available in .NET Core 2.1 but I also want to use the HttpClientHandler to utilize the AutomaticDecompression property when creating HttpClients . I am struggling because the .AddHttpMessageHandler<> takes a DelegatingHandler not a HttpClientHandler . Does anyone know how to get this to work? Thanks, Jim 回答1: Actually I'm not using automatic decompression but the way to achieve this is to properly register http client services.AddHttpClient

Cancel call to 'HttpClient.SendAsync()'

旧街凉风 提交于 2019-12-29 08:04:50
问题 Is it possible to cancel a call to HttpClient.SendAsync() ? I'm sending some data like this: var requestMessage = new HttpRequestMessage(HttpMethod.Post, "some url"); var multipartFormDataContent = new MultipartFormDataContent(); // ... construction of the MultipartFormDataContent. It contains form data + picture file requestMessage.Content = multipartFormDataContent; var response = await client.SendAsync(requestMessage).ConfigureAwait(false); This code works perfectly, but I need to be able

How to copy HttpContent async and cancelable?

走远了吗. 提交于 2019-12-29 06:49:14
问题 I'm using HttpClient.PostAsync() and the response is an HttpResponseMessage . Its Content property is of type HttpContent which has a CopyToAsync() method. Unfortunately, this is not cancelable. Is there a way to get the response copied into a Stream and pass a CancellationToken ? I am not stuck with CopyToAsync() ! If there is a workaround, that would be fine. Like read a couple of bytes, check if canceled, continue reading and so on. The HttpContent.CreateContentReadStreamAsync() methods

Whats the difference between HttpClient.Timeout and using the WebRequestHandler timeout properties?

回眸只為那壹抹淺笑 提交于 2019-12-28 15:06:19
问题 I can set the timeout of my HttpClient object directly with HttpClient.Timeout but I've recently read about the WebRequestHandler class which is a derivative of HttpClientHandler . WebRequestHandler has a ReadWriteTimeout property. How will this affect the operation of the request when used alongside HttpClient.Timeout ? 回答1: When you perform a SendAsync the HttpClient.Timeout is placed on the CancellationTokenSource . This means this timeout is for the entire async operation. On the other