dotnet-httpclient

Can I change the properties of a HttpClientHandler after the HttpClient has been created?

社会主义新天地 提交于 2021-02-08 06:13:24
问题 The HttpClientHandler can be used as a parameter when creating a HttpClient object, but after that there doesn't seem to be any way to access the handler without keeping a reference to it. Dim Handler as New HttpClientHandler Handler.CookieContainer = Cookies Handler.Proxy = Proxy Handler.UseProxy = True Handler.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate Dim Client as New HttpClient(Handler, True) Am I able to change the properties of a handler of an

HttpClient.PostAsJsonAsync never sees when the post is succeeding and responding

断了今生、忘了曾经 提交于 2021-02-07 11:28:36
问题 We are using an HttpClient to post json to a restful web service. In one instance, we are running into something that has us baffled. Using tools like postman, fiddler etc, we can post to an endpoint and see that it is working. When we do the same with HttpClient.PostAsJsonAsync, we can verify in the software we are posting to that it received the data just fine. However, our PostAsJsonAsync will always eventually time out rather than give us a response. We have worked with the team that

HttpClient Instancing Per Service-Endpoint

谁说胖子不能爱 提交于 2021-02-07 11:19:20
问题 When instancing an HttpClient, the one common piece of advice is: Use a singleton, do not dispose after each use. However, based on this link I see commentary which I think implies another rule: The HttpClient class instance acts as a session to send HTTP requests. An HttpClient instance is a collection of settings applied to all requests executed by that instance. In addition, every HttpClient instance uses its own connection pool, isolating its requests from requests executed by other

C#: Getting 403 even after sending CSRF token using httpclient

落爺英雄遲暮 提交于 2021-02-05 07:12:06
问题 I am trying to post a payload to our backend system from my UWP app. For which I am first doing a GET to fetch the CSRF token and then adding that to the header of the POST request. While posting, I am still getting the 403 Forbidden error. I am cross-testing this with 'Insomnia' REST client by doing a separate GET and POST requests and feeding the CSRF token fetched from the GET to the POST header and it is working just fine. I am new to C#, so please excuse the bad coding standards. Getting

Can't get any cookies with C# HttpClient

牧云@^-^@ 提交于 2021-02-04 19:58:11
问题 I'm trying to get cookies on the Spotify login page with C# and the HttpClient class. However, the CookieContainer is always empty when I know cookies are being set. I'm not sending any headers, but it should still give me the cookie(s) because when I send a GET request without any headers with python (requests module) I get the csrf token. Here's my code: using System; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; using System

How to handle concurrency (status 409) errors using HttpContent in ASP.NET MVC Web application?

耗尽温柔 提交于 2021-01-29 15:31:07
问题 I'm working on a Core 3.1 Web API and an MVC application that uses it. In the MVC app I have UserRepo set up containing an Update method: public async Task<User> Update(User user) { HttpClient client = _clientFactory.CreateClient("namedClient"); HttpResponseMessage response = await client.PutAsync($"api/Users/{user.Id}", ContentEncoder.Encode(user)); try { response.EnsureSuccessStatusCode(); } catch (Exception ex) { if ((int)response.StatusCode == StatusCodes.Status409Conflict) { throw; } }

Best practice to handle content in the response after a Web API call results in an exception

安稳与你 提交于 2021-01-29 08:53:07
问题 I'm working on a Core 3.1 Web API and an MVC application that uses it. In the MVC app I have UserRepo set up containing methods that send requests to the API: public class UserRepo : IUserRepo { private readonly IHttpClientFactory _clientFactory; public UserRepo(IHttpClientFactory httpClientFactory) { _clientFactory = httpClientFactory; } public async Task<User> GetById(int Id) { // same code structure as Update ... } public async Task<User> Update(User user) { HttpClient client =

Is it better to use HttpClient or WebRequest in ASP.NET Core to read the content of a file line by line asynchronously remotely?

吃可爱长大的小学妹 提交于 2021-01-29 08:12:04
问题 I plan to read a remote file line by line asynchronously using https://github.com/Dasync/AsyncEnumerable (since there is not yet Async Streams [C# 8 maybe]: https://github.com/dotnet/csharplang/blob/master/proposals/async-streams.md): public static class StringExtensions { public static AsyncEnumerable<string> ReadLinesAsyncViaHttpClient(this string uri) { return new AsyncEnumerable<string>(async yield => { using (var httpClient = new HttpClient()) { using (var responseStream = await

Httpclient Slow Performance same computer Android Emulator Xamarin

╄→гoц情女王★ 提交于 2021-01-29 05:31:11
问题 I am using the HttpClient but my results are taking up to 6 seconds coming back from the same machine on the same subnet and ip range of 192.168. When I call the api directly from the ip address the results are more or less instant so why is it so slow with httpclient on the same computer. I have seen other so's that suggest set to use proxy as false is the best way to go. I have also tested this on a stock phone and it takes around 8 seconds for the login to be successful on the phone.

How do I enable logging for HttpClient in a .NET Core console app?

夙愿已清 提交于 2021-01-29 03:25:57
问题 .NET Core 2.2 I have a basic CLI created with dotnet new console . It uses HttpClient to make web requests. How can I see trace or logging information about these requests? Data like this: https://www.stevejgordon.co.uk/httpclientfactory-asp-net-core-logging In the .NET Framework, you could turn on System Tracing: https://docs.microsoft.com/en-us/dotnet/framework/network-programming/how-to-configure-network-tracing 回答1: In your appsettings.json file there will be an object which looks similar