I am trying to authenticate to a server's secure URL using java.net.urlconnection
- based on the useful tutorial at Using java.net.URLConnection to fire and handle HTTP requests
The response will be as given below--
Return: A session id object: {'session_id': 'username:session_token'}
This session_id
will be used for all methods which require authentication.
This response is JSON response, and I am thinking of using the Google GSON library (since my web app is on Google App Engine).
The code I have used so far (based on the tutorial to which I have given link above) is given below--
String url = "https://api.wordstream.com/authentication/login";
String charset = "UTF-8";
String param1 = WORDSTREAM_API_USERNAME;
String param2 = WORDSTREAM_API_PASSWORD;
// ...
String query = String.format("username=%s&password=%s",
URLEncoder.encode(param1, charset),
URLEncoder.encode(param2, charset));
URLConnection connection = new URL(url + "?" + query).openConnection();
connection.setRequestProperty("Accept-Charset", charset);
InputStream response = connection.getInputStream();
InputStream error = ((HttpURLConnection) connection).getErrorStream();
//now checking HTTP Response status
int status = ((HttpURLConnection) connection).getResponseCode();
How do I proceed further to obtain the JSON response and retrieve session ID from that response correctly?
Another question- I want to store the session ID in session data that can be accessed by javascript functions, because I plan to use javascript functions to make calls and obtain further data. Is this possible, and how to implement this?