HttpWebRequest closes the connection on each request

  • Thread starter Thread starter Basel
  • Start date Start date
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
 
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
 
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,
 
Back
Top