【问题标题】:c# HttpWebRequest IOExceptionc# HttpWebRequest IOException
【发布时间】:2012-03-05 17:52:06
【问题描述】:

我正在使用以下函数导航到我的 c# winform 代码中的 URL 地址。

我认为,代码很好,但是当它尝试连接到 URL 地址时,它失败并抛出 IOException 错误

我的问题是:

如何在代码中添加检查以确保成功建立与 URL 的连接,如果未成功连接,它将重试直到连接成功?

public String WebRequestNavigate(string url)
        {
            StringBuilder sb = new StringBuilder();
            byte[] buf = new byte[8192];

            if (url != "")
            {
                HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
                myReq.KeepAlive = false;
                try
                {
                    HttpWebResponse resp = (HttpWebResponse)myReq.GetResponse();
                    Stream stream = resp.GetResponseStream();

                    String test = "";
                    int count = 0;
                    do
                    {

                        **count = stream.Read(buf, 0, buf.Length);**

                        if (count != 0)
                        {
                            test = Encoding.UTF8.GetString(buf, 0, count);
                            sb.Append(test);
                        }
                    }
                    while (count > 0);
                    stream.Close();
                }
                catch (WebException ex)
                {

                }


            }


            return sb.ToString();
        }

感谢朋友们的所有回答。他们都是正确的。但是,我终于发现了我的错误并纠正了它。

问题是我试图捕获 WebException ,但是,我得到了 IOException 的错误。

我将 WebException 更改为 IOException 并将我的代码更正如下:

catch (IOException ex)
                    {
                       System.Threading.Thread.Sleep(500);                       
                       myReq = (HttpWebRequest)WebRequest.Create(url);
                       myReq.KeepAlive = false;
                    }

我使用了您对 Thread.Sleep 的建议,以便在尝试新的 URL 连接之前让我的代码等待。这解决了我的问题 %100。

很抱歉耽搁了您的时间,但您对我的帮助很大并提供了见解。谢谢!

【问题讨论】:

  • 我能问一下您要连接的网址是什么吗?
  • 网址地址不同。如果你是这个意思,不是一个 url 地址。

标签: c#


【解决方案1】:

执行此操作的最简单(但不一定是“最佳”)方法是将其包装在 while 循环中,并创建一个布尔变量来确认连接是否成功。如果它永远不会成功,那么ConnectionSucceeded 将永远不会设置为true

public String WebRequestNavigate(string url)
        {
            StringBuilder sb = new StringBuilder();
            byte[] buf = new byte[8192];

            if (url != "")
            {
                bool ConnectionSucceeded;
                while (!ConnectionSucceeded) {
                    HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
                    myReq.KeepAlive = false;
                    try
                    {
                        HttpWebResponse resp = (HttpWebResponse)myReq.GetResponse();
                        Stream stream = resp.GetResponseStream();

                        String test = "";
                        int count = 0;
                        do
                        {

                            count = stream.Read(buf, 0, buf.Length);

                            if (count != 0)
                            {
                                test = Encoding.UTF8.GetString(buf, 0, count);
                                sb.Append(test);
                            }
                        }
                        while (count > 0);
                        stream.Close();
                        ConnectionSucceeded = true;
                    }
                    catch (WebException ex)
                    {

                    }

                }
            }


            return sb.ToString();
        }

【讨论】:

    【解决方案2】:

    一些类似但更简单的东西:

    Stream stream = null;
    
    while (stream == null)
    {
        try
        {
            HttpWebResponse resp = (HttpWebResponse)myReq.GetResponse();
            stream = resp.GetResponseStream();
        }
        catch (Exception e)
        {
            System.Threading.Thread.Sleep(500);
    
            // plus idea: die after a few try?
        }
    }
    

    【讨论】:

      【解决方案3】:

      我的建议是以类似的方式使用 WebClient:

      string result = "";
      bool success = false;
      int remainingAttempts = 5;
      
      while(success == false && remainingAttempts > 0) {
      
          remainingAttempts--;
      
          try
          {
              WebClient client = new WebClient();
              result = client.DownloadString(url);
              success = true;
          }
          catch (WebException ex)
          {
          }
      
          if(success == false) {
              System.Threading.Thread.Sleep(1000);
          }
      
      }
      

      查看http://msdn.microsoft.com/en-us/library/system.net.webclient.aspxhttp://msdn.microsoft.com/en-us/library/system.net.webexception.aspx 以供参考。

      【讨论】:

      • WebClient 的问题在于它没有提供与HttpWebRequest 相同的功能。它们并不是真正可以互换的,具体取决于 OP 试图实现的目标。
      • 我同意它们不可互换,这当然值得一提。但是,根据他的代码示例,他似乎想要在 url 中找到的内容的字符串表示形式。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-07
      • 2010-10-05
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多