Why is my WebRequest in Xamarin iOS failing in ipv

2019-03-02 02:12发布

问题:

I setup a local ipv6 environment following https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/NetworkingOverview/UnderstandingandPreparingfortheIPv6Transition/UnderstandingandPreparingfortheIPv6Transition.html#//apple_ref/doc/uid/TP40010220-CH213-SW1

I have a very simple iOS Xamarin project where i do nothing but:

WebRequest req = WebRequest.Create("http://109.68.230.138");
WebResponse response = req.GetResponse(); //exception
var dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();

At line 2 i get the following exception. Would be great if anyone knows how this should behave or how to get this running. I guess passing ipv4 addresses is still a commom use case.

Unhandled Exception:
System.Net.WebException: Error: ConnectFailure (Network is unreachable) ---> System.Net.Sockets.SocketException: Network is unreachable
  at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x000cb] in /Users/builder/data/lanes/3859/b638977c/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System/System.Net.Sockets/Socket.cs:1313 
  at System.Net.WebConnection.Connect (System.Net.HttpWebRequest request) [0x001c2] in /Users/builder/data/lanes/3859/b638977c/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System/System.Net/WebConnection.cs:195 
   --- End of inner exception stack trace ---
  at System.Net.HttpWebRequest.EndGetResponse (System.IAsyncResult asyncResult) [0x00065] in /Users/builder/data/lanes/3859/b638977c/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System/System.Net/HttpWebRequest.cs:1025 
  at System.Net.HttpWebRequest.GetResponse () [0x0000e] in /Users/builder/data/lanes/3859/b638977c/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System/System.Net/HttpWebRequest.cs:1037 
  at Test.iOS.AppDelegate.FinishedLaunching (UIKit.UIApplication application, Foundation.NSDictionary launchOptions) [0x0000d] in /Users/xxx/Projects/Test/iOS/AppDelegate.cs:26 
  at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Users/builder/data/lanes/3859/b638977c/source/xamarin-macios/src/UIKit/UIApplication.cs:79 
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/3859/b638977c/source/xamarin-macios/src/UIKit/UIApplication.cs:63 
  at Test.iOS.Application.Main (System.String[] args) [0x00008] in /Users/xxx/Projects/Test/iOS/Main.cs:17 
2016-11-09 09:34:32.194 Test.iOS[763:313310] Unhandled managed exception:
Error: ConnectFailure (Network is unreachable) (System.Net.WebException)
  at System.Net.HttpWebRequest.EndGetResponse (System.IAsyncResult asyncResult) [0x00065] in /Users/builder/data/lanes/3859/b638977c/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System/System.Net/HttpWebRequest.cs:1025 
  at System.Net.HttpWebRequest.GetResponse () [0x0000e] in /Users/builder/data/lanes/3859/b638977c/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System/System.Net/HttpWebRequest.cs:1037 
  at Test.iOS.AppDelegate.FinishedLaunching (UIKit.UIApplication application, Foundation.NSDictionary launchOptions) [0x0000d] in /Users/xxx/Projects/Test/iOS/AppDelegate.cs:26 
  at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Users/builder/data/lanes/3859/b638977c/source/xamarin-macios/src/UIKit/UIApplication.cs:79 
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/3859/b638977c/source/xamarin-macios/src/UIKit/UIApplication.cs:63 
  at Test.iOS.Application.Main (System.String[] args) [0x00008] in /Users/xxx/Projects/Test/iOS/Main.cs:17 
 --- inner exception ---
Network is unreachable (System.Net.Sockets.SocketException)
  at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x000cb] in /Users/builder/data/lanes/3859/b638977c/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System/System.Net.Sockets/Socket.cs:1313 
  at System.Net.WebConnection.Connect (System.Net.HttpWebRequest request) [0x001c2] in /Users/builder/data/lanes/3859/b638977c/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System/System.Net/WebConnection.cs:195 
2016-11-09 09:34:32.195 Test.iOS[763:313310] critical: Stacktrace:

2016-11-09 09:34:32.195 Test.iOS[763:313310] critical: 
Native stacktrace:

2016-11-09 09:34:32.265 Test.iOS[763:313310] critical:  0   libmonosgen-2.0.dylib               0x00000001001abd0c mono_handle_native_sigsegv + 260
2016-11-09 09:34:32.266 Test.iOS[763:313310] critical:  1   libsystem_platform.dylib            0x0000000192eec348 _sigtramp + 52
2016-11-09 09:34:32.266 Test.iOS[763:313310] critical:  2   libsystem_pthread.dylib             0x0000000192ef2450 pthread_kill + 112
2016-11-09 09:34:32.266 Test.iOS[763:313310] critical:  3   libsystem_c.dylib                   0x0000000192d9e3e0 abort + 140
2016-11-09 09:34:32.267 Test.iOS[763:313310] critical:  4   libxamarin-debug.dylib              0x0000000100480d90 _ZL20print_all_exceptionsP10MonoObject + 0
2016-11-09 09:34:32.267 Test.iOS[763:313310] critical:  5   libmonosgen-2.0.dylib               0x00000001001ac59c mono_invoke_unhandled_exception_hook + 100
2016-11-09 09:34:32.267 Test.iOS[763:313310] critical:  6   libmonosgen-2.0.dylib               0x00000001001ab830 mono_handle_exception_internal + 3860
2016-11-09 09:34:32.268 Test.iOS[763:313310] critical:  7   libmonosgen-2.0.dylib               0x00000001001aa910 mono_handle_exception + 40
2016-11-09 09:34:32.268 Test.iOS[763:313310] critical:  8   libmonosgen-2.0.dylib               0x00000001001a1be4 mono_arm_throw_exception + 216
2016-11-09 09:34:32.268 Test.iOS[763:313310] critical:  9   libmscorlib.dll.dylib               0x00000001007a9918 throw_exception + 168
2016-11-09 09:34:32.268 Test.iOS[763:313310] critical:  10  libSystem.dll.dylib                 0x0000000100bbac44 System_Net_HttpWebRequest_EndGetResponse_System_IAsyncResult + 388
2016-11-09 09:34:32.269 Test.iOS[763:313310] critical:  11  libSystem.dll.dylib                 0x0000000100bbacc0 System_Net_HttpWebRequest_GetResponse + 112
2016-11-09 09:34:32.269 Test.iOS[763:313310] critical:  12  libTest.iOS.exe.dylib               0x00000001000770b4 Test_iOS_AppDelegate_FinishedLaunching_UIKit_UIApplication_Foundation_NSDictionary + 276
2016-11-09 09:34:32.269 Test.iOS[763:313310] critical:  13  libmscorlib.dll.dylib               0x0000000100786db4 wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 244
2016-11-09 09:34:32.269 Test.iOS[763:313310] critical:  14  libmonosgen-2.0.dylib               0x00000001001ba1dc mono_jit_runtime_invoke + 1772
2016-11-09 09:34:32.270 Test.iOS[763:313310] critical:  15  libmonosgen-2.0.dylib               0x000000010022ad0c do_runtime_invoke + 112
2016-11-09 09:34:32.270 Test.iOS[763:313310] critical:  16  libmonosgen-2.0.dylib               0x000000010022ac68 mono_runtime_invoke + 208
2016-11-09 09:34:32.270 Test.iOS[763:313310] critical:  17  Test.iOS                            0x000000010005c62c _ZL30native_to_managed_trampoline_5P11objc_objectP13objc_selectorPP11_MonoMethodP13UIApplicationP12NSDictionaryPKcSB_SB_SB_ + 692
2016-11-09 09:34:32.270 Test.iOS[763:313310] critical:  18  Test.iOS                            0x000000010005c368 -[AppDelegate application:didFinishLaunchingWithOptions:] + 96
2016-11-09 09:34:32.271 Test.iOS[763:313310] critical:  19  UIKit                               0x0000000199d20100 <redacted> + 400
2016-11-09 09:34:32.271 Test.iOS[763:313310] critical:  20  UIKit                               0x0000000199f30858 <redacted> + 3524
2016-11-09 09:34:32.271 Test.iOS[763:313310] critical:  21  UIKit                               0x0000000199f365c8 <redacted> + 1656
2016-11-09 09:34:32.271 Test.iOS[763:313310] critical:  22  UIKit                               0x0000000199f4ae60 <redacted> + 48
2016-11-09 09:34:32.272 Test.iOS[763:313310] critical:  23  UIKit                               0x0000000199f335ac <redacted> + 168
2016-11-09 09:34:32.272 Test.iOS[763:313310] critical:  24  FrontBoardServices                  0x0000000195a018bc <redacted> + 36
2016-11-09 09:34:32.272 Test.iOS[763:313310] critical:  25  FrontBoardServices                  0x0000000195a01728 <redacted> + 176
2016-11-09 09:34:32.272 Test.iOS[763:313310] critical:  26  FrontBoardServices                  0x0000000195a01ad0 <redacted> + 56
2016-11-09 09:34:32.273 Test.iOS[763:313310] critical:  27  CoreFoundation                      0x0000000193e08278 <redacted> + 24
2016-11-09 09:34:32.273 Test.iOS[763:313310] critical:  28  CoreFoundation                      0x0000000193e07bc0 <redacted> + 524
2016-11-09 09:34:32.273 Test.iOS[763:313310] critical:  29  CoreFoundation                      0x0000000193e057c0 <redacted> + 804
2016-11-09 09:34:32.273 Test.iOS[763:313310] critical:  30  CoreFoundation                      0x0000000193d34048 CFRunLoopRunSpecific + 444
2016-11-09 09:34:32.274 Test.iOS[763:313310] critical:  31  UIKit                               0x0000000199d192b0 <redacted> + 608
2016-11-09 09:34:32.274 Test.iOS[763:313310] critical:  32  UIKit                               0x0000000199d14034 UIApplicationMain + 208
2016-11-09 09:34:32.274 Test.iOS[763:313310] critical:  33  libXamarin.iOS.dll.dylib            0x0000000100514fb4 wrapper_managed_to_native_UIKit_UIApplication_UIApplicationMain_int_string___intptr_intptr + 324
2016-11-09 09:34:32.274 Test.iOS[763:313310] critical:  34  libXamarin.iOS.dll.dylib            0x00000001004d61ac UIKit_UIApplication_Main_string___intptr_intptr + 44
2016-11-09 09:34:32.275 Test.iOS[763:313310] critical:  35  libXamarin.iOS.dll.dylib            0x00000001004d616c UIKit_UIApplication_Main_string___string_string + 172
2016-11-09 09:34:32.275 Test.iOS[763:313310] critical:  36  libTest.iOS.exe.dylib               0x0000000100076de0 Test_iOS_Application_Main_string__ + 160
2016-11-09 09:34:32.275 Test.iOS[763:313310] critical:  37  libmscorlib.dll.dylib               0x0000000100786db4 wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 244
2016-11-09 09:34:32.275 Test.iOS[763:313310] critical:  38  libmonosgen-2.0.dylib               0x00000001001ba1dc mono_jit_runtime_invoke + 1772
2016-11-09 09:34:32.276 Test.iOS[763:313310] critical:  39  libmonosgen-2.0.dylib               0x000000010022ad0c do_runtime_invoke + 112
2016-11-09 09:34:32.276 Test.iOS[763:313310] critical:  40  libmonosgen-2.0.dylib               0x000000010022d2f0 mono_runtime_exec_main + 832
2016-11-09 09:34:32.276 Test.iOS[763:313310] critical:  41  libmonosgen-2.0.dylib               0x000000010022cf0c mono_runtime_run_main + 764
2016-11-09 09:34:32.276 Test.iOS[763:313310] critical:  42  libmonosgen-2.0.dylib               0x000000010019e7f8 mono_jit_exec + 236
2016-11-09 09:34:32.277 Test.iOS[763:313310] critical:  43  libxamarin-debug.dylib              0x000000010048dcd8 xamarin_main + 2168
2016-11-09 09:34:32.277 Test.iOS[763:313310] critical:  44  Test.iOS                            0x000000010005de50 main + 96
2016-11-09 09:34:32.277 Test.iOS[763:313310] critical:  45  libdyld.dylib                       0x0000000192d185b8 <redacted> + 4
2016-11-09 09:34:32.277 Test.iOS[763:313310] critical: 
=================================================================
Got a SIGABRT while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

回答1:

You can't use IPv4 address literals. That's an automatic failure and app rejection, since these don't work on an IPv6-only environment. You must use a hostname instead.



回答2:

Apple seems to be testing in pure ipv6 environments, so any direct call will fail. This is their comment when they failed my app yesterday:

Please run your app on a device while connected to an IPv6 network (all apps must support IPv6) to identify the issue(s), then revise and resubmit your app for review.

If we misunderstood the intended behavior of your app, please reply to this message in Resolution Center to provide information on how these features were intended to work.

For new apps, uninstall all previous versions of your app from a device, then install and follow the steps to reproduce the issue(s). For updates, install the new version as an update to the previous version, then follow the steps to reproduce the issue(s).

Resources

For information about supporting IPv6 Networks, please refer to Supporting IPv6 DNS64/NAT64 Networks and About Networking.