HttpWebRequest closes the connection on each request

B

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?

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
 
T

Tasos Vogiatzoglou

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
 
J

Joerg Jooss

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,
 

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