Java 使用JSoup以用户名/密码连接到网站

Java 使用JSoup以用户名/密码连接到网站,java,jsoup,Java,Jsoup,我正在尝试使用JSoup连接到一个网站。不幸的是,我在尝试建立连接时出错 ADVFN错误-找不到页面 我要提交的表格 <form action="https://secure.advfn.com/login/secure" id="login_form" name="login_form" method="POST" target=""> <input type="hidden" value="aHR0cHM6Ly91ay5hZHZmbi5jb

我正在尝试使用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);
Connection.Response loginForm=Jsoup.connect(“http://www.advfn.com")
.userAgent(“Mozilla/4.0(兼容;MSIE 6.0;Windows NT 5.1)”).method(Connection.method.GET)
.execute();
映射cookies=loginForm.cookies();
Map mapParams=新HashMap();
mapParams.put(“登录用户名”等等);
mapParams.put(“登录密码”等等);
mapParams.put(“现场”、“英国”);
mapParams.put(“重定向url”,“aHR0cHM6Ly91ay5hZHZmbi5jb20vcC5waHA/cGlkPW1lc3NhZ2UmbXNnPTY=”);
Document Document=Jsoup.connect(“https://uk.advfn.com/forum")
.推荐人(”https://secure.advfn.com/login/secure")
.饼干(饼干)
.数据(mapParams)
.userAgent(“Mozilla/4.0(兼容;MSIE 6.0;Windows NT 5.1)”
.超时(1000)
.ignoreHttpErrors(真)
.followRedirects(true.post();
系统输出打印项次(文件);

我的请求中是否缺少输入字段,或者我是否没有在代码中编写一些东西

我试着用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();
}

尝试转到()表示找不到页面。我想那将是你的问题。登录表单不存在。也尝试了,没有骰子。您尝试登录的表单也不存在。这就是它不起作用的原因。请在连接中尝试此链接,它实际上是登录表单。