Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsoup posting Java

Tags:

java

jsoup

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 ?

like image 492
exussum Avatar asked Mar 22 '26 03:03

exussum


1 Answers

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.

like image 148
Shoshi Avatar answered Mar 23 '26 17:03

Shoshi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!