0

I have a Windows 8.1 application running on a Windows 10 Enterprise tablet. This app is calling an ASP.NET RESTful Web API running on a remote server. The Web API is using Windows Authentication. I have Fiddler installed on the tablet and I can make calls into the Web API so that proves the Web API is available to the outside world.

Here is the code I use to make the call:

if (WebApiAuthClientHandler == null)
{
   WebApiAuthClientHandler = new HttpClientHandler()
   {
      Credentials = CredentialCache.DefaultNetworkCredentials,
      ClientCertificateOptions = ClientCertificateOption.Automatic
   };
}

if (WebApiHttpClient == null)
{
   WebApiHttpClient = new HttpClient(WebApiAuthClientHandler)
   {
      BaseAddress = new Uri(_serviceUri, UriKind.Absolute),
      Timeout = new TimeSpan(0, 0, 150)
   };

   WebApiHttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}

HttpResponseMessage response = await WebApiHttpClient.GetAsync("api/user", HttpCompletionOption.ResponseContentRead).ConfigureAwait(continueOnCapturedContext: false);

var userLoginInfo = await response.Content.ReadAsAsync<UserLoginInformation>();

where WebApiAuthClientHandler and WebApiHttpClient are my class' static members.

The _serviceUri is constructed from the app's settings and results in a nicely formatted absolute URI like "http://10.120.4.0:5000". Like I said, if I issue a GET HTTP command with Fiddler to the URL "http://10.120.4.0:5000/api/user" I get the expected response on my tablet.

The problem is that my GetAsync call throws an exception after about 21 seconds. It is not timeout, because, as you can see, I set the timeout to 150 seconds (I know it's too much, but for now I was just trying to eliminate all potential causes). The exception thrown is of type HttpRequestException and the several nested messages inform me that I was "Unable to connect to the remote server" because "A Connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 127.0.0.1:8888"

This last part really puzzles me because I do not try to access the Web API locally but remotely, having passed the proper remote URL address.

This issue it's been bugging me for close to two weeks and I still have no resolution to it. Any suggestion would be highly appreciated.

TIA, Eddie

Eddie
  • 271
  • 4
  • 18
  • Forgot to mention that in my app's package manifest I have both Internet (Client & Server) and Private Networks (Client & Server) capabilities checked, in addition to the Webcam, which I also need to use to take pictures and scan some barcodes. – Eddie Sep 01 '16 at 17:45
  • 1
    That port number looks like your app is trying to communicate via Fiddler. Is Fiddler running? Have you run the AppContainer loopback utility to see if your app is configured? – Richard Deeming Sep 01 '16 at 18:08
  • Yes Richard, you are right, Fiddler was running on the tablet at the time. I was trying to capture traffic to the remote server, to see if my app makes any attempt to communicate with the Web API. I ran it w/o Fiddler and now the exception is thrown right away (not after 21 seconds). The exception is of type UnsupportedMediaTypeException and it tells me that "No MediaTypeFormatter is available to read on object of type 'UserLoginInformation' from content with media type 'text/html'". Looks like the server returns XML instead of the JSON that I am expecting. Need to look into it. Thanks, Eddie – Eddie Sep 01 '16 at 18:23
  • This UnsupportedMediaTypeException is really weird. First of all, the Web API si configured to return JSON when the Content-Type is set to "application/json". The second part is that I still don't make it into the server with my call. I have a breakpoint in the Web API method. I hit that breakpoint if I use Fiddler, but I do not hit it when I call from the app. – Eddie Sep 01 '16 at 18:40
  • 1
    It sounds like there's either something else sitting between your app and the service, or the call from the app is invalid somehow and you're getting an error HTML page. Try inspecting the `response.Content` before you try to deserialize it, to see if that gives you any clues. – Richard Deeming Sep 01 '16 at 18:46
  • This app works when I run it with the simulator, on my development machine. I am thinking that the HttpClient call is built correctly. I will try to trace fields of the response.Content, to see if they give me extra information. Thanks again for helping out. – Eddie Sep 01 '16 at 18:58
  • You were right again, Richard. It's not JSON that comes back, but HTML. It's HTTP Error 401.1 - Unauthorized message, informing me that "You do not have permission to view this directory or page using the credentials that you supplied". I don't know how this can be since (1) the Web API is configured to use Windows Authentication, (2) Fiddler is successful in getting the response from the Web API and (3) the app uses Windows Authentication when ran in simulation mode. – Eddie Sep 01 '16 at 19:44
  • One thing I noticed in Fiddler is that the first call generates an HTTP Error 401.2 - Unauthorized rather than the 401.1 I get in my app. Fiddler continues the NTLM Negotiation and the subsequent call returns HTTP 200 Ok. My app is not doing the negotiation even though I would expect it to do it, given the way I construct the header of the request. – Eddie Sep 01 '16 at 19:49
  • The first comment by Richard should be marked as answer. After I stopped running Fiddler on the tablet, I did not get that same error but a different one. To prevent confusion, I think I am going to post a new question in a new context. – Eddie Sep 01 '16 at 21:02

0 Answers0