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