Xamarin-macios: HttpClient fails to reach any URL via mobile data

Created on 13 Aug 2019  路  20Comments  路  Source: xamarin/xamarin-macios

Steps to Reproduce

  1. Create a Xamarin.Forms app that makes an HttpClient SendAsync call to any URL and deploy it to a physical iOS device.
  2. Disconnect from Wi-Fi and connect to mobile data (4G LTE in my case).
  3. Make the HttpClient call.

Expected Behavior

The call succeeds.

Actual Behavior

System.Net.WebException: The Internet connection appears to be offline.
--->
Foundation.NSError-Exception: Error Domain=NSURLError-Domain Code=-1009
"The Internet connection appears to be offline."

UserInfo={_kCFStreamErrorCodeKey=50, NSUnderlyingError=0x281f164c0 {Error Domain=kCFErrorDomainCFNetwork Code=-1009 "(null)" UserInfo={_kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <29082FC1-D10D-41CE-A44E-D5D41503D0BB>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalDataTask <29082FC1-D10D-41CE-A44E-D5D41503D0BB>.<1>" ), NSLocalizedDescription=The Internet connection appears to be offline., NSErrorFailingURLStringKey=https://some.url/path, NSErrorFailingURLKey=https://some.url/path, _kCFStreamErrorDomainKey=1}

The request is not even being attempted, and instead this error is immediately returned, exactly the same behavior seen when the device is legitimately not connected to the Internet.

Obviously, I've verified that I do in fact have an Internet connection when testing this. Xamarin Essentials correctly reports that there is connectivity, and I can open Safari and browse the same websites that HttpClient is failing to reach.

Environment

iPhone 7 Plus
iOS 12.3.1 (also seen reports from 12.2 and 12.4)
VS2019 16.3.1 P1 & P2
Xamarin.Forms 4.1.0.673156 (also seen with 4.0.0 and 4.2.0-pre)

This works fine on Android and does not work with any one of the three iOS HttpClient implementations.

Related: https://forums.xamarin.com/discussion/162993/http-request

bug iOS

Most helpful comment

I had another team build the same test app on a different Mac, and they could not reproduce the issue. So the issue may be with the Mono, Xamarin.iOS, or Xcode installations on our Mac. These are all whatever version is prompted for update when first pairing VS2019 16.3.0 Preview 1 to the Mac.

If I build the app (on our Mac) using modernhttpclient (installing that NuGet package to the netstandard project, and then replacing new HttpClient() with new HttpClient(new NativeMessageHandler())), it works.

Edit: Just updated from 16.3 Preview 1 to Preview 2, which prompted to update the Mac's Xamarin.iOS version from 12.16.0.5 to 12.16.1.9. The issue persists after the update.

I believe this issue began occurring when I updated to 16.3 Preview 1, from whatever preview version came before that (16.2 Preview 2, I think). 16.3 P1 was released on July 24, while a version of our app I built on July 15 does not have this issue. The forum post I linked was opened on July 31.

All 20 comments

Thank you for your feedback!

Unfortunately I'm not able to reproduce this problem with the information available. For us to investigate this further, could you please provide a complete test project I can use to reproduce the problem, and all your version information.

The easiest way to get exact version information:

  • On Visual Studio for Mac: "Visual Studio" menu, "About Visual Studio" item, "Show Details" button.
  • On Visual Studio for Windows: "Help menu", "About Microsoft Visual Studio" item.

Then copy/paste the version information (you can use the "Copy Information" button).

We look forward to hearing from you!

VS version info here. OS is Windows 10. Behavior has been seen on iPhone 7 and 8, on iOS 12.2, 12.3.1, and 12.4.

Attaching a repro project. I would like to include the built IPA, but obviously it wouldn't be provisioned for you to use.

HttpTestApp.zip

I had another team build the same test app on a different Mac, and they could not reproduce the issue. So the issue may be with the Mono, Xamarin.iOS, or Xcode installations on our Mac. These are all whatever version is prompted for update when first pairing VS2019 16.3.0 Preview 1 to the Mac.

If I build the app (on our Mac) using modernhttpclient (installing that NuGet package to the netstandard project, and then replacing new HttpClient() with new HttpClient(new NativeMessageHandler())), it works.

Edit: Just updated from 16.3 Preview 1 to Preview 2, which prompted to update the Mac's Xamarin.iOS version from 12.16.0.5 to 12.16.1.9. The issue persists after the update.

I believe this issue began occurring when I updated to 16.3 Preview 1, from whatever preview version came before that (16.2 Preview 2, I think). 16.3 P1 was released on July 24, while a version of our app I built on July 15 does not have this issue. The forum post I linked was opened on July 31.

I can reproduce the problem with your test case and we'll have a look.

@mandel-macaque can you have a look at this when you get back?

Having this issue too. #6814

Seems to be on Visual Studio Preview 1 and 2. Only happens on cell service and not wifi; and NSUrlSession as the HttpClient. I'm not sure if this is Mono or Xamarin.iOS 12.16.1.9. If I build and release in Visual Studio 2017 15.9.14 I have no problems - I haven't updated to 15.9.15 to test.

System.Net.Http.NSUrlSessionHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) <0x101b2e440 + 0x0093b> in <f1b796db9c7d4549b05892265dd5c856#b0cff759c8b961ea3351e930e2fd08b9>:0 System.Net.Http.HttpRequestException: Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo={_kCFStreamErrorCodeKey=50, NSUnderlyingError=0x283bba160 {Error Domain=kCFErrorDomainCFNetwork Code=-1009 "(null)" UserInfo={_kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <0B852FAE-0EBD-423A-B5FA-7386B0671A93>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalDataTask <0B852FAE-0EBD-423A-B5FA-7386B0671A93>.<1>" ), NSLocalizedDescription=The Internet connection appears to be offline., NSErrorFailingURLStringKey=https://, NSErrorFailingURLKey=https://, _kCFStreamErrorDomainKey=1}

@ryanherman can confirm that downgrading from VS Preview fixed the issue.

@Bronson15 Yes 100% works on Visual Studio 2017 15.9.14 My app is in review to replace the broken one.

I am still Getting Same Issue

Visual Studio Community 2019 for Mac (Preview)
Version 8.3 Preview (8.3 build 1290)

{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=0x283601b30 {Error Domain=kCFErrorDomainCFNetwork Code=-1009 "(null)" UserInfo={_kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <BA3583D2-B2A6-4D40-B76E-8FA18835504F>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <BA3583D2-B2A6-4D40-B76E-8FA18835504F>.<1>"
), NSLocalizedDescription=The Internet connection appears to be offline.,

I have the same issue,

Visual Studio Community 2019 for Mac (Preview)
Any Support,

Just update to Stable Channel, this issue will be fixed

Just update to Stable Channel, this issue will be fixed

Confirmed, Downgrading to the last stable release fixes the issue,

I have executed the provided test application on the following release branches:

  • master (8a4c85863) - Failure
  • d16-3 Preview 1 (26f04c54) Pkg Version 12.16.0.9 - *Failure * (Package can be found here is not .5 but there are no network commits.)
  • d16-3 (2741499f) Pkg Version 12.16.1.22- * Failure* (Package can be found here)
  • d16-2 (a3b8cddd) Pkg Version 12.14.0.116- Success (Package can be found here)

All tests have done with a iPhone 8+ on 12.4.1 (16G102). The issue is due to commit https://github.com/xamarin/xamarin-macios/pull/6059. The property was set to be 'false' by default. I'll do an update to revert that default value.

@mandel-macaque Is there some workaround that involves setting AllowsCellularAccess to true on our instance of NSUrlSessionHandler? Since I'm just using HttpClient in the shared XF project, I'm not sure how I would access that property.

@ArrowCase yes, before you do the very first request, use the property here: https://github.com/xamarin/xamarin-macios/blob/master/src/Foundation/NSUrlSessionHandler.cs#L285 to set the value to true and you should be good to go.

So something like this:

var handler = new NSUrlSessionHandler ();
handler.AllowsCellularAccess = true;
var httpClient = new HttpClient (handler);

You might need to do an #if IOS to do it. But will unblock you until we land the fix.

I am facing this issue randomly.
My Xamarin.iOS version is 12.4.0.64
Visual studio 2017 Version 15.9.16
I am facing it first time on the launch of the app.

@gopalawasthi123 you probably are facing a diff issue (sorry I missed you comment since the issue is closed). Can you please test with the latests xamarin and if that is the case, create another issue.

Thanks!

@mandel-macaque Yes you are Right this solved the issue. Thanks anyway

I updated to the latest VS on Mac and started having this issue.

Here's my VS Info:

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

Version 8.8.1 (build 37)
Installation UUID: a5ee7707-56b3-4bbd-b7c4-cbe31efff731
GTK+ 2.24.23 (Raleigh theme)
Xamarin.Mac 6.18.0.23 (d16-6 / 088c73638)

Package version: 612000093

=== Mono Framework MDK ===

Runtime:
Mono 6.12.0.93 (2020-02/620cf538206) (64-bit)
Package version: 612000093

=== Roslyn (Language Service) ===

3.8.0-5.20515.7+713efb0e68d34ab75faaf709e071e5c2d8d4ed0e

=== NuGet ===

Version: 5.8.0.6860

=== .NET Core SDK ===

SDK: /usr/local/share/dotnet/sdk/5.0.100/Sdks
SDK Versions:
5.0.100
5.0.100-rc.2.20479.15
3.1.403
3.1.402
3.1.301
3.1.300
3.1.202
3.1.200
3.1.101
3.0.101
2.2.401
2.1.701
2.1.403
2.1.4
MSBuild SDKs: /Applications/Visual Studio.app/Contents/Resources/lib/monodevelop/bin/MSBuild/Current/bin/Sdks

=== .NET Core Runtime ===

Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
5.0.0
5.0.0-rc.2.20475.5
3.1.9
3.1.8
3.1.5
3.1.4
3.1.2
3.1.1
3.0.1
2.2.6
2.1.23
2.1.22
2.1.19
2.1.18
2.1.17
2.1.16
2.1.15
2.1.12
2.1.5
2.0.5

=== .NET Core 3.1 SDK ===

SDK: 3.1.403

=== Xamarin.Profiler ===

Version: 1.6.13.11
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Updater ===

Version: 11

=== Xamarin.Android ===

Version: 11.1.0.17 (Visual Studio Community)
Commit: xamarin-android/d16-8/c0e2b8e
Android SDK: /Users/mtio/Library/Developer/Xamarin/android-sdk-macosx
Supported Android versions:
7.0 (API level 24)
7.1 (API level 25)
8.0 (API level 26)
8.1 (API level 27)

SDK Tools Version: 26.1.1
SDK Platform Tools Version: 30.0.5
SDK Build Tools Version: 29.0.2

Build Information:
Mono: be2226b
Java.Interop: xamarin/java.interop/d16-8@79d9533
ProGuard: Guardsquare/proguard/proguard6.2.2@ebe9000
SQLite: xamarin/sqlite/3.32.1@1a3276b
Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-8@2fb1cbc

=== Microsoft OpenJDK for Mobile ===

Java SDK: /Users/mtio/Library/Developer/Xamarin/jdk/microsoft_dist_openjdk_1.8.0.25
1.8.0-25
Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

=== Android SDK Manager ===

Version: 16.8.0.32
Hash: 01a7774
Branch: remotes/origin/d16-8
Build date: 2020-10-13 23:32:30 UTC

=== Android Device Manager ===

Version: 16.8.0.45
Hash: fc0af5f
Branch: remotes/origin/d16-8
Build date: 2020-10-13 23:32:54 UTC

=== Apple Developer Tools ===

Xcode 12.2 (17535)
Build 12B45b

=== Xamarin.Mac ===

Version: 7.0.0.15 (Visual Studio Community)
Hash: 87a1b18d8
Branch: d16-8
Build date: 2020-11-16 21:39:40-0500

=== Xamarin.iOS ===

Version: 14.6.0.15 (Visual Studio Community)
Hash: 87a1b18d8
Branch: d16-8
Build date: 2020-11-16 21:39:41-0500

=== Xamarin Designer ===

Version: 16.8.0.507
Hash: e87b24884
Branch: remotes/origin/d16-8
Build date: 2020-10-29 00:31:38 UTC

=== Build Information ===

Release ID: 808010037
Git revision: acd8ddc086768c701bd641d6ec27316f45813175
Build date: 2020-11-17 12:49:41-05
Build branch: release-8.8
Xamarin extensions: acd8ddc086768c701bd641d6ec27316f45813175

=== Operating System ===

Mac OS X 10.15.7
Darwin 19.6.0 Darwin Kernel Version 19.6.0
Mon Aug 31 22:12:52 PDT 2020
root:xnu-6153.141.2~1/RELEASE_X86_64 x86_64

Was this page helpful?
0 / 5 - 0 ratings