14

I am making calls to a WEB API using RESTSHARP and they work fine. However, the Initial call to the API (regardless of what call it is) can sometimes take up to 10 seconds to get a response. Every other call after that is really quick. Does anyone know a way around this?

I am running a WPF 4.0 application

code:

var client = new RestClient(apiAddress);
var request = new RestRequest(Method.GET);

IRestResponse response = client.Execute(request);
RB.
  • 36,301
  • 12
  • 91
  • 131
Neil Hobson
  • 515
  • 1
  • 7
  • 14

4 Answers4

13

It's most likely the network settings causing this problem. I recently had the same issue and it turned out that when using HttpWebRequest or RestSharp it was trying some auto configuration to look for a proxy server.

Open the network settings in Internet Explorer and disable auto configuration for the local network. In my case this resolved the delay for the first request in RestSharp as well.

Servy
  • 202,030
  • 26
  • 332
  • 449
skrause
  • 989
  • 1
  • 9
  • 14
  • 1
    This did fix the problem Servy. I have marked this as correct, however it wont be any use to me as I am developing a commercial application which is to be installed on multiple computers and I cant force users to disable this option. Thanks though for clarifying. – Neil Hobson Oct 05 '12 at 15:23
  • You can also do this: http://stackoverflow.com/questions/2519655/httpwebrequest-is-extremely-slow – christo8989 Sep 16 '15 at 21:58
5

I had attempted @skrause's answer, but it wasn't work for me. I spend much time, and finaly I solved it. This my sulotion.

public class SimpleWebProxy : IWebProxy
{
    public ICredentials Credentials { get; set; }

    public Uri GetProxy(Uri destination)
    {
        return destination;
    }

    public bool IsBypassed(Uri host)
    {
        // if return true, service will be very slow.
        return false;
    }

    private static SimpleWebProxy defaultProxy = new SimpleWebProxy();
    public static SimpleWebProxy Default
    {
        get
        {
            return defaultProxy;
        }
    }
}

var client = new RestClient();
client.Proxy = SimpleWebProxy.Default;
wi11du0n
  • 51
  • 1
  • 3
2

Tried to get rid of the auto configuration to look for a proxy server with this

System.Net.WebRequest.DefaultWebProxy = null;
CAA
  • 23
  • 4
0

If You use winforms etc. in app.config after connectionstrings:

<system.net>
  <defaultProxy enabled="true">
    <proxy usesystemdefault="True"/>
  </defaultProxy>
</system.net>
Inspector
  • 11
  • 1