1

I am trying to send HTTPS Post Request from jsp to Opera Server. But I can't. I have found many HTTP Post Request examples but no HTTPS. I am getting an Error Message now..

java.lang.ClassCastException: sun.net.www.protocol.https.HttpsURLConnectionImpl cannot be cast to com.sun.net.ssl.HttpsURLConnection 

My import are as follows for this code

java.security.Security, com.sun.net.ssl.

My code as follows

try{     
    String data = URLEncoder.encode("AccountID", "UTF-8") + "=" + URLEncoder.encode(accountid, "UTF-8");
    data += "&" + URLEncoder.encode("CallerTransactionID", "UTF-8") + "=" + URLEncoder.encode(callertransactionid, "UTF-8");    
    data += "&" + URLEncoder.encode("CurrentTime", "UTF-8") + "=" + URLEncoder.encode(currenttime, "UTF-8");    
    data += "&" + URLEncoder.encode("ErrorURL", "UTF-8") + "=" + URLEncoder.encode(errorurl, "UTF-8");    
    //data += "&" + URLEncoder.encode("FrameURL", "UTF-8") + "=" + URLEncoder.encode(frameurl, "UTF-8");    

    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 opxurl = new URL("https://opx-test.opera.com/opx/2.0/OPXPaymentEnable");
    HttpsURLConnection connopx = (HttpsURLConnection) opxurl.openConnection();
    connopx.setDoInput(true); 
    connopx.setDoOutput(true);
    connopx.setRequestMethod("POST"); 
    connopx.setFollowRedirects(true); 
    connopx.setRequestProperty("Content-length",String.valueOf(data.length()));
    connopx.setRequestProperty("Content-Type","application/x-www- form-urlencoded"); 

    // open up the output stream of the connection 
    DataOutputStream output = new DataOutputStream(connopx.getOutputStream()); 
    // write out the data 
    int dataLength = data.length(); 
    output.writeBytes(data); 
    //output.flush();

    System.out.println("HOly Portal ResponseDesc Code:"+connopx.getResponseCode()+" : "+connopx.getResponseMessage()); 

    // get ready to read the response from the cgi script 
    DataInputStream input = new DataInputStream( connopx.getInputStream() ); 

    // read in each character until end-of-stream is detected 
    for( int c = input.read(); c != -1; c = input.read() ) 
            System.out.print("HolyPortal respnse: "+ (char)c ); 

        input.close(); 
Madan Madan
  • 674
  • 1
  • 11
  • 28

2 Answers2

1

After all connopx's settings you have to call

connopx.connect();

I use this import and it works in my code:

import javax.net.ssl.HttpsURLConnection;

I don't have the two lines above URL opxurl =

Have a look at this answer with similar issue also.

EDIT:

To avoid error 500 remove space in "application/x-www-form-urlencoded" and add those two lines right after output.writeBytes(data);:

output.flush();
output.close();

Then it should work.

Community
  • 1
  • 1
Knut Herrmann
  • 30,880
  • 4
  • 31
  • 67
0

This should be your code

  try {
    URL url = new URL("yourURL");

    URLConnection conn = url.openConnection();

    if (conn instanceof HttpsURLConnection) {
      // Try again as HTTPS
      HttpsURLConnection conn1 = (HttpsURLConnection) url.openConnection();
      conn1.setHostnameVerifier(new HostnameVerifier() {
        @Override
        public boolean verify(String hostname, SSLSession session) {
          return true;
        }
      });

      conn1.getResponseCode();
    }
  }
  catch (MalformedURLException e) {

  }
  catch (IOException e) {

  }
M Sach
  • 33,416
  • 76
  • 221
  • 314