如何限制HttpWebRequest连接? [英] How to limit HttpWebRequest connections?

查看:111
本文介绍了如何限制HttpWebRequest连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在打开一些线程,每个线程使用HttpWebRequest生成http

请求。

如何让每个线程只打开一个连接?

我试图为每个线程设置一个唯一的ConnectionGroupName,但在

之后,连接数超过了线程数(I

看到超过一千个连接而只有100个线程正在运行)。


谢谢

解决方案

巴塞尔写道:

大家好,

我正在打开一些线程,每个线程使用HttpWebRequest生成http
请求。
如何让每个线程只打开一个连接?
我试图为每个线程设置一个唯一的ConnectionGroupName,但是在
一段时间之后的数量连接超过线程数(我看到超过一千个连接,而只有100个线程正在运行)。


HttpWebResponses是IDisposable。您是否正在使用使用来呼叫关闭,处理或

在上面?这应该会杀死所有剩余的连接。


hth,

Max


我正在关闭响应流GetResponseStream()

无论如何,我设置:

ServicePointManager.DefaultConnectionLimit = 10;


程序的明星有10个连接,但10分钟后,它开始

来打开和关闭连接(我看到有数千个

已建立的连接)。


有没有办法真正限制代码不超过预定义的

连接数???

巴塞尔,


设置连接组是一种方法,但是你并没有正确处理请求。仅仅因为你处理了回复

流并不意味着你正在处理请求所使用的所有资源




对于任何WebRequest派生类(包括

HttpWebRequest / HttpWebResponse),您需要处理以下内容:


从GetRequestStream返回的流在WebRequest上

从WebResponse上的GetResponseStream返回的流

WebResponse(它实现了IDisposable)


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Basel" < BA **** @ gmail.com>在消息中写道

news:11 ********************** @ 38g2000cwa.googlegro ups.com ...

我正在关闭响应的流GetResponseStream()
无论如何,我设置:
ServicePointManager.DefaultConnectionLimit = 10;

程序明星有10个连接,但10分钟后,它开始打开和关闭连接(我看到有数千个已建立的连接)。

有没有办法真正限制代码不超过预定义的连接数???



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

解决方案

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


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???


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???



这篇关于如何限制HttpWebRequest连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆