Network subsystem is down, but it works on browser

RicardoSRicardoS ESMember ✭✭✭
edited October 12 in Xamarin.Forms

That's the main gist of the question.
We added a new webAPI query, but the httpRequest subroutine takes too long and gives us an exception, which we haven't seen before.

We'll provide you with the culprit url, which works perfectly on the device's browser, but not when the app is running.

Get request code:

static string GetFromURL (string url, string user, string pass) {
        try {
            string result;
            HttpRequestMessage request = new HttpRequestMessage (HttpMethod.Get, "");
            CancellationToken cancellationToken = new CancellationToken (false);
            using (HttpClient client = new HttpClient ()) {
                client.Timeout = TimeSpan.FromMinutes (2);
                client.BaseAddress = new Uri (url);
                Encoding encodign = Encoding.GetEncoding ("iso-8859-1");
                byte[] toBase64 = encodign.GetBytes ($"{user}:{pass}");
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue ("Basic", Convert.ToBase64String (toBase64));
                using (HttpResponseMessage response = client.SendAsync (request, cancellationToken).Result) {
                    result = response.Content.ReadAsStringAsync ().Result;
                }
            }
            return result;
        } catch (Exception ex) {
            Debug.WriteLine (ex is AggregateException && ex.InnerException is HttpRequestException ? ex.InnerException.ToString () : ex.Message);
            return "";
        }
    }

url: https://avsa-apitad.softoursistemas.com/api/Expedition/getExpeditionWithDemands?idExpe=184

& exception on attached file.

Thank you for your time.

ManuelR

Best Answer

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    I removed the request header as you haven't posted the user and pass and then the code runs fine on my side.
    I can get the result successfully:

    string result;
    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "https://avsa-apitad.softoursistemas.com/api/Expedition/getExpeditionWithDemands?idExpe=184");
    CancellationToken cancellationToken = new CancellationToken(false);
    using (HttpClient client = new HttpClient())
    {
        client.Timeout = TimeSpan.FromMinutes(2);
        //client.BaseAddress = new Uri(url);
        Encoding encodign = Encoding.GetEncoding("iso-8859-1");
        //byte[] toBase64 = encodign.GetBytes($"{user}:{pass}");
        //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(toBase64));
        using (HttpResponseMessage response = client.SendAsync(request, cancellationToken).Result)
        {
            result = response.Content.ReadAsStringAsync().Result;
        }
    }
    

    I tested the code above on the Android part with Forms version 4.2.0.848062.
    Please offer your environment and a sample could be better.

  • RicardoSRicardoS ESMember ✭✭✭
    edited October 14

    @LandLu
    Aw, great! I knew I was forgetting something. The user is "cross" and the password is "platform".

    A sample would be a violation of the final user's privacy, and with that URL I'm already on thin ice.

    And I almost forgot to post my environment specs:

    • Visual Studio for Mac [Community] v. 8.3.3

    • .NET Portable PCL 4.5 - Profile 111

    • Xamarin.Forms v. 3.2.0.839982

    That's all I can find.

  • LandLuLandLu Member, Xamarin Team Xamurai

    I've added the headers parameters and it still works on iOS and Android.
    I think you need to update the packages as we now all consume .Net Standard.

  • RicardoSRicardoS ESMember ✭✭✭

    @LandLu said:
    I've added the headers parameters and it still works on iOS and Android.
    I think you need to update the packages as we now all consume .Net Standard.

    What is .Net Standard?

    And will the change allow me to update the app on the Play Store and Apple App Store?

    Thanks

  • LandLuLandLu Member, Xamarin Team Xamurai

    This blog discussed the .Net Standard vs PCL: https://solidbrain.com/2017/09/12/xamarin-is-it-time-to-move-from-pcl-to-net-standard/
    However, this is written in 2017 and we even can't create a PCL template now in VS 2019. So I think it's the time we should move from PCL to .Net Standard.

    will the change allow me to update the app on the Play Store and Apple App Store?

    And this is what we have done in .Net Standard. Publishing apps using .Net Standard Forms is an important part.

  • RicardoSRicardoS ESMember ✭✭✭

    @LandLu said:
    This blog discussed the .Net Standard vs PCL: https://solidbrain.com/2017/09/12/xamarin-is-it-time-to-move-from-pcl-to-net-standard/
    However, this is written in 2017 and we even can't create a PCL template now in VS 2019. So I think it's the time we should move from PCL to .Net Standard.

    Agreed. But we can't still see where or how to change PCL to .Net Standard

    And this is what we have done in .Net Standard. Publishing apps using .Net Standard Forms is an important part.

    So is that a yay or nay about being able to update the app that is already uploaded to the store?

  • RicardoSRicardoS ESMember ✭✭✭
    edited October 15

    @LandLu said:
    First of all, here your problem is unreachable to the rest api. Try to update your forms version to see whether it works.
    I'm not sure this is caused by the PCL. But I consumed Forms 4.2 to test it.

    Gotcha! Updating as I answer.

    Thanks.

  • LandLuLandLu Member, Xamarin Team Xamurai

    Do you mean updating the Forms version resolves your issue?

  • RicardoSRicardoS ESMember ✭✭✭

    @LandLu said:
    Do you mean updating the Forms version resolves your issue?

    No, no! I meant that I will try that and comment back the results.

    Sorry if it felt like it was over.

  • RicardoSRicardoS ESMember ✭✭✭

    @Lanlu

    Update: it worked! After updating Xamarin.Forms to version 4.2.0.848062. the url worked and we expect it to continue working for the meantime. Thank you for your help.

Sign In or Register to comment.