1

I am creating a NuGet package to retrieve images from an online API. I am using .NetStandard 1.4 and using System.Net.Http version 4.3.2. Every time I call the Login method to get a token to access the api, the http call never returns and just hangs till the task cancelation exception happens due to timing out. I have tried multiple solutions I have found online such as making sure there are no .Result or .Wait() before the async call and there are none in my test. I have also tried using an incorrect url and the call still hangs and doesn't return saying the url doesn't exist.

I have posted the main http call below. Any help on why this is hanging would be very much appreciated.

HttpRequestMessage requestMessage = new HttpRequestMessage();
requestMessage.RequestUri = new Uri($"{BaseUrl}/login");
requestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(HttpContentType.JSON));
requestMessage.Method = HttpMethod.Post;
requestMessage.Content = new StringContent(JsonConvert.SerializeObject(values), Encoding.UTF8, HttpContentType.JSON);

using (HttpClient httpClient = new HttpClient())
{
    using (HttpResponseMessage response = await httpClient.SendAsync(requestMessage))
    {
        if (response.IsSuccessStatusCode == true)
        {
            // Do work
        }
    }
}

The BaseUrl value and the path for the website is correct and I have checked that. Also I have used the same link and all the information I need to send in Postman in order to verify I am using the correct information.

I have tried to test this using MSTest, a console project, and a WPF project using a button and click event to fire off this method. Every method hangs and never gets passed the

using (HttpResponseMessage ...
CraftyCoder
  • 55
  • 1
  • 7
  • Did you check these- https://stackoverflow.com/questions/19704432/await-httpclient-sendasynchttpcontent-is-non-responsive, https://stackoverflow.com/questions/12933090/async-await-with-configureawaits-continueoncapturedcontext-parameter-and-synchr – Souvik Ghosh Aug 04 '17 at 03:56
  • Yeah I have already found those two posts and have tried the solutions with no luck... – CraftyCoder Aug 04 '17 at 03:59
  • Can you hit `http://example.com`? If no, the problem's probably not with `HttpClient`. It could be that the server is just not responding. – Stephen Cleary Aug 04 '17 at 09:33
  • I ran your test trying to hit [http://example.com](http://example.com) and it still hangs at the same spot. No change.. – CraftyCoder Aug 04 '17 at 13:08
  • How do you call the method? how do you know that the "work" is never executed? are you able to set up a new console app to reproduce this behaviour? – Martin Ullrich Aug 05 '17 at 10:37
  • I have tried to call this method three different ways. Once in a unit test calling this method async. Second in a console project. And third from a button click event in a WPF application. Last night I even created a whole new visual studio solution and recreated it again. I know the work is never executed because I set a breakpoint on the using (HttpResponseMessage response line and the if (response.IsSuccessCode line and that second breakpoint never gets hit and after the timeout, the call exceptions from a task canceled exception. – CraftyCoder Aug 05 '17 at 14:39

1 Answers1

0

I figured this out awhile back, but it happened to be a setting on my router. I am using an Apple Airport Extreme and the IPv6 setting was set to "Automatically". I changed it to "Link-local only" and all my HTTP calls started working correctly and were very responsive.

CraftyCoder
  • 55
  • 1
  • 7