xamarin / xamarin-macios

.NET for iOS, Mac Catalyst, macOS, and tvOS provide open-source bindings of the Apple SDKs for use with .NET managed languages such as C#
Other
2.46k stars 511 forks source link

HttpClient.PostAsync throws WebException instead of HttpRequestException if a device is offline #6439

Closed kwintAl closed 5 years ago

kwintAl commented 5 years ago

We have a netstandard2.0 library for our REST API and authentication flow.

HttpClient.PostAsync lists only two exceptions:

Exceptions ArgumentNullException The requestUri is null.

HttpRequestException The request failed due to an underlying issue such as network connectivity, DNS failure, server certificate validation or timeout.

I expect to get an HttpRequestException if a device is not connected to the internet. But I get a WebException.

Exception Log

Application Specific Information:
*** Terminating app due to uncaught exception 'System.Net.WebException', reason: 'System.Net.WebException: The Internet connection appears to be offline. ---> Foundation.NSErrorException: Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo={_kCFStreamErrorCodeKey=50, NSUnderlyingError=0x280823f00 {Error Domain=kCFErrorDomainCFNetwork Code=-1009 "(null)" UserInfo={_kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <CFC20CAE-EF8A-4DD1-9212-A9B610C830C7>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=('    

"LocalDataTask <CFC20CAE-EF8A-4DD1-9212-A9B610C830C7>.<1>"
), NSLocalizedDescription=The Internet connection appears to be offline., NSErrorFailingURLStringKey=https://..., NSErrorFailingURLKey=https://... _kCFStreamErrorDomainKey=1}
   --- End of inner exception stack trace ---
  at System.Net.Http.NSUrlSessionHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) <0x105b195c0 + 0x008c7> in <60cab046a6f74db6b96c69ed467017c2#230e9475fa646760043dd17312e02a69>:0 
  at System.Net.Http.HttpClient.SendAsyncWorker (System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken) <0x105b147d0 + 0x003cb> in <60cab046a6f74db6b96c69ed467017c2#230e9475fa646760043dd17312e02a69>:0 

Steps to Reproduce

  1. Go offline (airplane mode)
  2. Make a post request

            try {
                var response = await client.PostAsync("https://...",
                    new FormUrlEncodedContent(dict), ct);
                // ...
    
            } catch (HttpRequestException)
            {
                // ...
            }

Expected Behavior

HttpRequestException

Actual Behavior

WebException

Environment

=== Visual Studio Community 2019 for Mac ===

Version 8.1.2 (build 2)
Installation UUID: 0b379bc9-f2fb-4ff2-963d-d41a3016674b
    GTK+ 2.24.23 (Raleigh theme)
    Xamarin.Mac 5.6.0.25 (d16-0 / 50f75273)

    Package version: 518010028

=== Mono Framework MDK ===

Runtime:
    Mono 5.18.1.28 (2018-08/223ea7ef92e) (64-bit)
    Package version: 518010028

=== NuGet ===

Version: 5.0.2.5988

=== .NET Core ===

Laufzeit: /usr/local/share/dotnet/dotnet
Laufzeitversionen:
    2.2.4
    2.2.3
    2.2.0
    2.1.9
    2.1.3
    2.0.5
    2.0.0
SDK: /usr/local/share/dotnet/sdk/2.2.105/Sdks
SDK-Versionen:
    2.2.105
    2.2.101
    2.1.505
    2.1.401
    2.1.4
    2.0.2
MSBuild-SDKs: /Library/Frameworks/Mono.framework/Versions/5.18.1/lib/mono/msbuild/Current/bin/Sdks

=== Xamarin.Profiler ===

Version: 1.6.10
Speicherort: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Updater ===

Version: 11

=== Apple Developer Tools ===

Xcode 10.2.1 (14490.122)
Build 10E1001

=== Xamarin.Mac ===

Version: 5.10.0.157 (Visual Studio Community)
Hash: 6bd94753
Branch: d16-1
Build date: 2019-06-12 17:28:48-0400

=== Xamarin.iOS ===

Version: 12.10.0.157 (Visual Studio Community)
Hash: 6bd94753
Branch: d16-1
Build date: 2019-06-12 17:28:47-0400

=== Xamarin Designer ===

Version: 16.1.0.467
Hash: f1657e133
Branch: remotes/origin/d16-1-new-document-model
Build date: 2019-06-18 21:57:42 UTC

=== Xamarin.Android ===

Version: 9.3.0.23 (Visual Studio Community)
Commit:HEAD/d0b48056f
Android SDK: /Users/kwint/Library/Android/sdk
    Unterstützte Android-Versionen:
        5.1 (API-Ebene 22)
        7.0 (API-Ebene 24)
        7.1 (API-Ebene 25)
        8.1 (API-Ebene 27)

Version von SDK Tools: 26.1.1
Version der SDK-Plattformtools: 28.0.2
Version der SDK-Buildtools: 29.0.0 rc1

Buildinformationen: 
Mono: mono/mono/2018-08@3a07bd426d3
Java.Interop: xamarin/java.interop/d16-1@5ddc3e3
LibZipSharp: grendello/LibZipSharp/d16-1@44de300
LibZip: nih-at/libzip/rel-1-5-1@b95cf3f
ProGuard: xamarin/proguard/master@905836d
SQLite: xamarin/sqlite/3.27.1@8212a2d
Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-1@acabd26

=== Microsoft Mobile OpenJDK ===

Java SDK: /Users/kwint/Library/Developer/Xamarin/jdk/microsoft_dist_openjdk_8.0.25
1.8.0-25
Android Designer EPL-Code ist hier verfügbar:
https://github.com/xamarin/AndroidDesigner.EPL

=== Android Device Manager ===

Version: 1.2.0.44
Hash: aac645b
Branch: remotes/origin/d16-1
Build date: 2019-05-29 19:55:24 UTC

=== Xamarin Inspector ===

Version: 1.4.3
Hash: db27525
Branch: 1.4-release
Build date: Mon, 09 Jul 2018 21:20:18 GMT
Client compatibility: 1

=== Build Information ===

Release ID: 801020002
Git revision: 5d9e30e11e16dbb655977e4f7051ffedd3de9f94
Build date: 2019-06-24 20:05:44+00
Build branch: release-8.1
Xamarin extensions: 5104b76623e0df8e5f763f87d170279c0b788e89

=== Operating System ===

Mac OS X 10.14.5
Darwin 18.6.0 Darwin Kernel Version 18.6.0
    Thu Apr 25 23:16:27 PDT 2019
    root:xnu-4903.261.4~2/RELEASE_X86_64 x86_64
spouliot commented 5 years ago

stack trace (for NSUrlSessionHandler)

2019-07-02 10:58:08.352 gh6439[18579:15880723] System.Net.WebException: The Internet connection appears to be offline. ---> Foundation.NSErrorException: Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo={_kCFStreamErrorCodeKey=50, NSUnderlyingError=0x283b6d350 {Error Domain=kCFErrorDomainCFNetwork Code=-1009 "(null)" UserInfo={_kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <55894EBD-B898-4803-9981-46317EEFE280>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <55894EBD-B898-4803-9981-46317EEFE280>.<1>"
), NSLocalizedDescription=The Internet connection appears to be offline., NSErrorFailingURLStringKey=https://www.microsoft.com/fr-ca/, NSErrorFailingURLKey=https://www.microsoft.com/fr-ca/, _kCFStreamErrorDomainKey=1}
   --- End of inner exception stack trace ---
  at System.Net.Http.NSUrlSessionHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x001d4] in /Users/poupou/git/xcode11/xamarin-macios/src/Foundation/NSUrlSessionHandler.cs:541 
  at System.Net.Http.HttpClient.SendAsyncWorker (System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken) [0x0009e] in /Users/poupou/git/xcode11/xamarin-macios/external/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClient.cs:281 
spouliot commented 5 years ago

stack trace (for CFNetworkHandler)

2019-07-02 11:04:35.407 gh6439[18580:15881497] CoreFoundation.CFException: The operation couldn’t be completed. Network is down
  at System.Net.Http.CFNetworkHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken, System.Boolean isFirstRequest) [0x002f2] in /Users/poupou/git/xcode11/xamarin-macios/src/System.Net.Http/CFNetworkHandler.cs:266 
  at System.Net.Http.CFNetworkHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x00034] in /Users/poupou/git/xcode11/xamarin-macios/src/System.Net.Http/CFNetworkHandler.cs:199 
  at System.Net.Http.HttpClient.SendAsyncWorker (System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken) [0x0009e] in /Users/poupou/git/xcode11/xamarin-macios/external/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClient.cs:281 
spouliot commented 5 years ago

stack trace (for HttpClientHandler)

2019-07-02 11:06:53.632 gh6439[18583:15882519] System.Net.Http.HttpRequestException: An error occurred while sending the request ---> System.Net.WebException: Error: NameResolutionFailure
  at System.Net.WebConnection.Connect (System.Net.WebOperation operation, System.Threading.CancellationToken cancellationToken) [0x00050] in /Users/poupou/git/xcode11/xamarin-macios/external/mono/mcs/class/System/System.Net/WebConnection.cs:137 
  at System.Net.WebConnection.InitConnection (System.Net.WebOperation operation, System.Threading.CancellationToken cancellationToken) [0x000cc] in /Users/poupou/git/xcode11/xamarin-macios/external/mono/mcs/class/System/System.Net/WebConnection.cs:259 
  at System.Net.WebOperation.Run () [0x00052] in /Users/poupou/git/xcode11/xamarin-macios/external/mono/mcs/class/System/System.Net/WebOperation.cs:268 
  at System.Net.WebCompletionSource`1[T].WaitForCompletion () [0x00094] in <ff8a47e1bc5b4604beec4845f32111dd>:0 
  at System.Net.HttpWebRequest.RunWithTimeoutWorker[T] (System.Threading.Tasks.Task`1[TResult] workerTask, System.Int32 timeout, System.Action abort, System.Func`1[TResult] aborted, System.Threading.CancellationTokenSource cts) [0x000f8] in <ff8a47e1bc5b4604beec4845f32111dd>:0 
  at System.Net.Http.MonoWebRequestHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x0029b] in /Users/poupou/git/xcode11/xamarin-macios/external/mono/mcs/class/System.Net.Http/MonoWebRequestHandler.cs:489 
   --- End of inner exception stack trace ---
  at System.Net.Http.MonoWebRequestHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x0046a] in /Users/poupou/git/xcode11/xamarin-macios/external/mono/mcs/class/System.Net.Http/MonoWebRequestHandler.cs:503 
  at System.Net.Http.HttpClient.SendAsyncWorker (System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken) [0x0009e] in /Users/poupou/git/xcode11/xamarin-macios/external/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClient.cs:281 
spouliot commented 5 years ago

with above PR the stack traces now looks like

NSUrlSessionHandler

2019-07-02 13:48:06.764 gh6439[18937:15939539] System.Net.Http.HttpRequestException: The Internet connection appears to be offline. ---> Foundation.NSErrorException: Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo={_kCFStreamErrorCodeKey=50, NSUnderlyingError=0x283f8bdb0 {Error Domain=kCFErrorDomainCFNetwork Code=-1009 "(null)" UserInfo={_kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <EF1E824F-6D33-467B-84A6-7E4901AB6470>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <EF1E824F-6D33-467B-84A6-7E4901AB6470>.<1>"
), NSLocalizedDescription=The Internet connection appears to be offline., NSErrorFailingURLStringKey=https://www.microsoft.com/fr-ca/, NSErrorFailingURLKey=https://www.microsoft.com/fr-ca/, _kCFStreamErrorDomainKey=1}
   --- End of inner exception stack trace ---
  at System.Net.Http.NSUrlSessionHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x001d4] in /Users/poupou/git/master/xamarin-macios/src/Foundation/NSUrlSessionHandler.cs:432 
  at System.Net.Http.HttpClient.SendAsyncWorker (System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken) [0x0009e] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/System.Net.Http/System.Net.Http/HttpClient.cs:281 

CFNetworkHandler

2019-07-02 13:52:15.616 gh6439[18939:15940082] System.Net.Http.HttpRequestException: Network is down ---> CoreFoundation.CFException: The operation couldn’t be completed. Network is down
   --- End of inner exception stack trace ---
  at System.Net.Http.CFNetworkHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken, System.Boolean isFirstRequest) [0x002f2] in /Users/poupou/git/master/xamarin-macios/src/System.Net.Http/CFNetworkHandler.cs:266 
  at System.Net.Http.CFNetworkHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x00034] in /Users/poupou/git/master/xamarin-macios/src/System.Net.Http/CFNetworkHandler.cs:199 
  at System.Net.Http.HttpClient.SendAsyncWorker (System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken) [0x0009e] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/System.Net.Http/System.Net.Http/HttpClient.cs:281 
divil5000 commented 3 years ago

This appears to still be an issue, using the latest version of Xamarin. Are you aware of that?

divil5000 commented 3 years ago

This is a stack trace from a WebException thrown when attempting to connect to a non-existent local end point:

{System.Net.WebException: Failed to connect to localhost/127.0.0.1:44399 ---> Java.Net.ConnectException: Failed to connect to localhost/127.0.0.1:44399 at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue args) [0x0006e] in <24e422c426e0468ca1fd74b59870ff08>:0 at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeAbstractVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue parameters) [0x00014] in <24e422c426e0468ca1fd74b59870ff08>:0 at Javax.Net.Ssl.HttpsURLConnectionInvoker.Connect () [0x00000] in /Users/builder/azdo/_work/278/s/xamarin-android/src/Mono.Android/obj/Release/monoandroid10/android-29/mcw/Javax.Net.Ssl.HttpsURLConnection.cs:433 at Xamarin.Android.Net.AndroidClientHandler+<>c__DisplayClass44_0.b__0 () [0x0007d] in /Users/builder/azdo/_work/278/s/xamarin-android/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs:357 at System.Threading.Tasks.Task.InnerInvoke () [0x0000f] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2476 at System.Threading.Tasks.Task.Execute () [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2319 --- End of stack trace from previous location where exception was thrown ---

at Xamarin.Android.Net.AndroidClientHandler.DoProcessRequest (System.Net.Http.HttpRequestMessage request, Java.Net.URL javaUrl, Java.Net.HttpURLConnection httpConnection, System.Threading.CancellationToken cancellationToken, Xamarin.Android.Net.AndroidClientHandler+RequestRedirectionState redirectState) [0x000e4] in /Users/builder/azdo/_work/278/s/xamarin-android/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs:406 --- End of inner exception stack trace --- at Xamarin.Android.Net.AndroidClientHandler.DoProcessRequest (System.Net.Http.HttpRequestMessage request, Java.Net.URL javaUrl, Java.Net.HttpURLConnection httpConnection, System.Threading.CancellationToken cancellationToken, Xamarin.Android.Net.AndroidClientHandler+RequestRedirectionState redirectState) [0x0016e] in /Users/builder/azdo/_work/278/s/xamarin-android/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs:413 at Xamarin.Android.Net.AndroidClientHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x00286] in /Users/builder/azdo/_work/278/s/xamarin-android/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs:287 at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered (System.Threading.Tasks.Task`1[TResult] sendTask, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationTokenSource cts, System.Boolean disposeCts) [0x000b3] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/HttpClient.cs:531 at [redacted]

spouliot commented 3 years ago

@divil5000 that stack trace is for Xamarin.Android while this issue was for iOS/macOS

divil5000 commented 3 years ago

Looks like I need to create a new issue then. Thanks, I hadn't noticed that.