該問題的解決辦法 1、在請求前需要將證書導入,不推薦 2、繞開安全協議處理
下面的代碼時一段http請求並且繞開安全協議。可直接使用
/** * * @param url 需要請求的網關路徑 * @param sendData 請求時需要傳入的參數 * @param urlencode url的編碼格式 * @param connTimeOut 鏈接超時時間 * @param readTimeOut 讀取超時時間 * @param contentType 請求頭部 固定輸入"application/x-www-form-urlencoded;charset="+urlencode * @param header 輸入null * @return */ public static String sendAndRcvHttpPostBase(String url,String sendData,String urlencode,int connTimeOut,int readTimeOut,String contentType,Map<String,String> header){ Long curTime = System.currentTimeMillis(); Trace.logInfo(Trace.COMPONENT_HTTP, "SimpleHttpConnUtil Prepare @"+curTime); String result = ""; BufferedReader in = null; DataOutputStream out = null; int code = 999; HttpsURLConnection httpsConn = null; HttpURLConnection httpConn = null; try{ URL myURL = new URL(url); Trace.logInfo(Trace.COMPONENT_HTTP, "請求地址:"+url); if(url.startsWith("https://")){ httpsConn = (HttpsURLConnection) myURL.openConnection(); TrustManager[] trustAllCerts = new TrustManager[]{ new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted( java.security.cert.X509Certificate[] certs, String authType) { } public void checkServerTrusted( java.security.cert.X509Certificate[] certs, String authType) { } } }; SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); httpsConn.setSSLSocketFactory(sc.getSocketFactory()); HostnameVerifier hv = new HostnameVerifier() { @Override public boolean verify(String urlHostName, SSLSession session) { return true; } }; httpsConn.setHostnameVerifier(hv); httpsConn.setRequestProperty("Accept-Charset", urlencode); httpsConn.setRequestProperty("User-Agent","java HttpsURLConnection"); if(header!=null){ for(String key:header.keySet()){ httpsConn.setRequestProperty(key, (String)header.get(key)); } } httpsConn.setRequestMethod("POST"); httpsConn.setUseCaches(false); httpsConn.setRequestProperty("Content-Type",contentType); httpsConn.setConnectTimeout(connTimeOut); httpsConn.setReadTimeout(readTimeOut); httpsConn.setDoInput(true); httpsConn.setInstanceFollowRedirects(true); if(sendData !=null){ httpsConn.setDoOutput(true); // 獲取URLConnection對象對應的輸出流 out = new DataOutputStream(httpsConn.getOutputStream()); // 發送請求參數 out.write(sendData.getBytes(urlencode)); // flush輸出流的緩沖 out.flush(); out.close(); } // 取得該連接的輸入流,以讀取響應內容 in = new BufferedReader(new InputStreamReader(httpsConn.getInputStream(),urlencode)); code = httpsConn.getResponseCode(); }else{ httpConn = (HttpURLConnection) myURL.openConnection(); httpConn.setRequestProperty("Accept-Charset", urlencode); httpConn.setRequestProperty("user-agent","java HttpURLConnection"); if(header!=null){ for(String key:header.keySet()){ httpConn.setRequestProperty(key, (String)header.get(key)); } } httpConn.setRequestMethod("POST"); httpConn.setUseCaches(false); httpConn.setRequestProperty("Content-Type",contentType); httpConn.setConnectTimeout(connTimeOut); httpConn.setReadTimeout(readTimeOut); httpConn.setDoInput(true); httpConn.setInstanceFollowRedirects(true); if(sendData !=null){ httpConn.setDoOutput(true); // 獲取URLConnection對象對應的輸出流 out = new DataOutputStream(httpConn.getOutputStream()); // 發送請求參數 out.write(sendData.getBytes(urlencode)); // flush輸出流的緩沖 out.flush(); out.close(); } // 取得該連接的輸入流,以讀取響應內容 in = new BufferedReader(new InputStreamReader(httpConn.getInputStream(),urlencode)); code = httpConn.getResponseCode(); } if (HttpURLConnection.HTTP_OK == code){ String line; while ((line = in.readLine()) != null) { result += line; System.out.println("=====反回結果====="+ line); } if(result.length()>2000){ Trace.logInfo(Trace.COMPONENT_ACTION, "http返回結果 !\n"+result.substring(0,2000)+"..."); }else{ Trace.logInfo(Trace.COMPONENT_ACTION, "http返回結果 !\n"+result); } }else{ result = null; throw new Exception("支付失敗,服務端響應碼:"+code); } }catch(IOException e){ Trace.logError(Trace.COMPONENT_ACTION, "http通訊失敗 !",e); result = null; }catch(Exception e){ Trace.logError(Trace.COMPONENT_ACTION, "http通訊失敗 !",e); result = null; }finally{ Trace.logInfo(Trace.COMPONENT_ACTION,"對方地址:"+url); if(out!=null){ try { out.close(); } catch (IOException e) { } } if(httpConn!=null){ httpConn.disconnect(); } if(httpsConn!=null){ httpsConn.disconnect(); } if(in!=null){ try { in.close(); } catch (IOException e) { } } } Trace.logInfo(Trace.COMPONENT_HTTP, "SimpleHttpConnUtil "+curTime+" end for "+(System.currentTimeMillis()-curTime)+"ms"); return result; }
以上代碼中使用的java類的包路徑,只有涉及到安全協議的包路徑。
import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager;