Question

I have a small sample to test the HttpClient. The code is as below. The issue is that for a majority of the time, the request fails with a time out or host did not response on time. The same request works in the browser all the time. When it does, it takes a very long time.

Interestingly, accessing the same url via the browser always works without issues all the time. The same code being executed from another machine hitting the same page works as well.

What could be the cause of this problem when accessing localhost with HttpClient? How can it be fixed?

const string url = "http://localhost:12000/konnect/sync/adverts/status";
var handler = new HttpClientHandler();
handler.Proxy = null;
handler.UseProxy = false;
var client = new HttpClient(handler);
client.GetStringAsync(url).Result.Dump();
Was it helpful?

Solution

It turned out this had nothing to do with the library as generally requests that have to do with local host fails most of the time. It may be an issue with the system when it comes to local host. In any case, I disabled IPv6 and it still didn't work.

What worked however was rawcap. I realized that when I run rawCap to monitor 127.0.0.1, all calls were going through successfully. It appears something it did rectified the issue. Hope it helps someone who also had the same issue with localhost.

OTHER TIPS

Just a thought... Open the windows Lan Settings and uncheck "Automatically detect settings". I faced a similar situaltion recently and the problem was solved by diabling this setting.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top