AvadaKedavra
AvadaKedavra

Reputation: 11

System.Net.HttpWebRequest works infinitely

I have such function :

static void doAction(string link){
  Uri myUri = new Uri(link);
  HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(ur) ;
  request.GetResponse();
 }

After calling this function 2-3 times it always returns with WebException (request timeout expired) regardless of the value of link.

What's wrong?

Upvotes: 1

Views: 112

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500055

You're not disposing of the response - which means you'll end up blocking the connection pool if several URLs go to the same host, until those responses are garbage collected.

The simplest fix is just to use a using statement:

using (request.GetResponse())
{
    // No-op
}

Upvotes: 5

Related Questions