我编写了这个方法(C#)来接收URL的HTTP响应状态码。 当我运行此方法一次时,它可以正常工作,但是当我在循环中运行它时,第三次就会卡住。有什么线索吗?
public static string isAlive(string url)
{
Console.WriteLine("start: Is Alive Test");
WebRequest request = WebRequest.Create(url);
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
return Convert.ToString((int)response.StatusCode);
}
catch(WebException ex)
{
HttpWebResponse res = (HttpWebResponse)ex.Response;
return Convert.ToString((int)res.StatusCode);
}
}
循环结构
for (int i = 0; i < 5; i++)
{
string a = isAlive("https://www.yahoo.com/");
Console.WriteLine(a);
}
BeginX
/EndX
调用。 - Jameshttps
请求。另一种可能性是您的操作系统阻止对同一地址进行快速连接(内部洪水保护)。编辑-答案是另一种可能性,您同时打开了太多的连接。 - Adam Kewley.GetResponse()
放入using
语句中,并将isAlive方法异步化以考虑性能问题。请参见下面的代码示例: - Konstantin TarkusGetResponse
包装到using
中会使它异步。你是从哪里得到这个想法的? - Konstantin Tarkus