2013-10-18 95 views
0

我們遇到了一個奇怪的問題。直到昨天,多年來我們在使用一小塊代碼在我們的應用程序,以訪問特定的網址,以檢查是否一個特定的文件中存在:WebRequest.GetResponse不會返回404錯誤

public static bool IsUpdateAvailable() 
    { 
     System.Net.WebRequest webRequest = System.Net.WebRequest.Create("http://site/updatefile.exe"); 
     System.Net.WebResponse webResponse; 

     try 
     { 
      webResponse = webRequest.GetResponse(); 
     } 
     catch (System.Net.WebException e) //If WebException exception thrown then couldn't get response from address 
     { 
      Console.WriteLine("This program is throw a WebException."+ 
         "\n\nException Message :" + e.Message); 
       if(e.Status == System.Net.WebExceptionStatus.ProtocolError) return false; 
     } 
     catch (Exception e) //If general exception thrown then couldn't get response from address 
     { 
      return false; 
     } 

     return true; 
    } 

從昨天上面的代碼將停止返回404錯誤如果檢查的文件或URL不存在,並因此始終返回true。 我們無法從c#視圖中解釋發生了什麼。任何幫助將不勝感激。

回答

1

捕獲WebException並從中你將能夠恢復WebResponse Response,投入到HttpWebResponse。你會得到你期望的StatusCode。