【问题标题】:jsp HTTPS Post Requestjsp HTTPS 发布请求
【发布时间】:2013-05-11 08:03:59
【问题描述】:

我正在尝试将 HTTPS 发布请求从 jsp 发送到 Opera 服务器。但我不能。我发现了很多 HTTP Post Request 示例,但没有 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(); 

【问题讨论】:

    标签: java jsp https


    【解决方案1】:

    完成所有 connopx 的设置后,您必须调用

    connopx.connect();
    

    我使用这个导入,它在我的代码中工作:

    import javax.net.ssl.HttpsURLConnection;
    

    URL opxurl =上面的两行我都没有

    也看看这个answer with similar issue

    编辑:

    为避免错误 500,请删除“application/x-www-form-urlencoded”中的空格并在output.writeBytes(data); 之后添加这两行:

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

    那么它应该可以工作了。

    【讨论】:

    • @Krut 谢谢.. 这解决了我的问题.. 但我收到 500 错误.. 可能与服务器相关.. !!
    • @Madan:很高兴能帮助您并回答您的问题。我编辑了我的答案。
    【解决方案2】:

    这应该是你的代码

      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.. Knut 的解决方案成功了!无论如何,感谢您提供建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 1970-01-01
    • 1970-01-01
    • 2012-12-28
    • 2015-02-27
    • 2010-12-15
    • 1970-01-01
    相关资源
    最近更新 更多