I'm trying to write a code that will open multiple URLs that require authentication and parse the text of the webpage for a SUCCESS message. If I supply the username and password in the authentication pop up, then the rest will go through without having to supply authentication every time. Right now, my authentication class looks like so:
public class CustomAuthenticator extends Authenticator {
protected PasswordAuthentication getPasswordAuthentication() {
String username = "********";
String password = "********";
return new PasswordAuthentication(username, password.toCharArray());
}
}
And my URL parsing method is:
public static String urlSuccessUnknown(String url) {
Document doc;
String res = null;
Authenticator.setDefault(new CustomAuthenticator());
try {
doc = Jsoup.connect(url).get();
res = doc.body().text();
if(res.indexOf("SUCCESS") >= 0)
return "SUCCESS";
else if(!(res.indexOf("SUCCESS") >= 0))
return "FLAG";
}
catch (Exception e) { e.printStackTrace(); }
return "Unable to correctly parse";
}
Whenever I test this with one URL, I receive a "java.lang.NullPointerException" at the line "res = doc.body().text();" and two other lines in other parts of my code. What am I doing wrong?
Also, when the webpage is opened, it asks for the username and password in a pop-up window instead of embedded in the page. I just wanted to include this in case a CustomAuthenticator is not the correct way to supply the username and password. Thank you for your time.