Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Filter by
Sorted by
Tagged with
Best practices
0 votes
0 replies
23 views

Most efficient way to manage the lifecycle of HttpClient in many wrapper classes?

Our application is somewhat of a middleman that transforms data from numerous APIs. Initially to simplify things, we wrote a bunch of specific clients which wrap an instance of HttpClient created in ...
MetallicHydrogen's user avatar
Best practices
1 vote
4 replies
49 views

How do I parse multipart content from an HttpResponseMessage?

I am using HttpClient in .NET 9 to get content that has a JSON part along with a base-64 encoded file. Reading the content as a string, I don't see a clear format that would ensure consistent parsing ...
NeartCarp's user avatar
  • 227
0 votes
1 answer
165 views

C# Https call using HttpClient does work in Windows 7, but not in Windows 10

In Windows 7 32-bit, on .NET 4.6.1, this code works fine and the remote server is returning data: using (HttpClient client = new HttpClient()) { client.Timeout = TimeSpan.FromSeconds(3); ...
panci matika's user avatar
3 votes
2 answers
149 views

How to make system proxy settings apply to HttpClient.GetAsync() without restarting Windows?

We use the following function to test internet connectivity every 5 seconds. But in some part of the world, this function always returns false. Now, the problem is we can set a proxy server in Windows ...
Ming's user avatar
  • 597
1 vote
0 answers
55 views

Android HttpClient "Socket closed" error on slow connections, but server successfully processes request

I'm working on a .NET MAUI Android app that sends order data to a Web API using HttpClient. On normal Wi-Fi connections everything works fine, but when I throttle the network speed to around 56 kbps (...
Lasitha Lankajeewa's user avatar
1 vote
0 answers
60 views

HttpClient OAuth "No user found matching username"

I'm working on a console application to automatically interact with an API that needs and OAuth authorized user with a specific set of claims. If I use a browser to log in with a username and password ...
user31727878's user avatar
5 votes
3 answers
144 views

.NET IHttpClientFactory + Microsoft Resilience retry: same HttpRequestMessage resent, delegating handler not re-running?

I have an HttpClient built with IHttpClientFactory and Microsoft.Extensions.Http.Resilience retry on 401. Pipeline: Primary: HttpClientHandler (AllowAutoRedirect = false) Delegating: ....
Artyom Avetisyan's user avatar
2 votes
1 answer
115 views

Why am I getting a different HTTP response for a seemingly same request between curl, C# HttpClient Win10 and C# HttpClient Win11

I'm trying to download a webpage from a specific website (Bandcamp). I'm getting different results although the request seems the same: On Windows 10, using a HttpClient in C#: I'm getting "...
Otiel's user avatar
  • 18.8k
1 vote
1 answer
96 views

Set-Cookie header disappears after some time when using HttpClient with DelegatingHandler in ASP.NET Core

I have a .NET Core project with two layers: an API layer and a UI layer, both in the same solution. The UI layer is built with ASP.NET Core MVC, where controllers call services, and the services use ...
za mk's user avatar
  • 11
4 votes
1 answer
149 views

Can't listen to 'http://127.0.0.100:50001/' with HttpListener on my machine, even though the port does not appear to be in use

My HttpListener for some reason can't listen to prefix http://127.0.0.100:50001/, getting an error Unhandled exception. System.Net.HttpListenerException (32): The process cannot access the file ...
Roman Kovalov's user avatar
1 vote
0 answers
52 views

.NET HttpClient: How to read multiple responses from one request? [duplicate]

The http spec allows multiple responses per http request: A single request can have multiple associated responses: zero or more "interim" (non-final) responses with status codes in the &...
Volker's user avatar
  • 1,837
0 votes
0 answers
51 views

.NET MAUI for Android in connection API localhost [duplicate]

I have a problem, I get this error [0:] Connection failure: ERROR {0} when using a connection in a .NET MAUI project for Android. I applied the Microsoft instruction for the case when the API works ...
Артур Дементьев's user avatar
1 vote
1 answer
92 views

Interaction and communication between C# classes

I have an API service class that implements HttpClient to connect to my API, I have a user list model, and I have a View Model using the Community Toolkit MVVM approach. I would like to understand how ...
Артур Дементьев's user avatar
1 vote
1 answer
99 views

Binding a ViewModel to data from an API

I have a problem because I'm relatively new to this and I'm trying to learn and do things at the same time, and I'm not sure if I'm doing it correctly. I need to get data from my API in the view model ...
Артур Дементьев's user avatar
1 vote
1 answer
99 views

How to handle certificate revocation behind a firewall which blocks revocation checks

I'm using a .NET HttpClient to connect to a server over HTTPS. For best practice, I want to check the certificate hasn't been revoked, by setting HttpClientHandler.CheckCertificateRevocationList = ...
Simon W's user avatar
  • 111
0 votes
0 answers
61 views

Not receiving any response with HttpClient, despite one being sent

The calling application calls the auth endpoint of an API to receive a token. The API will, on successful authentication, return a JWT bearer token for the calling application to then use in ...
David Hirst's user avatar
  • 2,032
0 votes
0 answers
35 views

App Service sandbox blocks all forms of dynamic client certificate usage in HttpClient

I’m trying to make TLS calls using HttpClient in an Azure App Service (Windows) with a certificate that is dynamically loaded at runtime (e.g., from blob storage or Key Vault as a byte array). A third-...
Ellen's user avatar
  • 1
2 votes
1 answer
80 views

How to correctly create middleware extensions?

I am refactoring my code and moving some classes to an external assembly. In my projects I use Refit and currently I set it up like this in Startup: services.AddHttpClient(); services.AddRefitClient&...
Ivan Debono's user avatar
  • 1,016
4 votes
1 answer
545 views

ServicePointManager is obsolete in .NET 9 — how to configure SecurityProtocol (e.g., TLS 1.2/1.3) with HttpClient?

Working on project, we have upgraded to .Net 9. I am getting a warning in Program.cs at below line: ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13 | SecurityProtocolType.Tls12; ...
Mayur Narkhede's user avatar
0 votes
0 answers
99 views

Refit client returns cached responses despite no-cache headers and HttpMessageHandler

I'm experiencing a caching issue with Refit when calling Keycloak Admin API endpoints. Despite setting various no-cache headers and implementing a custom HttpMessageHandler, I'm still getting cached ...
Alptekin's user avatar
1 vote
0 answers
45 views

"ssl connection could not be established" when try to call API from local IIS

I try to call an azure hosted API from local IIS, but keep getting the error: ssl connection could not be established. authentication failed. The message received was unexpected or badly formatted ...
AGuyCalledGerald's user avatar
1 vote
0 answers
120 views

Right way to do HttpClient that can be injected and used in Blazor server

Currently, I have this configuration for http client that I can use and inject when needed, wrapped with JWT authentication to hit some API address that has multiple end points: builder.Services....
Prefetcher Shatnawi's user avatar
0 votes
0 answers
60 views

HttpClient fails to connect to specific HTTPS site in Docker container, works locally

I'm encountering an issue where HttpClient fails to establish an HTTPS connection to a specific site (https://www.csgt.vn) when running inside a Docker container. This is the minimal code: var client =...
Nam Pham's user avatar
3 votes
2 answers
197 views

HttpClient - Request times out but I don't understand why

I have put together an example code that shows what I mean. I am trying to download two different datasheets, one from TI, the other from ST. Both exist and I can download them using any browser (even ...
Tom L.'s user avatar
  • 1,044
2 votes
1 answer
260 views

Forcing C# HttpClient to resolve host via DNS every time

My use case is I have a series of services running via Docker Compose, each using the deploy.replicas attribute to get some n number of instances. services: myservice: image: ... ports: ...
joelc's user avatar
  • 2,841
2 votes
0 answers
44 views

UWP HttpClient GetAsync waits forever depending on computer

Why might this Windows.Web.Http.HttpClient instance work on one computer and not another? I've found these three posts that talk about why Windows.Web.Http.HttpClient.GetAsync might not ever return ...
Brian A. Henning's user avatar
4 votes
1 answer
163 views

Cannot replicate successful Postman request with HttpClient

I want to download a web page using HttpClient in .NET, but I receive a 403 Forbidden response, whereas using Postman with the same request parameters, I get a 200 OK. Here's my C# code: var client = ...
Paul Dubsky's user avatar
0 votes
1 answer
190 views

Avoid blocking on NSwag generated client

The code generated by NSwag allows the consumer to pass a CancellationToken, which it uses on the call to HttpClient.SendAsync, but it doesn't use on the downstream call to response.Content....
Andrew's user avatar
  • 241
2 votes
1 answer
130 views

How can I mitigate "Only 'http' and 'https' schemes are allowed." ArgumentException in System.Net.HttpClient.PutAsync?

I am using System.Net.HttpClient in a client application and needing to PUT to a third party service I have no control over. The third party service gives responses such as: Date: Wed, 04 Jun 2025 20:...
Cinder Biscuits's user avatar
0 votes
1 answer
96 views

How provide bearer token (or pass any parameter) at runtime when configuring IHttpClientFactory

We are using the new Keyed DI support in IHttpClientFactory in .NET 9, with different clients defined for each API endpoint. Defining static values, such as the base URL is straight forward, but we ...
Josh's user avatar
  • 8,551
0 votes
0 answers
74 views

Correct use of HttpClient for a VSTO addin

I recently updated and migrated my backend from SOAP WCF to a REST API (C#). This backend is utilized by an Outlook add-in (desktop version) to save documents, including emails and attachments, to a ...
Mihai Zotescu's user avatar
0 votes
2 answers
60 views

Get data received before HttpClient request is cancelled

I'm trying execute an HTTP request to receive some data (for example as a stream), but with ability to cancel this request at any time. I've read about CancellationToken and use it for cancel request (...
ibogolyubskiy's user avatar
0 votes
1 answer
204 views

Define custom headers using .NET 6 OpenAI SDK

Technical details Client applications are using .NET 6 Client applications are using the OpenAIClient class from the official OpenAI nuget library There is an inhouse cluster of LLM servers with ...
Mario's user avatar
  • 847
0 votes
2 answers
100 views

Add individual certificate to HttpClient created via HttpClientFactory

I do have a .NET 8.0 web application using HttpClient to send requests to different vendors. To get the HttpClient objects, I'm using the IHttpClientFactory interface installed via AddHttpClient ...
Peter's user avatar
  • 41
1 vote
0 answers
103 views

HttpClient cold start and idle timeout problem

ASP.NET Core 8 Web API service with can execute another service. I read this: https://www.aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/ https://byterot.blogspot.com/2016/07/singleton-...
merve şahinoglu's user avatar
0 votes
2 answers
100 views

Can we configure HttpClientHandler in IHttpClientFactory DI implentation?

I'm getting multiple intermittent 'Socket Exception' error while using httpclient(IHttpClientFactory DI Implementation) . As per MS documentation, A new HttpClient instance is returned each time ...
Nishad's user avatar
  • 35
0 votes
0 answers
63 views

ASP.Net Core 9 HttpClient.PostAsync fails to hit controller endpoint

HttpClient.PostAync does not reach controller endpoint at all. GetAsync and DeleteAsync are OK. Program.cs: builder.Services.AddControllersWithViews(); app.MapControllers(); Controller: [Route("...
khteh's user avatar
  • 4,280
-1 votes
2 answers
47 views

Dynamically setting the IHttpClientFactory timeout

What is the best way to dynamically change the timeout of an application if we are using IHttpClientFactory? I don't mean I want a different timeout for different addresses or endpoints - I mean all ...
Brad's user avatar
  • 1,423
0 votes
1 answer
98 views

.NET 7 HttpClient, HttpResponseMessage.Content.ReadAsStreamAsync - memory trouble

The environment is a .net (.net7.0) service, running in a Linux-based Docker container. It needs to communicate with an external REST API, download a large-ish zipped file, unzip it and upload to an ...
Yuri Makassiouk's user avatar
5 votes
0 answers
307 views

System.Net.Http.HttpRequestException: 'Received an invalid status line: 'HTTP/2 200 '.'

here is my code: using HttpClient client = new HttpClient(); try { HttpResponseMessage response = await client.GetAsync("http://131.189.89.86:11920/SetupWizard.aspx/yNMgLvRtUj"); } ...
Inside Man's user avatar
  • 4,219
0 votes
2 answers
154 views

Polly in C# is retrying the request, even though HttpClient isn't timing out and there are no errors

I have an API client that calls an API hosted in Kubernetes and using kong for gateway API, which is built using ASP.NET 6. We've also configured a Polly policy to retry the request if it receives an ...
Dhrup's user avatar
  • 89
3 votes
1 answer
117 views

Downloading a file from another website with Azure Functions

I am creating an Azure function that will download a file from a website and upload to the Blob store. I have the Azure function working locally but it does not work when deployed. I can't see ...
f_olivere's user avatar
0 votes
0 answers
36 views

Is there an alternative HttpClient handler for .NET Framework 4.5 that does not depend on Windows for HTTPS?

I have a legacy project running on .NET Framework 4.5, and we recently encountered issues because HttpClient relies on Windows for HTTPS connections. Is there an alternative HttpMessageHandler that ...
Max Bündchen's user avatar
1 vote
1 answer
65 views

Preventing existing polly policy from being invoked for certain results

I have a predefined IAsyncPolicy<HttpResponseMessage> policy coming from an external assembly. I want to apply this policy to a Http Client, BUT I want to exclude one of the cases this policy ...
Bogey's user avatar
  • 5,916
3 votes
1 answer
63 views

Reproduce Invoke-WebRequest behaviour in C# [duplicate]

I've exported a request from my Chrome browser using "Copy as PowerShell", trimmed it to a minimum and the script runs successfully: Invoke-WebRequest -Uri "https://www.sec.gov/data-...
vlad2048's user avatar
  • 191
1 vote
0 answers
81 views

Why is there a 15-second delay when using HttpClient to call a third-party API, but it works fine with curl? [duplicate]

I have deployed a feature in my production environment where I need to call a third-party API using HttpClient in .NET. However, I've noticed that there is a consistent 15-second delay when calling ...
Lan Lin's user avatar
  • 45
0 votes
0 answers
68 views

How to retrieve access token and use for subsequent requests?

I am creating an ASP.NET Core Web API that is intended to work as a middle ground between several third party API's and a first-party web application to sync data between the third parties and my main ...
Jon Sowers's user avatar
1 vote
2 answers
219 views

How to connect to kafka port and do integration testing by reading topic and message

I am trying to connect to Kafka port exposed by me in apphost in integrationtest.cs file after connecting to port, go and read all kafka topics and open a specific topic and read the messages in it ...
AbdulQASDET's user avatar
0 votes
0 answers
104 views

Creating an Imgur album with images in one request returns an empty album – What am I doing wrong?

I'm trying to use the Imgur API to create an album and have images attached in one request using my authenticated account. I first upload several images (which returns their image IDs), then I call my ...
n0degg's user avatar
  • 35
0 votes
0 answers
72 views

Create Teams meeting error though HttpClient

I'm creating an Application in C# to integrate with MS Teams. I was able to generate access token but getting error below when attempt to create a meeting using HttpClient() to Post to "{"...
Ph0ngvu's user avatar
  • 47

1
2 3 4 5
64