2012-03-05 31 views
0

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

我认为,该代码是好的,但是当它试图建立连接的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。

对不起,你花时间,但你帮助我很好,并提供了见解。谢谢!

+0

我可以问你想要连接到什么网址吗? – f2lollpll 2012-03-05 18:05:16

+0

网址有所不同。如果你的意思不是一个URL地址。 – 2012-03-16 14:04:02

回答

1

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

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? 
    } 
} 
2

这样做最简单(但不一定是最好的)方法是将其封装在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(); 
     } 
0

我的建议是尽量使用Web客户端的一个类似的方式:

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参考。

+0

WebClient问题在于它没有提供与HttpWebRequest相同的功能。它们并不是真正可以互换的,这取决于OP试图达到的目标。 – 2012-03-05 18:36:25

+0

我同意他们不可互换,这当然值得一提。但是,根据他的代码示例,他显示他想要在URL中找到的内容的字符串表示形式。 – tstuts 2012-03-05 18:53:46