2012-09-20 20 views
0

這是函數:當HtmlAgilityPack.Document嘗試加載以exe結尾的鏈接時,我該怎麼辦?

private static HtmlAgilityPack.HtmlDocument getHtmlDocumentWebClient(string url, bool useProxy, string proxyIp, int proxyPort, string usename, string password) 
     { 
      HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); 
      WebClient client = new WebClient(); 
      //client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"); 
      client.Credentials = CredentialCache.DefaultCredentials; 
      client.Proxy = WebRequest.DefaultWebProxy; 
      if (useProxy) 
      { 
       //Proxy     
       if (!string.IsNullOrEmpty(proxyIp)) 
       { 
        WebProxy p = new WebProxy(proxyIp, proxyPort); 
        if (!string.IsNullOrEmpty(usename)) 
        { 
         if (password == null) 
          password = string.Empty; 
         NetworkCredential nc = new NetworkCredential(usename, password); 
         p.Credentials = nc; 
        } 
       } 
      } 
      Stream data = client.OpenRead(url); 
      doc.Load(data); 
      data.Close(); 
      return doc; 
     } 

即時得到鏈接在我的計劃,並數次後可變網址的每個itertion是:

http://appldnld.apple.com/iTunes10/041-7196.20120912.Ber43/iTunesSetup.exe 

如果我mtrying在我的InternetExplorer這個鏈接,它會嘗試下載文件。 但在我的程序中它試圖加載它在行中:

doc.Load(data);

至極做了一段時間後,程序凍結卡住,在當我武力結束在任務管理器應用程序的結束程序扔給我一個例外:

StackOverFlowException was unhandled 

An unhandled exception of type 'System.StackOverflowException' occurred in HtmlAgilityPack.dll 

System.StackOverflowException was unhandled 
Message: An unhandled exception of type 'System.StackOverflowException' occurred in HtmlAgilityPack.dll 

現在我使用斷點和問題發生在線上:

doc.Load(data); 

問題是我應該如何處理這種鏈接的情況?我應該忽略他們的嘗試和趕上或者我應該把這視爲一個鏈接?如果將來有時我會想用這個鏈接下載exe文件,所以也許嘗試和ctach不是一個好主意?


編輯:

這是getHtmlDocumentWebClient怎麼看起來像現在:

private static HtmlAgilityPack.HtmlDocument getHtmlDocumentWebClient(string url, bool useProxy, string proxyIp, int proxyPort, string usename, string password) 
     { 

      HttpWebRequest myHttpWebRequest = null;  //Declare an HTTP-specific implementation of the WebRequest class. 
      HttpWebResponse myHttpWebResponse = null; //Declare an HTTP-specific implementation of the WebResponse class 
      //Create Request 
      myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url); 
      myHttpWebRequest.Method = "GET"; 
      myHttpWebRequest.ContentType = "text/html; encoding='utf-8'"; 
      //Get Response 
      myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); 

      HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); 

      Stream data = myHttpWebResponse.GetResponseStream();//client.OpenRead(url); 
      doc.Load(data); 
      data.Close(); 
      return doc; 
     } 

同樣的問題呢。這個函數現在有什麼問題,我如何做text/html內容的實際檢查/ s?

回答

1

在嘗試將響應解析爲HTML之前,您應該檢查Content-Type
如果它不是text/html或它的一個變體,請不要解析它。

要獲取內容類型,您需要使用HttpWebRequest而不是WebClient
然後您可以檢查response.Headers

+0

剛剛更新了我的問題。不知道現在該做什麼,如果我迄今爲止做得很好。試圖使用HttpWebRequest。 –

相關問題