HtmlAgilityPack发布登录

Sty*_*les 17 c# login html-agility-pack

我正在尝试使用HtmlAgilityPack登录网站(网站:http://html-agility-pack.net).

现在,我无法弄清楚如何解决这个问题.

我试过通过设置Html表单值

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

然后我提交表格

m_HtmlWeb.Load("http://example.com/", "POST");
Run Code Online (Sandbox Code Playgroud)

这不行.它没有登录或任何东西.还有其他人有其他见解吗?

谢谢

Kob*_*obi 19

HTML Agility Pack用于解析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
}
Run Code Online (Sandbox Code Playgroud)

  • 此外,如果您要下载和解析登录页面之外的页面,您可能必须在后续请求中从登录响应中传递收到的cookie.有关详细信息,请参阅[此问题+答案](http://stackoverflow.com/questions/571964/automatic-cookie-handling-c-net-httpwebrequesthttpwebresponse). (2认同)

小智 5

如果要使用HTML Agility Pack进行此操作。这是代码。

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");
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

22343 次

最近记录:

8 年 前