Subsequent HTTPS POST request in Java with cookies retained

后端 未结 3 1801
轮回少年
轮回少年 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: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

提交回复
热议问题