473,387 Members | 1,517 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.

How to limit HttpWebRequest connections?

Hi All,

I'm opening a number of threads, and each thread generates http
requests using HttpWebRequest.
How to make each thread open only 1 connection ?
I tried to set a unique ConnectionGroupName for each thread, but after
some time the number of connections exceeds the number of threads (I
see more than thousand connection while only 100 threads are running).

Thank you

May 21 '06 #1
10 20443
Basel wrote:
Hi All,

I'm opening a number of threads, and each thread generates http
requests using HttpWebRequest.
How to make each thread open only 1 connection ?
I tried to set a unique ConnectionGroupName for each thread, but after
some time the number of connections exceeds the number of threads (I
see more than thousand connection while only 100 threads are running).


HttpWebResponses are IDisposable. Are you calling Close, Dispose or are
you using "using" on it? That should kill all remaining connections.

hth,
Max
May 21 '06 #2
I'm closing the stream of the response GetResponseStream()
Anyway, I set :
ServicePointManager.DefaultConnectionLimit=10;

the program stars with 10 connections, but 10 minutes later, it starts
to open and close connections (and I see that there are thousand of
established connections).

is there a way to REALLY limit the code not to exceed a predefined
number of connections???

May 21 '06 #3
Basel,

Setting the connection group is one way to do it, but you aren't
properly disposing of the request. Just because you dispose on the response
stream doesn't mean that you are properly disposing of all the resources
that the request is using.

For any WebRequest derived class (including
HttpWebRequest/HttpWebResponse), you need to dispose of the following:

Stream returned from GetRequestStream on WebRequest
Stream returned from GetResponseStream on WebResponse
WebResponse (it implements IDisposable)

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Basel" <ba****@gmail.com> wrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
I'm closing the stream of the response GetResponseStream()
Anyway, I set :
ServicePointManager.DefaultConnectionLimit=10;

the program stars with 10 connections, but 10 minutes later, it starts
to open and close connections (and I see that there are thousand of
established connections).

is there a way to REALLY limit the code not to exceed a predefined
number of connections???

May 21 '06 #4
I closed the streams,

I set this:
ServicePointManager.DefaultConnectionLimit=10;
ServicePointManager.MaxServicePointIdleTime=10000;

when I run the following code in threads, after a couple of minutes I
see in the network status that hunderds of connections are opened, and
some connections are closing.

here is the code:

for(int i=0;i<1000000;i++)
{
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create("http://myserver");

using(WebResponse response = request.GetResponse())
{

StreamReader reader = new
StreamReader(response.GetResponseStream());

string str = reader.ReadLine();
//do some parsing on str
response.GetResponseStream().Close();

}
}

Note: I'm running 2000 request/ second

The code is ok? or I missed something!?

Thank you!

May 21 '06 #5
Basel,
No this is probably not alright. What you are doing here is opening a large
number of Requests all on the same thread, in a serial manner. This can not
only take a long time, but it is fraught with complications.

You want to structure your application to do this either on the default .NET
threadpool so you are running each on a background thread with an
asynchronous callback method to get the response, get your data and clean up,
or you want to use a custom threadpool. In this manner as each connection is
processed and disposed, that threadpool thread is freed up to handle another
workitem.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Basel" wrote:
I closed the streams,

I set this:
ServicePointManager.DefaultConnectionLimit=10;
ServicePointManager.MaxServicePointIdleTime=10000;

when I run the following code in threads, after a couple of minutes I
see in the network status that hunderds of connections are opened, and
some connections are closing.

here is the code:

for(int i=0;i<1000000;i++)
{
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create("http://myserver");

using(WebResponse response = request.GetResponse())
{

StreamReader reader = new
StreamReader(response.GetResponseStream());

string str = reader.ReadLine();
//do some parsing on str
response.GetResponseStream().Close();

}
}

Note: I'm running 2000 request/ second

The code is ok? or I missed something!?

Thank you!

May 22 '06 #6
My goal is to open only ONE connection for each thread that should stay
active along the way.

BTW, the more ServicePointManager.MaxServicePointIdleTime is smaller,
connections grows more rapidly

It seems all the threads uses only one connection from the established
connections. when the timeout occurs it renews the timed-out
connections.

What is the correct way to force each thread open one *socket* that
will be used only by this thread?

May 22 '06 #7
Thus wrote Basel,
Hi All,

I'm opening a number of threads, and each thread generates http
requests using HttpWebRequest.
How to make each thread open only 1 connection ?
I tried to set a unique ConnectionGroupName for each thread, but after
some time the number of connections exceeds the number of threads (I
see more than thousand connection while only 100 threads are running).


Are you sure closing the connection isn't initiated by the server?

Cheers,
--
Joerg Jooss
ne********@joergjooss.de
May 23 '06 #8
I dont know if closing the connection is initiated by the serve.

Let let me ask a question,
I want to create 10 threads so that each thread runs a loop that sends
a request to server and gets the response.

and I want each thread to open a single connection.

Can you please tell me how to do that?

May 23 '06 #9
Thus wrote Basel,
I dont know if closing the connection is initiated by the serve.
Well, find out ;-)
Let let me ask a question,
I want to create 10 threads so that each thread runs a loop that sends
a request to server and gets the response.
and I want each thread to open a single connection.
That's not possible, unless both client and server agree to use a persistent
connection. While this is the default behaviour in HTTP 1.1 and thus HttpWebRequest,
it doesn't mean that your server is willing to keep the connection open.
There's no point in reusing a TCP connection if your server sends a "Connection:
close" or switches back to HTTP 1.0's default behaviour -- the server will
close the connection and not accept any further requests on it.
Can you please tell me how to do that?


Monitor the HTTP traffic between your client and your server with a network
sniffer or a web proxy to see whether your server closes connections.

Cheers,
--
Joerg Jooss
ne********@joergjooss.de
May 25 '06 #10
Can't see the rest of this thread but you may also want to take a look at
the ServicePoint class
http://msdn2.microsoft.com/en-US/lib...vicepoint.aspx

Cheers,

Greg Young

"Joerg Jooss" <ne********@joergjooss.de> wrote in message
news:94**************************@msnews.microsoft .com...
Thus wrote Basel,
I dont know if closing the connection is initiated by the serve.


Well, find out ;-)
Let let me ask a question,
I want to create 10 threads so that each thread runs a loop that sends
a request to server and gets the response.
and I want each thread to open a single connection.


That's not possible, unless both client and server agree to use a
persistent connection. While this is the default behaviour in HTTP 1.1 and
thus HttpWebRequest, it doesn't mean that your server is willing to keep
the connection open. There's no point in reusing a TCP connection if your
server sends a "Connection: close" or switches back to HTTP 1.0's default
behaviour -- the server will close the connection and not accept any
further requests on it.
Can you please tell me how to do that?


Monitor the HTTP traffic between your client and your server with a
network sniffer or a web proxy to see whether your server closes
connections.

Cheers,
--
Joerg Jooss
ne********@joergjooss.de

May 25 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Cliff Harris | last post by:
I am trying to automate a series of form posts on a website. This site requires that my session be kept alive through successive posts (it basically tracks me by a sessionid, and if I get a new...
1
by: Paul DeMarco | last post by:
I'm using the HttpWebRequest repeatidly. I have basic authentication, unsafe connection pooling, keepalive, and preauthentication on. Within .NET it clearly reuses the http connections, I can...
5
by: japslam japslam via DotNetMonster.com | last post by:
Hi all, I have problem when I use HttpWebRequest and take long time to call to my service server. If at that time there are many request comes in semultaneous, I will get this exception ...
1
by: sfoxover | last post by:
Hi, Could someone please give me some suggestions on how to make this class robust. I need to be able to handle around 20 similtanious requests to this class which causes a web browser to...
0
by: a_newcomb | last post by:
I have an application which polls and connects to a webserver on a background thread. When the application is supposed to exit, I call abort on the HttpWebRequest object. I have observed that...
1
by: Kevin Landymore | last post by:
I have a vb.net service running under a Domain account. I'm trying to call a web service on our Mainframe (IBM CICS via SSL and Client Certificates) and after a while (1 or 2 days.. thousands of...
4
by: Bill | last post by:
Hi, I would be grateful if someone could clarify my rather confused ideas of the 10 connection limit on XP/2000 when its being used as a server. (I realise that XP is really a client op sys with...
1
by: Harold | last post by:
Hi, I'm writing software which should download some files. I want to limit the number of simultanioulsy downloads to two. I'm using the ServicePointManager object for this. But I still can...
2
by: Basel | last post by:
Hi All! I'm having a problem with a growing number of connections. my app generates http requests and reads responses. the app creates large number of threads (100 or more) that generates the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.