Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

First WebRequest slow.

I've made an app that's suppossed to make several requests to a Website.
When sending the first request using WebRequests, the process is extremely slow.
Being new to Xamarin I obviously tried google, but alas, no solution found.
That is why, I'm turning towards you for help.

I ran the exact same sequence of requests twice on an Android Emulator. You can see the Debug Log, and the code used below.
The Website requested to uses SSL, eg. HTTPS:// example.com/

My Debug Log:
pastebin.com/8DyfPPFf

My code snippet:
pastebin.com/HTSCHy4i

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited December 2019

    When sending the first request using WebRequests, the process is extremely slow.

    HttpClient provides powerful functionality with better syntax support for newer threading features, e.g. it supports the await keyword. Try to make use of the System.Net.Http.HttpClient instead, as well as the ModernHttpClient component.

    Check the tutorial:
    http://www.diogonunes.com/blog/webclient-vs-httpclient-vs-httpwebrequest/

  • @Jarvan said:

    When sending the first request using WebRequests, the process is extremely slow.

    Try to make use of the System.Net.Http.HttpClient instead, as well as the ModernHttpClient component. HttpClient provides powerful functionality with better syntax support for newer threading features, e.g. it supports the await keyword.

    Funny you should say that, since I posted this i ported my program from HttpWebRequest to HttpClient.
    The first request sequence is still about 6 seconds, whereas the second takes about 1-2 seconds.

  • It should be noted that running the exact same sequence in a console application results in no unexpected delay

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited December 2019

    Try to set this proxy property to null before calling the GetResponse method to skip the proxy autodetect step.

    request.Proxy = null;
    using (var response = (HttpWebResponse)request.GetResponse())
    {
    }
    

    Refer to: https://stackoverflow.com/a/3603413/11083277

  • @Jarvan said:
    Try to set this proxy property to null before calling the GetResponse method to skip the proxy autodetect step.
    ```
    request.Proxy = null;
    using (var response = (HttpWebResponse)request.GetResponse())

    Before switching to HttpClient i tried that without luck unfortunately.
    I've tried WebRequest
    HttpWebRequest with proxy = null and Version = 1.1
    And now HttpClient.
    All has the same problem with the first request taking longer than the rest.

  • NMackayNMackay GBInsider, University admin

    This issue has been around for yonks.

  • JohnHardmanJohnHardman GBUniversity admin

    @MrBraadDeveloper

    I haven't looked at your log or code snippet as I don't open unknown URLs. You might want to consider posting your code snippet in a forum post. The log too, if it is short.

    One thing to be aware of, is that if your app is communicating with a website/WebAPI that hasn't been accessed recently, the webhosting service may take some time to crank things up. To check this, before the first time that you make a request from your app, make a request from a browser manually. The request from the browser will result in the webhosting service cranking things up, so that when your app makes its first request there is no delay at the webhost end. Of course, this may not be what you are experiencing, but it's worth checking before spending time trying to find performance issues in your app.

  • I haven't looked at your log or code snippet as I don't open unknown URLs. You might want to consider posting your code snippet in a forum post. The log too, if it is short.
    One thing to be aware of, is that if your app is communicating with a website/WebAPI that hasn't been accessed recently, the webhosting service may take some time to crank things up. To check this, before the first time that you make a request from your app, make a request from a browser manually. The request from the browser will result in the webhosting service cranking things up, so that when your app makes its first request there is no delay at the webhost end. Of course, this may not be what you are experiencing, but it's worth checking before spending time trying to find performance issues in your app.

    I posted the resources to pastebin as I thought it was a "trusted" text holder, and since the log is rather long, I cant post it in here. It's doubtful my code has anything to do with the issue considering I've tried multiple different methods of making requests, and several other people experience the problem.

    The website is under traffic regularly, and since running a console application yields in a result without any delay on the first request sequence, i don't think its a problem on their end.

    But thanks for all the replies guys, It's nice.

  • Milner99Milner99 Member ✭✭

    Any movement on this...

    I've tried httpclient, httpwebrequest, webclient - all don't help. for Xamarin.Android I'm seeing massive delays seemingly randomly from 3--5 second (ok) right up to 60+ seconds (unacceptable). Using Postman the GET/POST testing software the requests to the same URL are always happening under 1 second.

    My project is using XF 4.2. Code for my request is:

    Top of the class

    ---------- CODE START-----------
    private HttpClient httpClient_local = new HttpClient()
    {
    Timeout = TimeSpan.FromSeconds(8),
    BaseAddress = new Uri("https://xxxxxxxxxxxx.net"),
    };
    ---------- CODE END-----------

    ....in the request method I call (added the console writes to monitor duration of course). "url" variable set above as a string. Same URL I've been using and testing in postman

    ---------- CODE START-----------
    HttpResponseMessage response = new HttpResponseMessage();
    try {
    var start_d = DateTime.Now;
    response = await httpClient_local.GetAsync(url);
    var end_d = DateTime.Now;
    TimeSpan duration = (end_d - start_d);
    Console.WriteLine(" -------------- Time to download the RSS feed: " + duration.Seconds.ToString());
    }
    catch (OperationCanceledException xc1)
    {
    Console.WriteLine("HttpClient_Local Timeout hit. Requesting again. " + xc1.Message);
    httpClient_local.CancelPendingRequests();

    try
    {
        response = await httpClient_local.GetAsync(url);
    }
    catch (OperationCanceledException xc2)
    {
    Console.WriteLine("HttpClient_Local Timeout hit again. Aborting. " + xc2.Message);
    return false;
    }
    

    }
    var stream = await response.Content.ReadAsStreamAsync();
    var feed = FeedReader.ReadFromString(new System.IO.StreamReader(stream).ReadToEnd());
    ---------- CODE ENDS-----------

    Have the NuGet CodeHollow.FeedReader to parse XML RSS that is returned.

    Any help would be greatly appreciated - this is driving me nuts!

  • ThrogarThrogar Member ✭✭

    Having the same problem, it appear to be something to do with the internet conection.

    On The Simulator it works just fine.
    On The Device it doesn't work over my wifi conection, but if I conect it to a different conection it works just fine.

Sign In or Register to comment.