Error on android and ios platforms, in the HttpClient.GetAsync(Uri RequestUri) function for redirect

I'm developing a Xamarin Forms application that basically uses the GetAsync (Uri requestUri) function through the HttpClient:

   HttpClient httpClient = new HttpClient(new NativeMessageHander());
   var uri = new Uri(htt p://www.appwebservice.meusite.com.br);
   httpResponseMessage = await httpClient.GetAsync(uri);

This url automatically redirects to another url with ip (something like that htt p://xxx.xx.xxx.xx:81/AppWebServiceBase/AppService.svc/json/WebServiceUrl), but this redirection isn't done by the app but by the hosting service itself, let's say if i put the link h ttp://www.appwebservice.meusite.com.br in the browser it will be automatically redirected to the url htt p://xxx.xx.xxx.xx:81/AppWebServiceBase/AppService.svc/json/WebServiceUrl.

On the UWP platform this works properly, however on Android and iOS platforms it gives error.

Android Error

--- End of managed Java.Net.MalformedURLException stack trace ---
java.net.MalformedURLException: java.lang.NumberFormatException: Invalid int: "81\AppWebServiceBase\AppService.svc\json\WebServiceUrl"
at java.net.URL.(URL.java:192)
at java.net.URL.(URL.java:139)
at com.squareup.okhttp.internal.http.HttpEngine.followUpRequest(HttpEngine.java:1009)
at com.squareup.okhttp.Call.getResponse(Call.java:285)
at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:228)
at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:199)
at com.squareup.okhttp.Call.access$100(Call.java:34)
at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:162)
at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)

iOS error:

Invalid URI: Invalid port specified.
at System.Uri.CreateThis (System.String uri, System.Boolean dontEscape, System.UriKind uriKind) [0x0007b] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.4/src/mono/mcs/class/referencesource/System/net/System/UriExt.cs:55
at System.Uri..ctor (System.String uriString) [0x00014] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.4/src/mono/mcs/class/referencesource/System/net/System/URI.cs:408
at ModernHttpClient.NativeMessageHandler+DataTaskDelegate.DidReceiveResponse (Foundation.NSUrlSession session, Foundation.NSUrlSessionDataTask dataTask, Foundation.NSUrlResponse response, System.Action1[T] completionHandler) [0x00106] in /Users/paul/code/paulcbetts/modernhttpclient/src/ModernHttpClient/iOS/NSUrlSessionHandler.cs:184 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.4/src/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:152 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00037] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.4/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.4/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.4/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 at System.Runtime.CompilerServices.TaskAwaiter1[TResult].GetResult () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.4/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:357
at OuvidoriaApp.Services.SaudeWebService+d__0.MoveNext () [0x000f1] in C:\Workspace\OuvidoriaApp\OuvidoriaApp\OuvidoriaApp\Services\OuvidoriaWebService.cs:44

If I put the url already redirected, ie put the address with IP the application works normally, the problem seems to be at www address ...

When I disable Auto Redirect the httpResponseMessage receives the following values on the Headers:

Values on Android:

Cache-Control: private
Location: htt p://xxx.xx.xxx.xx:81\AppWebServiceBase\AppService.svc\json\WebServiceUrl
Server: Microsoft-IIS/7.5
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
Date: Thu, 08 Mar 2018 14:38:01 GMT
X-Cache: MISS from localhost
X-Cache-Lookup: MISS from localhost:3128
Via: 1.1 localhost:3128 (squid/2.7.STABLE9)
Connection: keep-alive

Values on UWP:

Connection: keep-alive
Via: 1.1 localhost:3128 (squid/2.7.STABLE9)
Server: Microsoft-IIS/7.5
Location: h ttp://xxx.xx.xxx.xx:81\AppWebServiceBase\AppService.svc\json\WebServiceUrl
Cache-Control: private
X-Cache-Lookup: MISS from localhost:3128
X-AspNet-Version: 2.0.50727
Date: Thu, 08 Mar 2018 14:43:05 GMT
X-Powered-By: ASP.NET
X-Cache: MISS from localhost

Note: I changed the urls and made the error handling, I put in a summarized way where the error is located, I hope it was clear in the doubt,
I put space after the http for the question to be accepted.

Sign In or Register to comment.