473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

HttpWebRequest closes the connection on each request

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
2 2506
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Glenn | last post by:
Is it possible to only redraw a portion of an HTML page as part of a request? With each request, I am updating number of results but do not want to lose where you were in a page. I created...
9
by: MLH | last post by:
Trouble is, it doesn't happen every time. Yesterday, for example, it happened only once and not again afterward. Some days ago, a similar situation. Today, well - I tried 7 times straight to open...
1
by: Bruno Piovan | last post by:
Hi, I'm having problems with my site, the Session_Start event is fired every time the user makes a new request, it happened with 2 users up to now, so its not a common behavior..... does anyone...
6
by: Mike Kline | last post by:
Hi There! I'm creating a HttpModule and after AcquireRequestState event, I tried to access the HttpApplication.Session.SessionID and somehow I'm getting a new value on every Refersh of ASPX...
3
by: samik | last post by:
I have working on WinXP + IIS 5.0 + .NET 1.1 Whenever i open a page with the url starting as http://localhost/<page addressor http://127.0.0.1/<page addressthe application to hold session...
3
by: Bob | last post by:
I need to get several recordset for which I'm opening a datareader like so... OleDbCommand rsA = new OleDbCommand("Select * from Authors",cnAccess); OleDbDataReader drA = rsA.ExecuteReader();...
14
by: DavidNorep | last post by:
I do not know PHP, consider to write a CGI with this technology and have the following question. Is it possible to invoke a PHP script and let it endlessly wait for requests from a website (a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.