HttpWebRequest Hangs

  • Thread starter Thread starter mwieder
  • Start date Start date
M

mwieder

The following code hangs at the GetRequestStream after a few loops of
succesful execution:
while (true)
{
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(new
Uri(strUri));

//add headers here
httpRequest.Method = "POST";
httpRequest.Referer = strReferer;
httpRequest.ContentType = "application/x-www-form-urlencoded";
etc.....

string strPostData = strPostData;
byte[] baData = Encoding.ASCII.GetBytes(strPostData);
request.ContentLength = baData.Length;

Stream newStream = request.GetRequestStream();
newStream.Write(baData, 0, baData.Length);
newStream.Close();

HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse();
Stream sStream = httpResponse.GetResponseStream()
}

The code works fine the first few times through the loop, but I'm
guessing there is something that needs to be cleeared which is not
getting done so it gets gummed up?
 
Hello (e-mail address removed),
The following code hangs at the GetRequestStream after a few loops of
succesful execution:
while (true)
{
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(new
Uri(strUri));
//add headers here
httpRequest.Method = "POST";
httpRequest.Referer = strReferer;
httpRequest.ContentType = "application/x-www-form-urlencoded";
etc.....
string strPostData = strPostData;
byte[] baData = Encoding.ASCII.GetBytes(strPostData);
request.ContentLength = baData.Length;
Stream newStream = request.GetRequestStream();
newStream.Write(baData, 0, baData.Length);
newStream.Close();
HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse();
Stream sStream = httpResponse.GetResponseStream()
}
The code works fine the first few times through the loop, but I'm
guessing there is something that needs to be cleeared which is not
getting done so it gets gummed up?

You neither close your HttpWebResponse nor its response stream.

You should always use using-blocks or try/finally blocks for disposable types
(i.e. those that implement IDisposable) to avoid such resource leaks.

Cheers,
 
Back
Top