2010-07-13 110 views
1

作为我论文论文的一部分,我构建了一个“机器人”,用于抓取C语言程序的官方论坛,并搜索常见问题以发布回复。我必须模拟登录才能发布相应的回复。登录形式如下:使用Java登录到vbulletin论坛

- 登录表单 - 形式的行动= “?login.php中做=登录” 方法= “邮报” 的onsubmit = “md5hash(vb_login_password,vb_login_md5password,vb_login_md5password_utf,0)” > script type =“text/javascript”src =“clientscript/vbulletin_md5.js?v = 385”> table cellpadding =“0”cellspacing =“3”border =“0”> tr> td class =“ smallfont”样式= “空白:NOWRAP;”>Όνομαχρήστη TD>

TD类= “smallfont” NOWRAP = “NOWRAP”>ΑυτόματηΣύνδεση /TR> TR> TD类= “smallfont”>Κωδικός TD> TD> /TR> /TABLE>

INPUT TYPE = “隐藏” 名称= “S” 值= “”/> INPUT TYPE = “隐藏”名称=“securitytoken”value =“guest”/> input type =“hidden”name =“do”value =“login”/> input type =“hidden”name =“vb_login_md5password”/> input type =隐藏”的名字=‘vb_login_md5password_utf’/> /FORM> ! - /登录窗体 - >

我已经明白,我需要MD5哈希密码,但我不能任何登录。我使用POST方法,我准备内容执行以下操作:

content = "do=login&url=login.php" + "&vb_login_md5password=" + md5_pass+ "&vb_login_md5password_utf="+ md5_pass + "&s=&securitytoken=guest&vb_login_username=" + UserName + "&vb_login_password=" + PassWprd; 

我再发做以下的角逐:

urlConnection = (HttpURLConnection)(new URL(targetUrl).openConnection()); 

     // Specifying that we intend to use this connection for input 
     urlConnection.setDoInput(true); 

     // Specifying that we intend to use this connection for output 
     urlConnection.setDoOutput(true); 

     // Specifying the method of HTTP request which is POST 
     // throws ProtocolException 
     urlConnection.setRequestMethod("POST"); 

     // Specifying the content type of our post 
     urlConnection.setRequestProperty("Content-Type", POST_CONTENT_TYPE); 
     urlConnection.setRequestProperty("Content-length",String.valueOf (content.length())); 
     // urlConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)"); 

     // Prepare an output stream for writing data to the HTTP connection 
     // throws IOException 
     dataOutputStream = new DataOutputStream(urlConnection.getOutputStream()); 
     dataOutputStream.writeBytes(content); 

尽管如此,我无法登录。我相信我发送的表单有问题,但我找不到。任何帮助将不胜感激,因为我必须在几天内完成该计划。

回答

0

我认为最好的办法是,如果你第一次尝试看到你的http请求等于浏览器在正常登录时创建的请求。

为此,您可以在Firefox(https://addons.mozilla.org/de/firefox/addon/3829/)中安装Live HTTP Header插件 - 这将允许您查看浏览器登录请求的样子。

在java方面,toString()方法被覆盖,并且应该显示完整的头文件afaik。如果没有,只需比较内容字符串并确保您确实设置了所有正确的标题。

+0

我做了你所说的,我找到了正确的表单和正确的POST登录链接,最后它工作。感谢您的帮助:P – fysob 2010-07-13 16:15:23

+0

@fysob您可以使用您的代码发布答案吗?我试图在vbulletin论坛上完成同样的事情。 – localhost 2014-11-25 05:12:48

1

我不会尝试伪造HTTP请求,而是建议您依赖虚拟Web客户端,如HtmlUnit。它允许你在更高的层次上进行操作,而不是试图用正确的元素伪造一个HTTP查询,你“只是”必须用正确的值填充表单。