簡體   English   中英

使用JSoup通過用戶名/密碼連接到網站

[英]Using JSoup to connect to a website with username/password

我正在嘗試使用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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM