476rick 476rick - 1 month ago 19
HTTP Question

Xamarin UWP HttpRequestException when using non-localhost (PCL)

I'm building an application with Xamarin.Forms and a Portable Class Library.
I try to do an API call which returns JSON. I have problems when running the project on Universal Windows Platform.
This is the code that throws an error:

using (HttpClient wc = new HttpClient())
{
var data = await wc.GetStringAsync("http://something.e1923.companyname.local/content-api/Category/?format=json");


This is the error:


Exception thrown: 'System.Net.Http.HttpRequestException' in mscorlib.ni.dll
at System.Net.Http.HttpClientHandler.d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Somethoing.RESTConnection.d__4.MoveNext()


When I used the same code but an other URL it worked perfect:

wc.GetStringAsync("http://something.localhost.companyname.local/content-api/Category/?format=json");


The code does work on Android, but not in Universal Windows Platform.
Why is the above code (without localhost) not working on UWP?

I only changed from localhost to an other URL. The only requirement to have access to the non-localhost URL (in my particular case) is that you are connected to the right network (same as where the website is hosted).

Answer

Krumeler asked me in the comments if I did add the required capabilities in my UWP app, so I wen't looking.

The one I didn't check was: Private Networks (Client & Server).

This was the one that needed to be checked. It works now, thanks!