Program hangs at the third iteration (HTTPWebRequest) C#
I am currently facing a strange pro开发者_Go百科blem. I'm testing website response times, but when the method is looped on the third time (of timing the connection) it hangs:
internal class Program
{
Console.ReadLine();
loop();
}
}
The output before it hangs is: "HTTP Response Timer", so I assume it's something to do with the running instance.
You have to close the response. Otherwise you reach the maximum of open connections.
Use a using statement, this is the most simple way to close and dispose the response:
using(HttpWebResponse response = (HttpWebResponse)request.GetResponse()){
// .. here some action if any
}
You're not disposing of the response, which means it's hanging onto the connection from the previous requests. The new request is then waiting to get that connection from the pool, and blocking because the old response still "owns" the connection Just change your code to:
// You weren't really using the HttpWebResponse class anyway, so why cast?
using (var response = request.GetResponse())
{
}
Did you really mean to recurse though? Why aren't you looping using:
while(true)
{
GetResponse();
}
or something like that?
精彩评论