2013-05-11 182 views
1

我試圖從jsp向Opera Server發送HTTPS發佈請求。但我不能。我發現了許多HTTP Post請求示例,但沒有找到HTTPS。我現在收到錯誤消息..jsp HTTPS發佈請求

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

我進口是這個代碼如下

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

我的代碼如下

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(); 

回答

1

你必須調用所有connopx的設置之後

connopx.connect(); 

我ü SE這導入和它在我的代碼工作:

import javax.net.ssl.HttpsURLConnection; 

我沒有上述URL opxurl =

看一看這個answer with similar issue也是兩條線。

編輯:

爲了避免錯誤刪除500空間 「應用程序/ x-WWW的形式,進行了urlencoded」 和output.writeBytes(data);之後添加這些兩行:

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

那麼它應該工作。

+0

我編輯我的問題。現在我得到錯誤..這是個好.. !!! – 2013-05-11 08:18:56

+0

@Krut謝謝..這解決了我的問題..但我得到500錯誤..可能是服務器相關.. !! – 2013-05-11 11:21:33

+0

@馬丹:我很高興我能幫你解答你的問題。我編輯了我的答案。 – 2013-05-11 21:58:52

0

這應該是你的代碼

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) { 

    } 
+0

@M ..克努特的解決方案解決了!無論如何,感謝您提供您的建議。 – 2013-05-12 09:31:29