1

I´m writing a function to do a query to a webservice, hosted locally on my computer. It works fine, but the GetResponse method is taking more time than what I expect. More specifically, when I do the request on my browser, it takes about 10 milliseconds and the GetResponse method is taking far from that like 300 milliseconds.

Am I doing something wrong on the code?, something I can improve?

    public static string CargarListaRutas()
    {
        WebRequest request = HttpWebRequest.Create("http://localhost:8080/services/rest/184108301/listaRutas/");
        request.Timeout = 2000;
        WebResponse response;
        string responseFromServer;
        try
        {
            using (response = request.GetResponse())
            {
                Stream dataStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(dataStream);
                responseFromServer = reader.ReadToEnd();
            }
        }
        catch
        {
            responseFromServer = String.Empty;
        }

        return responseFromServer;
    }
NicoRiff
  • 4,803
  • 3
  • 25
  • 54
  • 4
    Are you sure it's not because the browser is using the cached version? You could try CTRL + F5 to force the browser to reload the page and not use cache to test timing. – itsme86 Aug 26 '13 at 23:25
  • 1
    If itsme86's comment doesn't help. This might be caused by the automatic proxy detection. Try setting request.Proxy = null. See http://stackoverflow.com/a/3603413/442078 – Will Aug 27 '13 at 00:42
  • itsme86, you are right. When I clean the cache the request takes about 300 ms just like my application. Thank you for your help. – NicoRiff Aug 27 '13 at 00:45

1 Answers1

3

It's possible that your browser is so much faster because it's using cache. Try CTRL + F5 to force the browser to reload the page and not use cache to test timing.

itsme86
  • 19,266
  • 4
  • 41
  • 57