发送多的WebRequest中的Parallel.For [英] Send multiple WebRequest in Parallel.For

查看:273
本文介绍了发送多的WebRequest中的Parallel.For的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想给多个的WebRequest 。我用了一个的Parallel.For 循环,这样做,但在循环运行一次,第二次提示错误而得到响应。

错误:


  

该操作已超时


code:

 的Parallel.For(0,10,委托(int i)以{
    HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(
        新的URI(http://www.mysite.com/service));    字符串dataToSend =数据;
    字节[]缓冲= System.Text.Encoding.GetEncoding(1252)。
        GetBytes会(dataToSend);
    request.Method =POST;
    request.ContentType =应用/的X WWW的形式urlen codeD;
    request.ContentLength = buffer.Length;    request.Host =www.mysite.com;    流requestStream = request.GetRequestStream();
    requestStream.Write(缓冲液,0,buffer.Length);
    requestStream.Close();    HttpWebResponse响应=(HttpWebResponse)request.GetResponse();
});


解决方案

最有可能的问题是,你需要调用 response.Close()大功告成后,处理响应

I want to send multiple WebRequest. I used a Parallel.For loop to do that but the loop runs once and the second time it gives error while getting response.

Error:

The operation has timed out

Code :

Parallel.For(0, 10, delegate(int i) {
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
        new Uri("http://www.mysite.com/service"));

    string dataToSend = "Data";
    byte[] buffer = System.Text.Encoding.GetEncoding(1252).
        GetBytes(dataToSend);
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = buffer.Length;

    request.Host = "www.mysite.com";

    Stream requestStream = request.GetRequestStream();
    requestStream.Write(buffer, 0, buffer.Length);
    requestStream.Close();

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
});

解决方案

Most likely the problem is that you need to call response.Close() after you're done processing the response.

这篇关于发送多的WebRequest中的Parallel.For的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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