How do I do a HTTP post using SSL?

Ben00Golden
Ben00Golden used Ask the Experts™
on
I have a simple piece of Java code that sends some form data to a URL, and gets the response. This works for non-secure HTTP, but how do I do the same thing using HTTPS/SSL?
String sPostData = "";
String sURL = "http://my.api.com";
String sXMLResponse = "";
 
// CONSTRUCT POST DATA
sPostData =        URLEncoder.encode("ACTION", "UTF-8") + "=" + URLEncoder.encode("IDENTIFY", "UTF-8");
sPostData += "&" + URLEncoder.encode("CARD_NUMBER", "UTF-8") + "=" + URLEncoder.encode((String) args[inCardNumber], "UTF-8");
 
// HTTP POST
if (sURL.startsWith("http://")) {
        try {
 
                // DO FORM POST
                URL requestURL = new URL(sURL);
                URLConnection requestConn = requestURL.openConnection();
                requestConn.setDoOutput(true);
                OutputStreamWriter wr = new OutputStreamWriter(requestConn.getOutputStream());
                wr.write(sPostData);
                wr.flush();
                    
                // GET HTTP RESPONSE
                BufferedReader rd = new BufferedReader(new InputStreamReader(requestConn.getInputStream()));
                while ((sXMLResponse += rd.readLine()) != null) { }
                wr.close();
                rd.close();
                        
        } catch (Exception e) {
                log.error("CREDIT CARD IDENTIFY FAILED [HTTP POST ERROR]: " + e.getMessage(), session);
                        return fwdFail;             
        }
 
} else if (sURL.startsWith("https://")) {
        try {
                                
                /* HOW DO I DO THIS? */
 
        } catch (Exception e) {
                log.error("CREDIT CARD IDENTIFY FAILED [SSL HTTP POST ERROR]: " + e.getMessage(), session);
                        return fwdFail;             
        }
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
Commented:
Mick BarryJava Developer
Top Expert 2010

Commented:
just use https in your url, shouldn't need to change your code
Top Expert 2016

Commented:
:-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial