Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform a fast web request in C#

I have a HTTP based API which I potentially need to call many times. The problem is that I can't get the request to take less than about 20 seconds, though the same request made through a browser is near instantaneous. The following code illustrates how I have implemented it so far.

WebRequest r = HttpWebRequest.Create("https://example.com/http/command?param=blabla"); var response = r.GetResponse(); 

One solution would be to make an asynchronous request but I would like to know why it takes so long and if I can avoid it. I have also tried using the WebClient class but I suspect it uses a WebRequest internally.

Update:

Running the following code took about 40 seconds in Release Mode (measured with Stopwatch):

WebRequest g = HttpWebRequest.Create("http://www.google.com"); var response = g.GetResponse(); 

I'm working at a university where there might be different things in the network configuration affecting the performance, but the direct use of the browser illustrates that it should be near instant.

Update 2:

I uploaded the code to a remote machine and it worked fine so the conclusion must be that the .NET code does something extra compared to the browser or it has problems resolving the address through the university network (proxy issues or something?!).

like image 461
Morten Christiansen Avatar asked Jul 13 '09 13:07

Morten Christiansen


People also ask

How fast should a HTTP request take?

A web response time ranging between 200 milliseconds and 1 second is considered acceptable as users still likely won't notice the delay. For better user satisfaction, you should take the time to optimize it.

What is HTTP in C?

HTTP is a text-based client-server protocol that runs over TCP. Plain HTTP runs over TCP port 80 .

How does a web request work?

A web request can also be known as an HTTP request which is the protocol that interacts between the client and the server. A client will type in a URL address prompting the web request to the server. The client or web browser will then connect to the server that it's seeking information and data from.

How HTTP request response works?

An HTTP request is made by a client, to a named host, which is located on a server. The aim of the request is to access a resource on the server. To make the request, the client uses components of a URL (Uniform Resource Locator), which includes the information needed to access the resource.


2 Answers

This problem is similar to another post on StackOverflow: Stackoverflow-2519655(HttpWebrequest is extremely slow)

Most of the time the problem is the Proxy server property. You should set this property to null, otherwise the object will attempt to search for an appropriate proxy server to use before going directly to the source. Note: this property is turn on by default, so you have to explicitly tell the object not to perform this proxy search.

request.Proxy = null; using (var response = (HttpWebResponse)request.GetResponse()) { } 
like image 56
James Roland Avatar answered Oct 03 '22 19:10

James Roland


I was having the 30 second delay on 'first' attempt - JamesR's reference to the other post mentioning setting proxy to null solved it instantly!

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_site.url); request.Proxy = null; // <-- this is the good stuff  ...  HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
like image 23
Dave Amphlett Avatar answered Oct 03 '22 20:10

Dave Amphlett