Subsequent HTTPS POST request in Java with cookies retained

后端 未结 3 1786
轮回少年
轮回少年 2020-12-20 09:03

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

相关标签:
3条回答
  • 2020-12-20 09:50

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

    0 讨论(0)
  • 2020-12-20 09:59

    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

    0 讨论(0)
  • 2020-12-20 10:05

    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();
    
    0 讨论(0)
提交回复
热议问题