我需要能夠登錄到一個網站。最好能夠將登錄憑證保存在Cookie中。之後,我應該能夠瀏覽到網站,而在任何時候都能夠獲得htmldocument。需要能夠登錄和瀏覽網站從網站獲取htmlDocument
我已經在使用Html Agility Pack解析網站。如果我只是可以使用登錄憑據加載HtmlDocument,但是目前尚未能夠滿足,我們將感到滿意。我已經嘗試了HtmlWeb和HttpWebRequest。
var client = new WebClient();
client.Credentials = new NetworkCredential("myemail", "mypassword");
HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
// There are various options, set as needed
htmlDoc.OptionFixNestedTags=true;
HtmlWeb hw = new HtmlWeb();
WebProxy prox = new WebProxy(url);
try{
htmlDoc = hw.Load(url, "POST", prox, new NetworkCredential("myEMAIL", "mYPAssword"));
}catch(HtmlWebException ex){
Console.WriteLine("htmlParser.cs " + ex.Message);
}
它只是不會登錄。沒有錯誤消息。
try{
var temp = new Uri(url);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(temp);
request.Credentials = new NetworkCredential("myUsername", "myPassword");
request.CookieContainer = myContainer;
request.PreAuthenticate = true;
request.Method = "POST";
using (var response = (HttpWebResponse)request.GetResponse())
{
using (var stream = response.GetResponseStream())
{
htmlDoc.Load(stream, Encoding.GetEncoding("UTF-8"));
}
}
}
「我試過了」那你爲什麼不顯示你的代碼?請將此添加到您的問題中,並解釋它有什麼問題。 – mason