在C#中的多个HTTP请求 [英] Multiple HTTP request in C#
问题描述
我需要并行约200 HTTP请求发送到不同的服务器,并得到回应。
我在C#中使用HttpWebRequest类。
但我没有看到好的时间增强请求时,并行处理。
例如,如果一个请求需要3秒获得响应,并行2请求 - 6秒,3请求 - 8秒,4请求 - 11sec ...
这是不好的,我希望应该有最好的时间,约10秒200的请求。
它看起来像只有2-3请求执行并行,但超时WebRequest对象创建后立即启动。
我试着设置 DefaultConnectionLimit
和 MaxServicePoints
的值,但ID没有帮助。据我了解这些参数的请求并行一个网站的数量。我需要请求发送到不同的站点。
I need to send about 200 HTTP requests in parallel to different servers and get response.
I use HttpWebRequest class in C#.
But I don't see good time enhancement when requests are handled in parallel.
For example if one request needs 3sec to get response, 2 request in parallel - 6sec, 3 requests - 8 secs, 4 requests - 11sec ...
It is not good, I hope that there should be best time, about 10 sec for 200 requests.
It looks like only 2-3 requests performs in parallel, but timeout starts immediately after WebRequest object creation.
I tried set DefaultConnectionLimit
and MaxServicePoints
values, but id didn't help. As I understand these parameters for number of requests to one site in parallel. I need requests to different sites.
$ C,我用它来测试$ C例如:
Code example that I use to test:
ArrayList a = new ArrayList(150);
for (i = 50; i < 250; i++ )
{
a.Add("http://207.242.7." + i.ToString() + "/");
}
for (i = 0; i < a.Count; i++)
{
Thread t = new Thread(new ParameterizedThreadStart(performRequest));
t.Start(a[i]);
}
static void performRequest(object ip)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create((stirng)ip);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
}
Сan有人曾经遇到过这样的问题吗? 感谢您的任何建议。
Сan anyone ever encountered such a problem? Thank you for any suggestions.
推荐答案
而不是启动自己的线程试图使用HttpWebRequest的异步方法,如<一的href="http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetresponse%28v=VS.100%29.aspx"相对=nofollow> HttpWebRequest.BeginGetResponse 和<一href="http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetrequeststream%28v=VS.100%29.aspx"相对=nofollow> HttpWebRequest.BeginGetRequestStream 。
Instead of starting up your own threads try using the asynchronous methods of HttpWebRequest such as HttpWebRequest.BeginGetResponse and HttpWebRequest.BeginGetRequestStream.
这篇关于在C#中的多个HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!