이 코드를 사용하여 로그인합니다.
CookieCollection cookies = new CookieCollection();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("example.com");
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cookies = response.Cookies;
string getUrl = "example.com";
string postData = String.Format("my parameters");
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl);
getRequest.CookieContainer = new CookieContainer();
getRequest.CookieContainer.Add(cookies);
getRequest.Method = WebRequestMethods.Http.Post;
getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0";
getRequest.AllowWriteStreamBuffering = true;
getRequest.ProtocolVersion = HttpVersion.Version11;
getRequest.AllowAutoRedirect = true;
getRequest.ContentType = "application/x-www-form-urlencoded";
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
getRequest.ContentLength = byteArray.Length;
Stream newStream = getRequest.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
newStream.Close();
HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream(), Encoding.GetEncoding("windows-1251")))
{
doc.LoadHtml(sr.ReadToEnd());
webBrowser1.DocumentText = doc.DocumentNode.OuterHtml;
}
그런 다음 HtmlWeb (HtmlAgilityPack) 또는 Webclient를 사용하여 HTML을 HtmlDocument (HtmlAgilityPack)로 구문 분석하고 싶습니다.
내 문제는 내가 사용할 때 :
WebClient wc = new WebClient();
webBrowser1.DocumentText = wc.DownloadString(site);
또는
doc = web.Load(site);
webBrowser1.DocumentText = doc.DocumentNode.OuterHtml;
로그인이 사라져서 쿠키를 어떻게 든 넘겨야한다고 생각합니다. 어떤 제안이 있습니까?
HtmlAgilityPack.HtmlDocument 쿠키 확인
다음은 당신이 찾고있는 예제입니다 (구문은 100 % 테스트되지 않았고, 나는 보통 사용하는 클래스를 수정했습니다) :
public class MyWebClient
{
//The cookies will be here.
private CookieContainer _cookies = new CookieContainer();
//In case you need to clear the cookies
public void ClearCookies() {
_cookies = new CookieContainer();
}
public HtmlDocument GetPage(string url) {
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
//Set more parameters here...
//...
//This is the important part.
request.CookieContainer = _cookies;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
var stream = response.GetResponseStream();
//When you get the response from the website, the cookies will be stored
//automatically in "_cookies".
using (var reader = new StreamReader(stream)) {
string html = reader.ReadToEnd();
var doc = new HtmlDocument();
doc.LoadHtml(html);
return doc;
}
}
}
사용 방법은 다음과 같습니다.
var client = new MyWebClient();
HtmlDocument doc = client.GetPage("http://somepage.com");
//This request will be sent with the cookies obtained from the page
doc = client.GetPage("http://somepage.com/another-page");
참고 : 또한 POST
메서드를 사용하려면 POST
로직, 리팩터 또는 클래스 등으로 GetPage
와 비슷한 메서드를 만듭니다.
몇 가지 권장 사항이 있습니다. WebClient 클래스와 함께 CookieContainer 사용
그러나 HttpWebRequest
를 계속 사용하고 CookieContainer
에서 쿠키를 설정하는 것이 더 쉽습니다.
코드는 다음과 같습니다.
// Create a HttpWebRequest
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(getUrl);
// Create the cookie container and add a cookie
request.CookieContainer = new CookieContainer();
// Add all the cookies
foreach (Cookie cookie in response.Cookies)
{
request.CookieContainer.Add(cookie);
}
두 번째로 이미 웹 응답에서 가져온 것이므로 여기에서 사이트를 다시 다운로드 할 필요가 없습니다.
HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream(), Encoding.GetEncoding("windows-1251")))
{
webBrowser1.DocumentText = doc.DocumentNode.OuterHtml;
}
HTML을 가져 와서 HTML Agility Pack으로 파싱 할 수 있어야합니다.
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(webBrowser1.DocumentText);
그리고 그걸해야합니다 ... :)