0

I need to obtain the input stream to a HTTPS URL eg. https://baseurl.com/mypdfgenerated.php?param=somevalue. In order to access this URL I need to get through the login page (eg. https://baseurl.com/login.php) by supplying BODY parameters:

user_name, web_pwd and submit_login 

I'm assuming the only way to successfully access the first URL is by a POST to the /login.php followed by storing the cookies and then reusing the cookie-session-ID in the next GET request; if this is the correct approach then could someone please share a solution with the correct/recent libraries?

L P
  • 1,444
  • 5
  • 23
  • 43
  • 1
    Pass a [CookieManager](http://docs.oracle.com/javase/8/docs/api/java/net/CookieManager.html) to [CookieHandler.setDefault](http://docs.oracle.com/javase/8/docs/api/java/net/CookieHandler.html#setDefault-java.net.CookieHandler-). – VGR Sep 15 '15 at 18:47

3 Answers3

2

Not sure which is the best way but what helped me achieve this is the CloseableHttpClient class which along with BasicCookieStore retains cookies for subsequent requests once logged in, implemented below:

BasicCookieStore cookieStore = new BasicCookieStore(); 
CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build(); 
HttpUriRequest login = RequestBuilder.post()
                    .setUri(new URI(url_login))
                    .addParameter("login", "loginuname")
                    .addParameter("password", "pwd")
                    .addParameter("submit", "sub_mit");
CloseableHttpResponse response = httpclient.execute(login);
List<Cookie> cookies = cookieStore.getCookies();
response.close();

HttpGet httpget2 = new HttpGet(url_to_get_after_login);
CloseableHttpResponse response2 = httpclient.execute(httpget2);
response2.close();
L P
  • 1,444
  • 5
  • 23
  • 43
1

Sample code snippet from Java Samples

try { 
    System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
    java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); 
    URL url = new URL("https://www.yourwebsite.com/"); // Some URL
    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
    connection.setDoInput(true); 
    connection.setDoOutput(true);
    connection.setRequestMethod("POST"); 
    connection.setFollowRedirects(true); 
    String query = "UserID=" + URLEncoder.encode("username"); 
    query += "&"; 
    query += "password=" + URLEncoder.encode("password"); 
    query += "&"; 
    // open up the output stream of the connection 
    DataOutputStream output = new DataOutputStream( connection.getOutputStream() ); 
    // write out the data 
    output.writeBytes( query ); 
}catch(Exception err){
    err.printStackTrace();
}

Have a look at Usage of cookies

Community
  • 1
  • 1
Ravindra babu
  • 42,401
  • 8
  • 208
  • 194
  • Found one more solution from SE : http://stackoverflow.com/questions/496651/connecting-to-remote-url-which-requires-authentication-using-java – Ravindra babu Sep 15 '15 at 17:49
0

You should use a library which handles cookies for you, such as Apache HTTPClient.

mvd
  • 1,991
  • 2
  • 25
  • 41