Question : How do I do a HTTP post using SSL?

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?
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
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;             
        }
}

Answer : How do I do a HTTP post using SSL?

Random Solutions  
 
programming4us programming4us