27

I am trying to POST data into website to make a login into the site using Jsoup , but its not working ?

I am trying the code

    Document docs = Jsoup.connect("http://some.com/login")
        .data("cmd", "login","username", "xxxx","password", "yyyyy")
        .referrer("http://some.com/login/").post();

here it is giving normal page of login in pagesource

i have also tried the code

 Document docs = (Document) Jsoup.connect("http://some.com/login")
    .data("cmd", "login","username", "xxxx","password", "yyyyy")
    .referrer("http://some.com/login/").method(Method.POST).execute().parse();

here also it is giving normal page of login again in pagesource.

Any suggestions regarding the same would be highly appreciated !!

Thanks....

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Aspirant
  • 2,238
  • 9
  • 31
  • 43

3 Answers3

34

I will give the answer of your question by taking an example. Suppose you want to login to facebook.

Then apart from username and password there are many other parameters that are also passed through POST request. Those all parameters are hidden and are passed similarly like username and password. For Example :

If you will open the html source of facebook , then you can see there is one parameter which is hidden is lgnrnd and its value is 071129_5D7M.

So there are many other parameter similar like this.You need to pass all the parameters. You should also specify the userAgent.

Document doc = Jsoup.connect("http://www.facebook.com")
.data("email", "myemailid")
.data("pass", "mypassword")
// and other hidden fields which are being passed in post request.
.userAgent("Mozilla")
.post();
System.out.println(doc); // will print html source of homepage of facebook.
ThisClark
  • 14,352
  • 10
  • 69
  • 100
vikiiii
  • 9,246
  • 9
  • 49
  • 68
  • i have cross checked for what are the values passed while post using Live Http headers , and found nothing other than `cmd=login&username=xxxx&password=yyyyy` and i am trying the code `Document docs = Jsoup.connect("http://some.com/login")
    .data("cmd", "login") .data("username", "xxxx") .data("password","yyyy") .userAgent("Mozilla") .referrer("http://some.com/login/") .post();` still its not giving the pagesource of the homepage in the docs !! rather its giving again the login page !! is there anything wrong in the coding ?
    – Aspirant Apr 05 '12 at 05:09
  • 1
    dont use referrer and try.If the website you want to login is not private, then you can tell me,I can be more helpful. – vikiiii Apr 05 '12 at 05:13
  • Hey i have tried removing the referer , still the same !! what do you mean by private ? – Aspirant Apr 05 '12 at 05:18
  • I mean if you want to tell the site which you want to login to. – vikiiii Apr 05 '12 at 05:20
  • i can't get you i have added the data("cmd","login") in my previous code only !! – Aspirant Apr 05 '12 at 05:30
  • Sorry i forgot.Now the code is perfect.I dont know why it is not able to login.I will register and let you know. – vikiiii Apr 05 '12 at 05:35
  • seems like chat is a bad option !! can you let me know where it went wrong ? – Aspirant Apr 05 '12 at 06:16
  • 2
    I dont think you are doing any wrong.I have done login through jsoup on facebook.Jsoup has its limitations.Cant work on Javascript. – vikiiii Apr 05 '12 at 06:27
  • so Jsoup can't work on java script !! OK , can you please tell me how to configure proxy in Jsoup ? – Aspirant Apr 05 '12 at 06:34
  • no idea about that.You can have a look at this.http://stackoverflow.com/questions/7482748/how-to-add-proxy-support-to-jsoup-html-parser – vikiiii Apr 05 '12 at 06:39
  • @Aspirant : i think we are doing a mistake here.When we want to login, then the parameter in **Jsoup.connect("link of home page not login page ")** . Did you understand what i am saying? – vikiiii Apr 11 '12 at 03:02
2

If the issue is a javascript redirect, you could try going into the javascript and checking if the URL it redirects to is static, and then use the redirection to gain access. I did that to access a popup box made by javascript once.

Ethan
  • 1,206
  • 3
  • 21
  • 39
0

Post data can be sent using the Map as well. Looks more managed and clean. Sometime the websites checks the presence of some headers so pass those headers to make the request as similar as it should be. Most of the time the content-type is expected.

package test;

import java.util.HashMap;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

/**
 * POST example
 * 
 * @author iampayload
 *
 */
public class JsoupPost {

    private final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:65.0) Gecko/20100101 Firefox/65.0";

    private final String urlPost = "https://www.huawei.com/en/accounts/PersonalPost";

    // main class
    public static void main(String[] args) throws Exception {
    JsoupPost http = new JsoupPost();
    http.sendPost();
    }

    // HTTP Post request
    private void sendPost() throws Exception {

    Map<String, String> postData = new HashMap<>();
    postData.put("username", "xxxx");
    postData.put("cmd", "login");
    postData.put("password", "yyyyy");

    Document doc = Jsoup.connect(urlPost).ignoreContentType(true).userAgent(USER_AGENT).data(postData).post();
    }
}
Manasi
  • 13
  • 4