Mahen Ratnayake Mahen Ratnayake - 12 days ago 6
Java Question

Trying to Log In to a site using Jsoup and nothing works

I've been trying to log into a site using Jsoup, and been trying every suggestion on every thread on here and nothing seems to work.

public static void main(String[] args) throws Exception {

Connection.Response loginForm = Jsoup.connect("http://webspace.apiit.lk/index.jsp")
.method(Connection.Method.GET)
.execute();

Response res = Jsoup.connect("http://webspace.apiit.lk/index.jsp")
.data("UserID", "cb004277")
.data("Password", " ")
//.data("Submit", "Log In")
.cookies(loginForm.cookies())
.method(Method.POST)
.execute();

Map<String, String> cookies = res.cookies();

Document doc = Jsoup.connect("http://webspace.apiit.lk/index.jsp").cookies(cookies).get();

System.out.println(doc);


Could someone take a look and let me know what I'm doing wrong?

Answer

From the java docs

timeout

Connection timeout(int millis) Set the request timeouts (connect and read). If a timeout occurs, an IOException will be thrown. The default timeout is 3 seconds (3000 millis). A timeout of zero is treated as an infinite timeout. Parameters: millis - number of milliseconds (thousandths of a second) before timing out connects or reads. Returns: this Connection, for chaining

I tried to visit the page and it does take far too long to load. Try this

public static void main(String[] args) throws Exception {

   Connection.Response loginForm = Jsoup.connect("http://webspace.apiit.lk/index.jsp")
        .timeout(0)  //0 means infinite
        .method(Connection.Method.GET)
        .execute();

   Response res = Jsoup.connect("http://webspace.apiit.lk/index.jsp")
        .timeout(0)  //0 means infinite
        .data("UserID", "cb004277")
        .data("Password", " ")
        //.data("Submit", "Log In")
        .cookies(loginForm.cookies())
        .method(Method.POST)
        .execute();

   Map<String, String> cookies = res.cookies();

   Document doc = Jsoup.connect("http://webspace.apiit.lk/index.jsp").cookies(cookies).get();

   System.out.println(doc);
}
Comments