1
votes

I have sent many http requests in cycle with

WebRequest request = (WebRequest)WebRequest.Create(str);

This line takes about 500ms on my computer

WebResponse response = request.GetResponse(); 

On other computers on the network: 10-20ms

how to fix this bug? What reasons could there be for other computers on the network completing their request faster?

There is no proxy installed, and the request is to 127.0.0.1:port.

code for log

String IdHTTPUt_Get(string str) { string res="";

DateTime dt = DateTime.Now;

WriteLog("begin");

WriteLog((DateTime.Now - dt).ToString()); dt = DateTime.Now;

WebRequest request = (WebRequest)WebRequest.Create(str);

WriteLog((DateTime.Now - dt).ToString()); dt = DateTime.Now;

request.Credentials = new NetworkCredential(opt_Utor_User,opt_Utor_Password);

request.Method = "GET";

WriteLog((DateTime.Now - dt).ToString()); dt = DateTime.Now;

request.Proxy = null;

using (WebResponse response = request.GetResponse())

{

WriteLog((DateTime.Now - dt).ToString()); dt = DateTime.Now;

Stream receiveStream = response.GetResponseStream();

WriteLog((DateTime.Now - dt).ToString()); dt = DateTime.Now;

StreamReader readStream = new

StreamReader(receiveStream,Encoding.UTF8);

WriteLog((DateTime.Now - dt).ToString()); dt = DateTime.Now;

res = readStream.ReadToEnd();

WriteLog((DateTime.Now - dt).ToString()); dt = DateTime.Now;

receiveStream.Close();

readStream.Close();

}

WriteLog((DateTime.Now - dt).ToString()); dt = DateTime.Now;

return res;

}

1

1 Answers

0
votes

Which line is taking the time - WebRequest.Create or request.GetResponse()?

Are you disposing of the response at the end? It should be within a using statement:

using (WebResponse response = request.GetResponse())
{
    ...
}

The connection pool limits the number of connections you can make to any particular host - in this case you would end up waiting for the garbage collector to release an earlier response. That may not be the problem, but I wouldn't be surprised to find it was.

EDIT: Of course, the other possibility is that it's just taking a long time to process the request on your machine. What is the request doing? If you make the same request from other machines (but still to your machine) how long does that take?