웹 페이지를 다운로드하려고합니다.
string remoteUri = "http://whois.domaintools.com/94.100.179.159";
WebClient myWebClient = new WebClient();
byte[] myDataBuffer = myWebClient.DownloadData(remoteUri);
string download = Encoding.ASCII.GetString(myDataBuffer);
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(download);
doc.Save("file1.htm");
오류 있음
웹 예외가 처리되지 않았습니다. (403) 금지되었습니다.
페이지를 다운로드 할 수있는 다른 방법이 있습니까? HtmlDocument 클래스를 시도했지만 브라우저에서로드 된 웹 페이지가 필요하다는 것을 알 수 있습니다.
HtmlWeb hwObject = new HtmlWeb();
string ip = "http://whois.domaintools.com/";
HtmlAgilityPack.HtmlDocument htmldocObject = hwObject.Load(ip);
foreach (HtmlNode link in htmldocObject.DocumentNode.SelectNodes("//meta[@name = 'description']"))
{
...
}
using (var myWebClient = new WebClient())
{
myWebClient.Headers["User-Agent"] = "MOZILLA/5.0 (WINDOWS NT 6.1; WOW64) APPLEWEBKIT/537.1 (KHTML, LIKE GECKO) CHROME/21.0.1180.75 SAFARI/537.1";
string page = myWebClient.DownloadString("http://whois.domaintools.com/94.100.179.159");
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(page);
}
요청에서 사용자 에이전트가 발견되지 않으면이 사이트는 단순히 오류를 반환합니다. 작업 코드는 다음과 같습니다.
string remoteUri = "http://whois.domaintools.com/94.100.179.159";
HtmlDocument doc = new HtmlDocument();
using (WebClient myWebClient = new WebClient())
{
myWebClient.Headers.Add(HttpRequestHeader.UserAgent, "some browser user agent");
doc.Load(myWebClient.OpenRead(remoteUri));
}
doc.Save("file1.htm");
또는 HtmlWeb
을 사용하려는 경우
HtmlWeb hwObject = new HtmlWeb();
hwObject.UserAgent = "some browser user agent";
//more code...