HtmlAgilityPack Post Login

本文关键字:Login Post HtmlAgilityPack | 更新日期: 2023-09-27 18:00:58

我正试图使用HtmlAgilityPack登录到一个站点(站点:http://html-agility-pack.net)。

现在,我不知道该怎么做。

我尝试过通过设置Html表单值

m_HtmlDoc.DocumentNode.SelectSingleNode("//input[@name='EMAIL']").SetAttributeValue("value", "myemail.com");

然后我用提交表格

m_HtmlWeb.Load("http://example.com/", "POST");

但这不起作用。它没有登录或其他什么。其他人有其他见解吗?

感谢

HtmlAgilityPack Post Login

HTML敏捷包用于解析HTML-您不能使用它来提交表单。您的第一行代码更改了内存中已解析的节点。第二行没有将页面发布到服务器——它再次加载DOM,但使用post方法而不是默认的GET。

此时似乎根本不需要解析页面,因为您已经知道控件的名称。使用HttpWebRequest类向服务器发送post请求,请求中包含字符串email=acb#example.com

这是我在需要类似东西时写的一个样本:

/// <summary>
/// Append a url parameter to a string builder, url-encodes the value
/// </summary>
/// <param name="sb"></param>
/// <param name="name"></param>
/// <param name="value"></param>
protected void AppendParameter(StringBuilder sb, string name, string value)
{
    string encodedValue = HttpUtility.UrlEncode(value);
    sb.AppendFormat("{0}={1}&", name, encodedValue);
}
private void SendDataToService()
{
    StringBuilder sb = new StringBuilder();
    AppendParameter(sb, "email", "hello@example.com");
    byte[] byteArray = Encoding.UTF8.GetBytes(sb.ToString());
    string url = "http://example.com/"; //or: check where the form goes
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    //request.Credentials = CredentialCache.DefaultNetworkCredentials; // ??
    using (Stream requestStream = request.GetRequestStream())
    {
        requestStream.Write(byteArray, 0, byteArray.Length);
    }
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    // do something with response
}

如果您想用Html敏捷包来实现这一点。这是代码。

CookieCollection Cookies = new CookieCollection();
            var web = new HtmlWeb();
            web.OverrideEncoding = Encoding.Default;
            web.UseCookies = true;
            web.PreRequest += (request) =>
            {
                if (request.Method == "POST")
                {
                    string payload = request.Address.Query;
                    byte[] buff = Encoding.UTF8.GetBytes(payload.ToCharArray());
                    request.ContentLength = buff.Length;
                    request.ContentType = "application/x-www-form-urlencoded";
                    System.IO.Stream reqStream = request.GetRequestStream();
                    reqStream.Write(buff, 0, buff.Length);
                }
                request.CookieContainer.Add(Cookies);
                return true;
            };
            web.PostResponse += (request, response) =>
            {
                if (request.CookieContainer.Count > 0 || response.Cookies.Count > 0)
                {
                    Cookies.Add(response.Cookies);
                }
            };
            string baseUrl = "Your Website URL";
            string urlToHit = baseUrl + "?QueryString with Login Credentials";
            HtmlDocument doc = web.Load(urlToHit, "POST");

我花了几个小时讨论这个主题,实际上找到了一个非常简单的解决方案。

我有:

.net核心1.1.2

HtmlAgilityPack 1.4.9.5

login url登录:"www.url.com/login"。

urlData的url:"www.url.com/data/3"=>要获得此信息,您应该连接。

以下是我所做的,它确实奏效了:

HttpClient hc = new HttpClient();
HttpResponseMessage resultLogin = await hc.PostAsync(urlLogin, new StringContent("login=myUserName&password=myPaswordValue", Encoding.UTF8, "application/x-www-form-urlencoded"));
HttpResponseMessage resultPlaylist = await hc.GetAsync(urlData);
Stream stream = await resultPlaylist.Content.ReadAsStreamAsync();
HtmlDocument doc = new HtmlDocument();
doc.Load(stream);
string webContent = doc.DocumentNode.InnerHtml;  => it works

我认为它需要首先登录你的HttpClient,然后你才能发送你想要的请求。

享受