使用 JSoup 连接到具有用户名/密码的网站



我正在尝试使用JSoup连接到网站。不幸的是,当我尝试建立连接时,我收到错误。

ADVFN 错误 - 找不到页面

我正在尝试提交的表格。

<form action="https://secure.advfn.com/login/secure" id="login_form" name="login_form" method="POST" target="">
<input type="hidden" value="aHR0cHM6Ly91ay5hZHZmbi5jb20vcC5waHA/cGlkPWxvZ291dA==" name="redirect_url" id="redirect_url">
<input type="hidden" value="uk" name="site" id="site">
<div class="fields">
<label for="login_username">Username</label> 
<input type="text" tabindex="1" class="text ui-widget-content" value ="tabhair"
id="login_username" name="login_username" maxlength="64">
</div>
<div class="fields">
<label for="login_password">Password</label> 
<input tabindex="2" type="password" class="text ui-widget-content" value="" id="login_password" name="login_password" maxlength="16">
</div>
<div class="fields lost-pass">
<strong><a href="/common/account/password/request">Forgotten password?</a></strong> &nbsp;
<input  class="button"  tabindex="3" type="submit"   value="Log In" id="login_submit">
</div>
</form> 

我的代码,用户名和密码已更改。

Connection.Response loginForm = Jsoup.connect("http://www.advfn.com")
.userAgent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)").method(Connection.Method.GET)
.execute();
Map<String, String> cookies = loginForm.cookies();
Map<String, String> mapParams = new HashMap<String, String>();
mapParams.put("login_username", "blah");
mapParams.put("login_password", "blah");
mapParams.put("site", "uk");
mapParams.put("redirect_url", "aHR0cHM6Ly91ay5hZHZmbi5jb20vcC5waHA/cGlkPW1lc3NhZ2UmbXNnPTY=");
Document document = Jsoup.connect("https://uk.advfn.com/forum")
.referrer("https://secure.advfn.com/login/secure")
.cookies(cookies)
.data(mapParams)
.userAgent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)")
.timeout(1000)
.ignoreHttpErrors(true)
.followRedirects(true).post();
System.out.println(document);

我的请求中是否缺少输入字段,或者我没有在代码中编写某些操作?

我尝试对 Htmlunit 做同样的事情,它以更简单的方式处理以下代码。

WebClient webClient = new WebClient();
try {
HtmlPage page = (HtmlPage) webClient.getPage("https://uk.advfn.com/forum/search?q=login&index=posts&post_poster=on");
HtmlForm form = page.getFormByName("login_form");
form.getInputByName("login_username").setValueAttribute("yourusername");
HtmlInput passWordInput = form.getInputByName("login_password");
passWordInput.removeAttribute("disabled");
passWordInput.setValueAttribute("yourpassword");
page = form.getInputByValue("Log In").click(); 
System.out.println(page.asText());
} catch (Exception e) {
e.printStackTrace();
} finally {
webClient.close();
}

最新更新