473,587 Members | 2,321 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ConnectionGroup Name 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 20477
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 ConnectionGroup Name 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).


HttpWebResponse s 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 GetResponseStre am()
Anyway, I set :
ServicePointMan ager.DefaultCon nectionLimit=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 GetRequestStrea m on WebRequest
Stream returned from GetResponseStre am on WebResponse
WebResponse (it implements IDisposable)

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

"Basel" <ba****@gmail.c om> wrote in message
news:11******** **************@ 38g2000cwa.goog legroups.com...
I'm closing the stream of the response GetResponseStre am()
Anyway, I set :
ServicePointMan ager.DefaultCon nectionLimit=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:
ServicePointMan ager.DefaultCon nectionLimit=10 ;
ServicePointMan ager.MaxService PointIdleTime=1 0000;

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.Cre ate("http://myserver");

using(WebRespon se response = request.GetResp onse())
{

StreamReader reader = new
StreamReader(re sponse.GetRespo nseStream());

string str = reader.ReadLine ();
//do some parsing on str
response.GetRes ponseStream().C lose();

}
}

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:
ServicePointMan ager.DefaultCon nectionLimit=10 ;
ServicePointMan ager.MaxService PointIdleTime=1 0000;

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.Cre ate("http://myserver");

using(WebRespon se response = request.GetResp onse())
{

StreamReader reader = new
StreamReader(re sponse.GetRespo nseStream());

string str = reader.ReadLine ();
//do some parsing on str
response.GetRes ponseStream().C lose();

}
}

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 ServicePointMan ager.MaxService PointIdleTime 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 ConnectionGroup Name 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********@joer gjooss.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********@joer gjooss.de
May 25 '06 #10

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

Similar topics

1
9319
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 connection each time, I get a new sessionid). An HttpWebRequest object has a KeepAlive property that is, by default, set to true. I have been...
1
1816
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 easily verify this is correct with a packet sniffer. I may make 1000s of requests over 1-2 established http connections in a given time period. Then...
5
5864
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 System.Net.WebException: The underlying connection was closed: The request was canceled. ---> System.IO.IOException: Unable to read data from the...
1
2597
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 display a waiting message while it requests XML data from a third party server. Some requests can take around 30 seconds. Usually the first time I do...
0
1429
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 when the PDT has no network connections ( i.e. configured for dhcp on the wireless interface, and the wireless settings aren't configured ), and the...
1
4092
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 transactions) I'm getting the following errors: First, 2 of these: SoapDriver GotRequestStream System.Net.WebException: The underlying connection...
4
10765
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 limited server capability, I am also aware you can kludge the number to 40, but assume I do not want to do that). As I understand it XP Pro will...
1
1781
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 download more then two files at the same time. When I use perfmon with the counter "Web service/current connections" the number of connection are more...
2
2468
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 requests, this is the code: ServicePointManager.DefaultConnectionLimit = 10; ThreadFunction()
0
7918
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8340
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6621
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2353
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.