我正在开发一个Xamarin Forms应用程序,它基本上通过HttpClient使用GetAsync(Uri requestUri)函数:
HttpClient httpClient = new HttpClient(new NativeMessageHandler());
var uri = new Uri(http://www.appwebservice.meusite.com.br);
httpResponseMessage = await httpClient.GetAsync(uri);
此网址会自动重定向到另一个网址(如http://xxx.xx.xxx.xx:81/AppWebServiceBase/AppService.svc/json/WebServiceUrl所示), 但是这个重定向不是由应用程序完成的,而是由托管服务本身完成的, 假设我将链接http://www.appwebservice.meusite.com.br放入浏览器,它会自动重定向到网址http://xxx.xx.xxx.xx:81/AppWebServiceBase/AppService.svc/json/WebServiceUrl。
在UWP平台上,这可以正常工作,但是在Android和iOS平台上它会出错。
Android错误
--- 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.<init>(URL.java:192)
at java.net.URL.<init>(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错误:
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.Action`1[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.TaskAwaiter`1[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+<ServiceCall>d__0.MoveNext () [0x000f1] in C:\Workspace\OuvidoriaApp\OuvidoriaApp\OuvidoriaApp\Services\OuvidoriaWebService.cs:44
如果我把网址重定向,即把IP地址放到应用程序正常工作,问题似乎是在www地址......
当我禁用自动重定向时,httpResponseMessage会收到以下值:
ReasonPhrase: Moved Temporarily
StatusCode: 302 (System.Net.HttpStatusCode.Found)
IsSuccessStatusCode: false
Content:
<html>
<head><title>Object moved</title></head>
<body>
<h2>Object moved to <a href="http://xxx.xx.xxx.xx:81\AppWebServiceBase\AppService.svc\json\WebServiceUrl">here</a>.</h2>
</body>
and here messages to the user saying that an error has occurred
</html>
内容(http://xxx.xx.xxx.xx:81 \ AppWebServiceBase \ AppService.svc \ json \ WebServiceUrl)上的这个href是我需要的正确重定向。
注意:我更改了网址并进行了错误处理,我总结了错误所在的方式,我希望这一点很明显。