• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

在C#的多个HTTP请求

用户头像
it1352
帮助1

问题说明

我需要并行约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.

正确答案

#1

而不是启动自己的线程试图使用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
系列文章
更多 icon
同类精品
更多 icon
继续加载