为啥我在 Xamarin iOS 中的 WebRequest 在 ipv6 环境中失败?

Posted

技术标签:

【中文标题】为啥我在 Xamarin iOS 中的 WebRequest 在 ipv6 环境中失败?【英文标题】:Why is my WebRequest in Xamarin iOS failing in ipv6 environment?为什么我在 Xamarin iOS 中的 WebRequest 在 ipv6 环境中失败? 【发布时间】:2016-11-09 08:40:04 【问题描述】:

我在https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/NetworkingOverview/UnderstandingandPreparingfortheIPv6Transition/UnderstandingandPreparingfortheIPv6Transition.html#//apple_ref/doc/uid/TP40010220-CH213-SW1之后设置了一个本地ipv6环境

我有一个非常简单的 ios Xamarin 项目,除了:

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();

在第 2 行,我得到以下异常。如果有人知道这应该如何表现或如何使其运行,那就太好了。我想传递 ipv4 地址仍然是一个常见的用例。

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】:

您不能使用 IPv4 地址文字。这是自动失败和应用程序拒绝,因为这些不适用于纯 IPv6 环境。您必须改用主机名。

【讨论】:

具有原生开发的同一个应用程序工作并且没有被拒绝。我认为只要你使用像 NSUrlSession 这样的高级框架就可以了。对我来说,它看起来像是 Xamarin 中的一个错误。 (HttpClient 有效,因为您可以注入 NSURLSession,而 WebRequest 不能)如果您确定任何直接 ip4 调用会导致应用拒绝,您能否提及该信息的来源?【参考方案2】:

Apple 似乎在纯 ipv6 环境中进行测试,因此任何直接调用都会失败。这是他们昨天在我的应用程序失败时的评论:

请在连接到 IPv6 网络的设备上运行您的应用 (所有应用程序必须支持 IPv6)以识别问题,然后修改和 重新提交您的应用以供审核。

如果我们误解了您应用的预期行为,请回复 解决中心中的此消息,以提供有关如何解决这些问题的信息 功能旨在发挥作用。

对于新应用,请从 设备,然后安装并按照步骤重现问题。 对于更新,安装新版本作为对先前版本的更新 版本,然后按照步骤重现问题。

资源

有关支持 IPv6 网络的信息,请参阅 Supporting IPv6 DNS64/NAT64 Networks and About Networking.

【讨论】:

在迈克尔汉普顿的回答中查看我上面的评论

以上是关于为啥我在 Xamarin iOS 中的 WebRequest 在 ipv6 环境中失败?的主要内容,如果未能解决你的问题,请参考以下文章

为啥滚动这个 xamarin 页面这么慢?

Xamarin.iOS 为啥在设备上调试时 HKAnchoredObjectQuery 处于非活动状态然后停用?

Xamarin.iOS 为啥在设备上调试时 HKAnchoredObjectQuery 处于非活动状态然后停用?

为啥 xamarin 突然不让我部署到 iOS 设备

Xamarin iOS:滚动视图中的自动布局?

为啥我在 Xamarin 中使用 ContentProvider 插入项目后数据库为空?