0

我在尝试使用 jsoup 登录时遇到问题。我不确定这个是否有任何 cookie?我错过了什么吗?为什么我不能登录?

import java.io.IOException;
import java.util.Map;

import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

public class LoginJtest {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        // Connect to page and parse html into a 'Document'


        //This will get you the response.
        Response res = Jsoup
            .connect("https://pslweb01.ciq.labs.att.com:8080/dis/login.jsp")
            .data("txtUserName", "myusername", "txtPassword", "mypassword")
            .method(Method.POST)
            .execute();

        //This will get you cookies
        Map<String, String> loginCookies = res.cookies();

        //And this is the easiest way I've found to remain in session
        Document doc = Jsoup.connect("https://pslweb01.ciq.labs.att.com:8080/dis/")
              .cookies(loginCookies)
              .get();
           String title = doc.title();
       System.out.println(title);


    }
}

这是我尝试访问的登录网站的来源。

<div class="loginPanel">
    <div class="container">
    <div class="title">Login</div>
    <form id="loginForm" class="loginForm" method="POST" action="/dis/login">
        <font class="portlet-msg-error"
         style="font-weight: bold; font-size: 10px; color:#FF0000; text-align: center;"></font>
        <table>
        <tr>
            <td class="label">USERNAME:</td>
            <td><input
                value=""
                class="edit"
                id="txtUsernameLogin"
                name="txtUserName"
                type="text"/></td>
        </tr>
        <tr>
            <td class="label">PASSWORD:</td>
            <td><input
                value=""
                class="edit"
                name="txtPassword"
                type="password"/></td>
        </tr>
        <tr>
            <td colspan="2" align="center">
            <input type="checkbox" name="txtRemember" value="true" align="middle"/>
            Remember me on this computer
            </td>
        </tr>
        <tr>
            <td></td>
            <td class=""><input type="submit"
                    class="submit"
                    value="Login"
                    alt="Login"/></td>
        </tr>
        </table>
    </form>
    </div>
</div>
4

1 回答 1

0

看来您正在发布到 /dis/login.jsp 而表单发布到 /dis/login

这也可能是因为网站采用的安全功能。尝试在 Jsoup 中设置引用者、用户代理等。

于 2013-06-17T17:38:32.047 回答