Http Request

FlorianKrieglFlorianKriegl ✭✭✭ATMember ✭✭✭

Hi,
I have some major problems with using a HttpRequest under iOS.. If I'm try to send a request, and WIFI is on, everything works like expected but when I try the exact same code with WIFI is off (using LTE) the request failes with the following exception: "The Internet connection appears to be offline." I don't know what to do and this is a critical feature in my app..

The http request code:

try
{
    HttpClient client = new HttpClient();
        var url = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/{endpoint}?key={options." + Secrets.Firebase_Auth_Secret + "}";

        var result = await client.PostAsync(url, new StringContent("test"));

}
catch (Exception e)
{

}

I'm using XF 4.1.0
and iOS 12.4

Tagged:

Answers

  • JohnHardmanJohnHardman mod GBUniversity mod

    @FlorianKriegl

    I've tested using XF 4.1 on iOS 12.4, turned off Wifi and turned on 4G mobile data (operating on the O2 - UK network). I am not seeing the problem that you report.

    I have to admit to not knowing about LTE. Do you do anything in particular to use or not use LTE?

    Also, could you post the full exception details pls.

  • FlorianKrieglFlorianKriegl ✭✭✭ ATMember ✭✭✭
    edited August 1

    @JohnHardman

    Firstly thanks for trying!

    I do not do anything in particular to use LTE I just meant I use the carrier instead of the WIFI, sorry for the miss leading.

    The full exception details:

    Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo={_kCFStreamErrorCodeKey=50, NSUnderlyingError=0x283d8b6c0 {Error Domain=kCFErrorDomainCFNetwork Code=-1009 "(null)" UserInfo={_kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <8440FF23-0EE1-48DA-B36E-A3BF2E2BFC38>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
        "LocalDataTask <8440FF23-0EE1-48DA-B36E-A3BF2E2BFC38>.<1>"
    ), NSLocalizedDescription=The Internet connection appears to be offline., NSErrorFailingURLStringKey=https://www.googleapis.com/identitytoolkit/v3/relyingparty/%7Bendpoint%7D?key=%7Boptions.SECRET_TOKEN%7D, NSErrorFailingURLKey=https://www.googleapis.com/identitytoolkit/v3/relyingparty/%7Bendpoint%7D?key=%7Boptions.SECRET_TOKEN%7D, _kCFStreamErrorDomainKey=1}
    
  • JoeMankeJoeManke ✭✭✭✭✭ USMember ✭✭✭✭✭

    Is the device you're testing on in Airplane mode or have cellular data turned off?

  • FlorianKrieglFlorianKriegl ✭✭✭ ATMember ✭✭✭
    Nope. Mobile data are on and the phone is Not in airplane mode. The strange thing is that if I activate WiFi the request works and if I disable it, it won’t
  • LuisDavidDelaCruzLuisDavidDelaCruz ✭✭✭ Member ✭✭✭

    Hi @FlorianKriegl, have you added NSAppTransportSecurity code in your info.plist file (Xamarin.iOS)
    you can check in this post how to do it, and how to consumming a rest service with Xamarin.Forms (Android & iOS)

    Also check in your iOS project to have a similar configuration

  • JohnHardmanJohnHardman mod GBUniversity mod

    Isn't the current recommendation to use "Managed (Default)" for the HttpClient Implementation?

  • FlorianKrieglFlorianKriegl ✭✭✭ ATMember ✭✭✭

    @LuisDavidDelaCruz
    I've tried it with and without ATS, this does not change anything also should ats not refer to both wifi and the carrier settings?

    @LuisDavidDelaCruz said:
    Also check in your iOS project to have a similar configuration

    My settings do exactly look like yours.

    @JohnHardman
    I don't know the current recommendation but at default, I've also NSUrlSession.

  • JohnHardmanJohnHardman mod GBUniversity mod

    @FlorianKriegl said:
    @LuisDavidDelaCruz
    I've tried it with and without ATS, this does not change anything also should ats not refer to both wifi and the carrier settings?

    @LuisDavidDelaCruz said:
    Also check in your iOS project to have a similar configuration

    My settings do exactly look like yours.

    @JohnHardman
    I don't know the current recommendation but at default, I've also NSUrlSession.

    Actually, NSUrlSession is the recommended one (for iOS 7+). See https://docs.microsoft.com/en-us/xamarin/cross-platform/macios/http-stack

  • skaylorskaylor USMember
    edited August 13

    I'm having this same issue with XF 4.1.0 on iOS 12. Happens when trying to use HttpClient to call any URL while on mobile data. I tried all three HttpClient implementations and got the same result.

    The error when connected to mobile data is the same as the error when not connected to the Internet at all. But at the same time, Xamarin.Essentials's connectivity test correctly reports that there is connectivity when connected to mobile data. I can even open Safari and browse the same pages that are failing to work through HttpClient.

    Edit: Also tried XF 4.0.0 and the pre-release 4.2.0, same issue.

  • JohnHardmanJohnHardman mod GBUniversity mod

    @skaylor said:
    I'm having this same issue with XF 4.1.0 on iOS 12. Happens when trying to use HttpClient to call any URL while on mobile data. I tried all three HttpClient implementations and got the same result.

    The error when connected to mobile data is the same as the error when not connected to the Internet at all. But at the same time, Xamarin.Essentials's connectivity test correctly reports that there is connectivity when connected to mobile data. I can even open Safari and browse the same pages that are failing to work through HttpClient.

    Edit: Also tried XF 4.0.0 and the pre-release 4.2.0, same issue.

    I suggest submitting a ticket with the support team. See https://forums.xamarin.com/discussion/150917/the-microsoft-xamarin-support-team-is-here-to-help#latest

    If you do so, please ask them to share the results back here in case it helps other people.

  • FlorianKrieglFlorianKriegl ✭✭✭ ATMember ✭✭✭
    edited August 14

    @skaylor

    If you want to use the HttpRequest to access a REST Api use Refit. I've changed to this where ever I can to avoid these annoying errors. I couldn't replace every HttpRequest, but it's at least a kind of workaround for some cases...

  • skaylorskaylor USMember
    edited August 14

    @FlorianKriegl

    When you started experiencing this issue, do you know what Visual Studio version you were using? I'm suspecting the issue has to do with Mono, Xamarin.iOS, and/or Xcode versions on the Mac machine, which are auto-updated whenever VS is updated. And you opened this issue a few days after VS2019 16.3 Preview 1 was released, which is the version with which I'm seeing this issue currently.

    My workaround for now is to install the modernhttpclient-updated NuGet package to the netstandard project and instantiate its implementation of HttpClient. This resolves the issue on iOS.

  • FlorianKrieglFlorianKriegl ✭✭✭ ATMember ✭✭✭

    @skaylor
    Yes, you are right. Today an update for visual studio and mono was released and now I don't get this error anymore. I'm will continue testing but it seems the new preview version has fixed the error :)

Sign In or Register to comment.