By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,772 Members | 1,425 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,772 IT Pros & Developers. It's quick & easy.

HttpWebRequest closes the connection on each request

P: n/a
Hi All!

My code issues Http POST requests in a loop using HttpWebRequest, I set
one unique ConnectionGroupName, and I expected from the client to open
one underlying persistent connection for all the requests I generate.

This is the code:
public static void Main()
{
for(int i=0;i<100000;i++)
PostRequest()
}

static public void PostRequest()
{
ASCIIEncoding encoding=new ASCIIEncoding();
byte[] data = encoding.GetBytes("This is a string");

HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("http://localhost/cgi");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
// Send the data.
newStream.Write(data,0,data.Length);
newStream.Close();
}

But the actual result is that on each request the client (or the
server?) closes the connection and opens a new one.

does the code above supposed to leave the connection opened?

I see that myStream.Close() method description is:
" Closes the current stream and releases any resources (such as sockets
and file handles) associated with the current stream."

So maybe this method closes the connection?

Or in this case, the server closes the connection?
Basel

May 18 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Basel,

try this :
public static void Main()
{
HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("http://localhost/cgi");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
myRequest.KeepAlive = true;
for(int i=0;i<100000;i++) {
PostRequest("This is a test")
}

}

static public void PostRequest(string data)
{
ASCIIEncoding encoding=new ASCIIEncoding();
byte[] data = encoding.GetBytes(data);

Stream newStream=myRequest.GetRequestStream();
// Send the data.
newStream.Write(data,0,data.Length);
newStream.Close();

}

Regards,
Tasos

May 18 '06 #2

P: n/a
Thus wrote Basel,
Hi All!

My code issues Http POST requests in a loop using HttpWebRequest, I
set one unique ConnectionGroupName, and I expected from the client to
open one underlying persistent connection for all the requests I
generate.

This is the code:
public static void Main()
{
for(int i=0;i<100000;i++)
PostRequest()
}
static public void PostRequest()
{
ASCIIEncoding encoding=new ASCIIEncoding();
byte[] data = encoding.GetBytes("This is a string");
HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("http://localhost/cgi");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
// Send the data.
newStream.Write(data,0,data.Length);
newStream.Close();
}
But the actual result is that on each request the client (or the
server?) closes the connection and opens a new one.

does the code above supposed to leave the connection opened?
Yes. Persistent connections are enabled by default.
I see that myStream.Close() method description is:
" Closes the current stream and releases any resources (such as
sockets
and file handles) associated with the current stream."
So maybe this method closes the connection?

Or in this case, the server closes the connection?


Quite possible. Check your HttpWebResponse for a "Connection: Close" header.
Or are you using a HTTP 1.0 proxy?

Cheers,
--
Joerg Jooss
ne********@joergjooss.de
May 19 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.