使用C#模拟对VBulletin的登录操作

时间:2010-03-16 11:19:01

标签: c# post vbulletin

我尝试编写一个可以登录并在VBulletin论坛中创建新线程的程序(C#)。我试过两种方式:

1)使用 HttpWebRequest :登录完成。但是,创建新线程失败。这是发布代码:

public static void CreateNewThread(string url,string fId, string title, string message, string tag)
    {
        url += "newthread.php?do=postthread";

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
        //string result = "";

        string values = "subject=" + title
                        + "&message=" + message
                        + "&tag=" + tag
                        + "&do=postthread"
                        + "&f=" + fId
                        + "&s="
                        + ""
                        ;

        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";
        req.ContentLength = values.Length;

        ServicePointManager.Expect100Continue = false; // prevents 417 error

        using (StreamWriter writer = new StreamWriter(req.GetRequestStream(), Encoding.UTF8))
        {
            writer.Write(values);
        }

        HttpWebResponse c = (HttpWebResponse)req.GetResponse();
    }

当执行上面的代码时,没有创建任何理论!

2)使用WebBrowser控件:

 webBrowser1.Document.GetElementById("navbar_username").InnerText = "admin";
 webBrowser1.Document.GetElementById("navbar_password").InnerText = "123";

但我不能提交因为没有名字/ ID,而且登录按钮是一样的!请告诉我如何提交没有表格名称/ ID和按钮名称/ ID的表格?

谢谢!

最好的考虑,

1 个答案:

答案 0 :(得分:0)

尝试模拟帖子数据而不是填写表格:

string postData = "username=Kurresmack&password=pw&action=login&url=/";
webBrowser1.Navigate("www.sweclockers.com/forum/member.php", "", System.Text.Encoding.UTF8.GetBytes(postData), "Content-Type: application/x-www-form-urlencoded\r\n");