HtmlAgilityPack登录后 [英] HtmlAgilityPack Post Login

查看:234
本文介绍了HtmlAgilityPack登录后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图登录到使用HtmlAgilityPack( http://htmlagilitypack.codeplex.com/ )的网站。

I'm trying to login to a site using HtmlAgilityPack ( http://htmlagilitypack.codeplex.com/ ).

现在,我无法确切弄清楚如何去这件事。

Now, I can't exactly figure out how to go about this.

我曾尝试透过

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



然后我和

I then submit the form with

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

这不,虽然工作。它不是在或任何记录。没有任何人有任何其他的见解?

This isn't working though. It's not logging in or anything. Does anyone else have any other insight?

感谢您

推荐答案

该HTML敏捷性包是用来解析HTML - 你不能用它来提交表单。你的第一行代码改变解析的节点在内存中。第二行不会将网页发布到服务器 - 它再次加载DOM的,但是使用POST方法而不是默认的GET

The HTML Agility Pack is used to parse HTML - you cannot use it to submit forms. Your first line of code changes the parsed nodes in memory. The second line does not post the page to the server - it loads the DOM again, but using the POST method instead of the default GET.

它看起来并不像需要在这一点上在所有解析页面,因为你已经知道控制的名称。使用 的HttpWebRequest 类发送POST请求到服务器,与字符串电子邮件=#ACB example.com 中的要求。

It doesn't look like you need to parse the page at all at this point, since you already know the name of the control. Use the HttpWebRequest class to send a post request to the server, with the string email=acb#example.com in the request.

下面是当我需要类似的东西我写了一个例子:

Here's a sample I wrote when I needed something similar:

/// <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
}

这篇关于HtmlAgilityPack登录后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆