3

Im struggling getting java submitting POST requests over HTTPS

Code used is here

     try{
        Response res = Jsoup.connect(LOGIN_URL)
    .data("username", "blah", "password", "blah")

    .method(Method.POST)
  .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0")
                .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
                .execute();
        System.out.println(res.body());
        System.out.println("Code " +res.statusCode());

        }
        catch (Exception e){
            System.out.println(e.getMessage()); 
        }

and also this

Document doc = Jsoup.connect(LOGIN_URL)
  .data("username", "blah")
  .data("password", "blah")
  .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0")
        .header("Content-type", "application/x-www-form-urlencoded")
         .method(Method.POST)
  .timeout(3000)
  .post();
            

Where LOGIN_URL = https://xxx.com/Login?val=login

When used over HTTP it seems to work, HTTPS it doesnt, But doesnt throw any exceptions

How can I POST over HTTPS

Edit:

seems there is a 302 redirect involved when the server gets a POST over HTTPS (which doesnt happen over http) How can I use jsoup to store the cookie sent with the 302 to the next page ?

14
  • Can you check the server log? Likely it's a problem with the server certificate. You can try to import it and see if is solves your problem Commented Mar 4, 2013 at 20:42
  • Added the key and put this in the code System.setProperty("javax.net.ssl.trustStore", "sitename.jks"); still not working as expected Commented Mar 4, 2013 at 20:53
  • making similar code in PHP, Cookies between requests need to be handled (302 redirect after the post) How do i enable this in jsoup ? Commented Mar 4, 2013 at 20:57
  • 1
    Use followRedirects before execute() Commented Mar 4, 2013 at 21:12
  • 1
    i have faced the same issue. then i have used HttpURLConnection and it works. if u want to use HttpURLConnection i can show u what i have done. Commented Mar 4, 2013 at 23:55

2 Answers 2

2

this is my code:

URL form = new URL(Your_url);
connection1 = (HttpURLConnection)form.openConnection();
connection1.setRequestProperty("Cookie", your_cookie);

connection1.setReadTimeout(10000);
StringBuilder whole = new StringBuilder();

BufferedReader in = new BufferedReader(
        new InputStreamReader(new BufferedInputStream(connection1.getInputStream())));
String inputLine;
while ((inputLine = in.readLine()) != null)
     whole.append(inputLine);
     in.close();
Document doc = Jsoup.parse(whole.toString());
String title = doc.title();

i have used this code to get the title of the new page.

Sign up to request clarification or add additional context in comments.

Comments

0

Here is what you can try...

import org.jsoup.Connection;


Connection.Response res = null;
    try {
        res = Jsoup
                .connect("your-first-page-link")
                .data("username", "blah", "password", "blah")
                .method(Connection.Method.POST)
                .execute();
    } catch (IOException e) {
        e.printStackTrace();
    }

Now save all your cookies and make a request to the other page you want.

//Saving Cookies
cookies = res.cookies();

Making a request to another page.

try {
    Document doc = Jsoup.connect("your-second-page-link").cookies(cookies).get();
}
catch(Exception e){
    e.printStackTrace();
}

Comment if further help needed.

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.