Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: sending password form post doesn't work

  1. #1
    Junior Member
    Join Date
    Nov 2019
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default sending password form post doesn't work

    There is a website from which I want to access the data in my Java program. When opening the site in a browser, instead of the content I want to see, there is a text field for the password and a button for submitting it. After typing in the correct password and pressing the submit button, I can see the content. The URL is still the same. Normally I can get the html data using code like this:

            URL url = new URL("https://wordpress.***.***/***/***/***/");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
     
            var reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
     
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

    Of course this won't print out the data I want to access. The interesting part is probably this one:

        <form action="https://wordpress.***.***/***/wp-login.php?action=postpass" class="post-password-form" method="post">
    <p>Some text:</p>
    <p><label for="pwbox-526">Passwort: <input name="post_password" id="pwbox-526" type="password" size="20" /></label> <input type="submit" name="Submit" value="Absenden" /></p>
    </form>

    After researching a bit about sending form posts in Java, I came up with this code, though now it doesn't print anything:

            URL url = new URL("https://wordpress.***.***/***/wp-login.php?action=postpass");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setDoInput(true);
     
            String postData = "post_password=*******&Submit=Absenden";
     
            connection.setRequestProperty("Content-Length", postData.length() + "");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
     
            connection.setUseCaches(false);
            connection.connect();
     
            var out = connection.getOutputStream();
            out.write(URLEncoder.encode(postData, StandardCharsets.UTF_8).getBytes());
            out.flush();
            out.close();
     
            var reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
     
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

    This is what the network panel looks like in the Microsoft Edge Developer Tools
    image.jpg
    Does anyone have a solution? This is the first time I'm working with networks in a Java project. I need it for an android app btw.

    Solved:
    Needed to use HttpClient and change the request header a bit
    Last edited by Orbit; November 6th, 2019 at 01:57 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: sending password form post doesn't work

    What response do you get from the server when you run that code with the login attempt?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2019
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: sending password form post doesn't work

    Quote Originally Posted by Norm View Post
    What response do you get from the server when you run that code with the login attempt?
    When running that code it doesnt print anything, when opening the url of that code in a browser it doesnt show anything

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: sending password form post doesn't work

    When running that code it doesnt print anything
    Add code to print the response code and the contents of the response header.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2019
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: sending password form post doesn't work

    Quote Originally Posted by Norm View Post
    Add code to print the response code and the contents of the response header.
    Response code is 200 and response header:
    Keep-Alive: [timeout=5, max=100]
    null: [HTTP/1.1 200 OK]
    Server: [Apache/2.2.22 (Debian)]
    Access-Control-Allow-Origin:[*]
    Connection: [Keep-Alive]
    Pragma: [no-cache]
    Date: [Mon, 04 Nov 2019 17:06:40 GMT]
    X-Frame-Options: [SAMEORIGIN]
    Cache-Control: [no-cache, must-revalidate, max-age=0]
    Vary: [Accept-Encoding]
    Set-Cookie: [wordpress_test_cookie=WP+Cookie+check; path=/; secure, qtrans_front_language=de; expires=Tue, 03-Nov-2020 17:06:41 GMT; path=/, PHPSESSID=so080m9cqqjogqjg3glpulvfa0; path=/]
    Expires: [Wed, 11 Jan 1984 05:00:00 GMT]
    Content-Length: [0]
    Content-Type: [text/html; charset=UTF-8]
    X-Powered-By: [PHP/5.4.45-0+deb7u14]

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: sending password form post doesn't work

    Content-Length: [0]
    I have no idea why the server didn't send any content.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2019
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: sending password form post doesn't work

    Quote Originally Posted by Norm View Post
    I have no idea why the server didn't send any content.
    So I changed the request properties a bit, now when setting instance follow redirects to false, response code is FOUND and content length is 20. When setting it to true, it prints out the html code of the first URL, though the content i want to access is still not there, instead, there is still this password form

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: sending password form post doesn't work

    response code is FOUND
    Strange. I've never seen the word FOUND as the response code. It has always been a number like 200 or 404

    content length is 20
    What is in the content?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Nov 2019
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: sending password form post doesn't work

    Quote Originally Posted by Norm View Post
    response code is FOUND
    Strange. I've never seen the word FOUND as the response code. It has always been a number like 200 or 404

    content length is 20
    What is in the content?
    Just some empty html code. I get the same results when just opening the url in a browser. Thats why I set instance follow redirects to true, so it redirects me to the page I want to access (the page of the first URL). When its set to true, response code is OK and I get a bunch of html code, but the password form is still there. It should be replaced with the content I want to access, the content I needed the password for.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: sending password form post doesn't work

    Maybe the server does not like the way it is receiving the password and wants to make sure it is talking to a person and not a program.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Nov 2019
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: sending password form post doesn't work

    Quote Originally Posted by Norm View Post
    Maybe the server does not like the way it is receiving the password and wants to make sure it is talking to a person and not a program.
    Ahh, nvm, now its working. I tried again using HttpClient

Similar Threads

  1. Replies: 6
    Last Post: August 5th, 2014, 11:19 AM
  2. Sending XML to PHP Service via POST
    By aussiemcgr in forum Java Theory & Questions
    Replies: 6
    Last Post: July 9th, 2014, 08:04 AM
  3. Search Form Doesn't work!
    By wiedy10 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 20th, 2014, 04:06 AM
  4. My JPanel-form doesn´t show up
    By iHank in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 11th, 2013, 12:03 PM
  5. Problem sending POST request with Java..
    By lost in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 20th, 2010, 09:16 PM

Tags for this Thread