Remote host closed connection during handshake


/**
* dopost請求
* @param url
* @param msgEn
* @return
*/
private String doPost(String url, String msgEn) throws Exception {
URL urlServlet = null;
BufferedReader reader = null;
StringBuffer lines = new StringBuffer();
try {
urlServlet = new URL(url);
HttpURLConnection urlConnect = (HttpURLConnection) urlServlet
.openConnection();
// 設置連接參數
urlConnect.setRequestMethod("POST");
urlConnect.setDoInput(true);
urlConnect.setDoOutput(true);
urlConnect.setRequestProperty("Content-type", "application/json");
urlConnect.setAllowUserInteraction(true);

// 開啟流,寫入流數據
if (null != msgEn || "".equals(msgEn)) {
OutputStream output = urlConnect.getOutputStream();
output.write(msgEn.getBytes("UTF-8"));
output.flush();
output.close();
}
// 獲取返回輸出流
InputStream input = urlConnect.getInputStream();
reader = new BufferedReader(new InputStreamReader(input,"GBK"));
String strMesg = "";
while ((strMesg = reader.readLine()) != null) {
lines.append(strMesg);
}
} catch (IOException e) {
throw new Exception("電子合同簽約失敗:" + e.getMessage());
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
throw new Exception("電子合同開戶關閉流失敗:" + e.getMessage());
}
}
}
return lines.toString();
}

用上述方法訪問http請求時,沒問題,如果訪問https請求時報錯Remote host closed connection during handshake,網上搜了搜大體意思是

1. jdk1.7默認是TSLv1, 但是可以支持TSLv1.1,TSLv1.2,jdk1.8默認是TSLv1.2

2.如果客服端是TSLv1,服務器端設置是TSLv1.2,訪問會出現connection reset的錯誤.

我搜了一下工程,用該方法可以解決:

private String doHttpsPost(String url, String strData) throws Exception {
URL uploadServlet = null;
BufferedReader reader = null;
StringBuffer lines = new StringBuffer();
try {
TrustManager[] tm = { new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
} };
// 創建SSLContext
SSLContext sslContext = SSLContext.getInstance("SSL");
// 初始化
sslContext.init(null, tm, new java.security.SecureRandom());
// 獲取SSLSocketFactory對象
SSLSocketFactory ssf = sslContext.getSocketFactory();
System.setProperty("https.protocols", "TLSv1.2,TLSv1.1,SSLv3");

uploadServlet = new URL(url);
HttpsURLConnection servletConnection = (HttpsURLConnection) uploadServlet.openConnection();//在此獲取https連接
// 設置連接參數
servletConnection.setRequestMethod("POST");
servletConnection.setDoOutput(true);
servletConnection.setDoInput(true);
servletConnection.setUseCaches(false);
servletConnection.setRequestProperty("Content-type", "application/json");
servletConnection.setAllowUserInteraction(true);
servletConnection.setConnectTimeout(10000);
servletConnection.setReadTimeout(10000);
// 設置當前實例使用的SSLSoctetFactory
servletConnection.setSSLSocketFactory(ssf);
servletConnection.connect();


// 開啟流,寫入數據
if(strData!=null && !"".equals(strData)){
OutputStream output = servletConnection.getOutputStream();
output.write(strData.getBytes("UTF-8"));
output.flush();
output.close();
}

// 獲取返回的數據
InputStream inputStream = servletConnection.getInputStream();
reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String line = "";
while ((line = reader.readLine()) != null) {
lines.append(line);
}
} catch (Exception e) {
throw new Exception("電子合同簽約失敗:" + e.getMessage());
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
throw new Exception("電子合同開戶關閉流失敗:" + e.getMessage());
}
}
}
return lines.toString();
}

但是上邊方法對http會有問題,所以可以用該邏輯

if(url.toLowerCase().startsWith("https")){

userid = doHttpsPost(url,requestStr);
} else {
userid = doPost(url,requestStr);
}

解決

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM