gpt4 book ai didi

c# - 在 Parallel.For 中发送多个 WebRequest

转载 作者:可可西里 更新时间:2023-11-01 08:56:51 26 4
gpt4 key购买 nike

我想发送多个 WebRequest。我使用了 Parallel.For 循环来执行此操作,但该循环运行一次,第二次在获得响应时出错。

错误:

The operation has timed out

代码:

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();
});

最佳答案

问题很可能是您需要在处理完响应后调用 response.Close()

关于c# - 在 Parallel.For 中发送多个 WebRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7477261/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com