HttpWebRequest Hangs

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?
 
J

Joerg Jooss

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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top