2012-09-20 3 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; 
     } 

임 링크를 변수 URL 내 프로그램과 몇 번 한 후 각 itertion입니다 점점 ​​:

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

내가 그것을 위해 노력할 것입니다 내 인터넷 익스플로러에서이 링크를 mtrying 경우 파일을 다운로드하십시오. 하지만 내 프로그램에서 라인에로드하려고 :

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는 좋은 생각이 아니겠습니까?


편집 됨 :

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

같은 문제를 아직 :

이것은 getHtmlDocumentWebClient 지금처럼 보이게하는 방법입니다. 뭐가 문제가 지금 기능과 내가 어떻게/text/html 콘텐츠에 대한 실제 검사를합니까?

답변

1

응답을 HTML로 구문 분석하기 전에 Content-Type을 확인해야합니다.
text/html 또는 해당 변형 중 하나가 아닌 경우이를 구문 분석하지 마십시오.

Content-Type을 얻으려면 WebClient 대신 HttpWebRequest을 사용해야합니다.
response.Headers을 확인할 수 있습니다.

+0

지금 내 질문이 업데이트되었습니다. 지금 무엇을해야할지 모르겠다. 만약 내가 지금까지했던 것이 좋다면. HttpWebRequest를 사용하려고했습니다. –

관련 문제