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 ?

exussum
  • 18,275
  • 8
  • 32
  • 65

2 Answers2

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.

Shoshi
  • 2,254
  • 1
  • 29
  • 43
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.

iamvinitk
  • 165
  • 3
  • 15