在C#的多个HTTP请求
问题说明
我需要并行约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(v=VS.100).aspx"相对=nofollow> HttpWebRequest.BeginGetResponse 和<一href="http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetrequeststream(v=VS.100).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.
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhbjhfge
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20