在c#中,我创建了一个要运行的任务。它运行正常但是在55到300多次重复之后,每个循环需要更长和更长的时间才能完成。怎么了?
var task = Task.Run(() =>
{
int i = 0;
string url = "http://xxxx/getall";
string token = "dddd";
string sendData = "{somejson}"
while (true)
{
//this gets slower and slower after 50-300+ repeats
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);//create
req.Headers.Add("Authorization", token);
req.ContentType = "application/json";
req.Accept = "application/json";
req.Method = "POST";
var stream_req = req.GetRequestStream();//create
byte[] reqdata = Encoding.UTF8.GetBytes(sendData);
stream_req.Write(reqdata, 0, reqdata.Length);
stream_req.Close();
var response = req.GetResponse();//create
//get response
var reader = new StreamReader(response.GetResponseStream());//create
json = reader.ReadToEnd();
reader.Close();
response.Close();
Console.WriteLine(i.ToString()+":"+json.Substring(0, 20));
i++;
}
});
答案 0 :(得分:0)
抱歉,它不是由客户端代码引起的,而是由服务器端引起的。事实证明,服务器有一个自动阈值限制,必要时会做出反应。
我已经测试过,这段代码对其他服务器网址没有任何问题。