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....

1
  • i am also face same problem now how you solve this issue Commented Jun 21, 2013 at 13:24

3 Answers 3

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.
Sign up to request clarification or add additional context in comments.

11 Comments

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")<br/> .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 ?
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.
Hey i have tried removing the referer , still the same !! what do you mean by private ?
I mean if you want to tell the site which you want to login to.
I dont think you are doing any wrong.I have done login through jsoup on facebook.Jsoup has its limitations.Cant work on Javascript.
|
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.

Comments

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();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.