I've created a simple Java project (not a Dynamic Web Project) in Eclipse.
I'd like to be able to access a web URL that returns some JSON values.
I'm able to get to the URL using HttpClient and all that but I'm unable to Authenticate my session, which is required for me to get access to the JSON data. If you're not authenticated, the URL redirects you to a login page.
I've tried numerous ways of authenticating my session but none seem to work -- I always end up up getting login page's HTML data instead of the JSON.
I've also noticed that a JSESSION ID is passed into the Request Header but I can't seem to figure out how to create a JSESSION ID and how to pass that into the GET request.
So basically, I can solve this issue in two ways:
- Authenticate my session and then access the URL to get the JSON
- Create a JSESSION ID and pass that into the GET request to get the JSON
Anyone know how to do either?
Eventually, I'd like to use the data I collected from the JSON to do some Selenium testing so do I need to do all this within a Dynamic Web Project (in Eclipse) or can it be all done within a regular Java project?
Attempt 1
URL url = new URL(_url);
InputStream ins = url.openConnection().getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
StringBuilder builder = new StringBuilder();
for (String line = null; (line = reader.readLine()) != null;) {
builder.append(line).append("\n");
}
System.out.println("RESPONSE:\n\n" + builder);
JSONTokener tokener = new JSONTokener(builder.toString());
JSONArray finalResult = new JSONArray(tokener);
System.out.println("JSON:\n\n" + finalResult.toString());
Attempt 1 Results
I end up printing out the HTML code for the log-in page and then get the following error message since there is no JSON on the page:
Exception in thread "main" org.json.JSONException: A JSONArray text must start with '[' at character 1
Attempt 2
I have also tried the code found in Oracle's Http Authentication page:
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
public class RunHttpSpnego {
static final String kuser = "username"; // your account name
static final String kpass = "password"; // your password for the account
static class MyAuthenticator extends Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
// I haven't checked getRequestingScheme() here, since for NTLM
// and Negotiate, the usrname and password are all the same.
System.err.println("Feeding username and password for " + getRequestingScheme());
return (new PasswordAuthentication(kuser, kpass.toCharArray()));
}
}
public static void main(String[] args) throws Exception {
Authenticator.setDefault(new MyAuthenticator());
URL url = new URL(args[0]);
InputStream ins = url.openConnection().getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
String str;
while((str = reader.readLine()) != null)
System.out.println(str);
}
}