在一个循环中运行时HttpWebResponse卡住HttpWebResponse

2023-09-03 06:06:57 作者:贱男春°

我为了从一个URL收到HTTP响应状态code构建这个方法(C#)。 whene我运行这个方法的人是工作正常,但是当我在一个循环中运行它之后,第三次的坚持。任何线索?

 公共静态字符串的isAlive(字符串URL)
    {
        Console.WriteLine(开始:还活着测试);
        WebRequest的请求= WebRequest.Create(URL);
        尝试
        {
            HttpWebResponse响应=(HttpWebResponse)request.GetResponse();
            返回Convert.ToString((int)的response.Status code);
        }
        赶上(WebException前)
        {
            HttpWebResponse RES =(HttpWebResponse)ex.Response;
            返回Convert.ToString((int)的res.Status code);
        }
    }
 

循环

 的for(int i = 0;我小于5;我++)
        {
            字符串=的isAlive(https://www.yahoo.com/);
            Console.WriteLine(一);
        }
 

解决方案

你不是叫处置 HttpWebResponse 的对象,这意味着连接仍然躺在附近。如果你改变你的code以下内容:

 公共静态字符串的isAlive(字符串URL)
{
   Console.WriteLine(开始:还活着测试);
   WebRequest的请求= WebRequest.Create(URL);
   尝试
   {
       使用(HttpWebResponse响应=(HttpWebResponse)request.GetResponse())
        {
            返回Convert.ToString((int)的response.Status code);
        }

   }
   赶上(WebException前)
   {
       使用(HttpWebResponse RES =(HttpWebResponse)ex.Response)
       {
          返回Convert.ToString((int)的res.Status code);
       }
   }
}
 
怎样实现分时段的循环时间控制

使用语句将隐式调用Dispose对你来说,这将关闭连接。

的原因,你的code为第二次迭代后终止是因为NET有一个内置的连接,它会打开一个网站,默认情况下是2。这是由 System.Net.ServicePointManager.DefaultConnectionLimit ,你可以增加应该需要。

I build this method (c#) in order to receive the HTTP response status code from an URL. whene I run this method ones it's works fine, but when I run it in a loop, the third time its stuck. any clue??

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

the loop

        for (int i = 0; i < 5; i++)
        {
            string a = isAlive("https://www.yahoo.com/");
            Console.WriteLine(a);
        }

解决方案

You're not calling Dispose on the HttpWebResponse object, which means that the connection is still lying around. If you change your code to the following:

public static string isAlive(string url)
{
   Console.WriteLine("start: Is Alive Test");
   WebRequest request = WebRequest.Create(url);
   try
   {
       using(HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            return Convert.ToString((int)response.StatusCode);
        }

   }
   catch(WebException ex)
   {
       using(HttpWebResponse res  = (HttpWebResponse)ex.Response)
       {
          return Convert.ToString((int)res.StatusCode);
       }
   }
}

the using statement will implicitly call Dispose for you, which will close the connection.

The reason your code is halting after the second iteration is because .Net has a built in maximum number of connections it will open to a website, which is by default 2. This is controlled by System.Net.ServicePointManager.DefaultConnectionLimit which you can increase should you need to.