packagecom.wl.webservice;importjava.io.InputStream;importjava.net.HttpURLConnection;importjava.net.SocketException;importjava.net.SocketTimeoutException;importjava.net.URL;importjava.security.cert.CertificateException;importjava.security.cert.X509Certificate;importjavax.net.ssl.HostnameVerifier;importjavax.net.ssl.HttpsURLConnection;importjavax.net.ssl.SSLContext;importjavax.net.ssl.SSLSession;importjavax.net.ssl.SSLSocketFactory;importjavax.net.ssl.X509TrustManager;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importcom.alibaba.fastjson.JSON;importcom.alibaba.fastjson.JSONObject;importcom.ricoh.rapp.ezcx.iwbservice.util.Constant;importcom.ricoh.rapp.ezcx.iwbservice.util.Utils;public classHttpsRequest {private final Logger logger = LoggerFactory.getLogger(HttpsRequest.class);/*** 处理https GET/POST请求
*@paramrequestUrl 请求地址
*@paramrequestMethod 请求方法
*@paramrequestStr 请求参数
*@return
*/
publicJSONObject httpsRequest(String requestUrl, String requestMethod, String requestStr) {
logger.info("req---->:" + requestMethod +requestStr);
HttpsURLConnection httpsConnection= null;try{//创建SSLContext
SSLContext sslContext = SSLContext.getInstance("SSL");
TrustManager[] tm= { newTrustManager() };//初始化
sslContext.init(null, tm, newjava.security.SecureRandom());//获取SSLSocketFactory对象
SSLSocketFactory ssf =sslContext.getSocketFactory();
HostnameVerifier HostnameVerifier= newHostnameVerifier() {
@Overridepublic booleanverify(String var1, SSLSession var2) {return true;
}
};
URL url= newURL(requestUrl);
httpsConnection=(HttpsURLConnection) url.openConnection();
httpsConnection.setDoOutput(false);
httpsConnection.setDoInput(true);
httpsConnection.setConnectTimeout("3 * 1000");
httpsConnection.setReadTimeout("15 * 1000");
httpsConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
httpsConnection.setRequestProperty("Charset", "UTF-8");
httpsConnection.setRequestProperty("Authorization", "Basic aXdidXNlcjp0ZXN0MDAwMA==");
httpsConnection.setRequestProperty("User-Agent", "Client identifier");
httpsConnection.setRequestMethod("GET");/** httpsConnection.setUseCaches(false);
* httpsConnection.setRequestMethod(requestMethod);*/
//设置当前实例使用的SSLSoctetFactory
httpsConnection.setSSLSocketFactory(ssf);
httpsConnection.setHostnameVerifier(HostnameVerifier);
httpsConnection.connect();//往服务器端写内容//读取服务器端返回的内容
InputStream inputStream =httpsConnection.getInputStream();if (httpsConnection.getResponseCode() !=HttpURLConnection.HTTP_OK) {
logger.error("connect ezcs service failed: " +httpsConnection.getResponseCode());
JSONObject responseJson= newJSONObject();
responseJson.put(ResponseKey.KEY_RESULT,
com.ricoh.rapp.unifiedPlatform.dsdkService.constant.HttpConstant.ResultCode.ERROR_SERVER_HTTP_ERROR);returnresponseJson;
}
String response=Utils.convertStreamToString(inputStream, Constant.ENCODING_UTF_8);
logger.debug("response from ezcs service: " +response);
JSONObject responseJson=JSON.parseObject(response);returnresponseJson;
}catch(Exception e) {
e.printStackTrace();
logger.debug("connect local ezcs service exception: " +e.getMessage());
JSONObject responseJson= newJSONObject();if (e instanceof SocketTimeoutException || e instanceofSocketException) {
responseJson.put(ResponseKey.KEY_RESULT,
com.ricoh.rapp.unifiedPlatform.dsdkService.constant.HttpConstant.ResultCode.ERROR_SERVER_HTTP_ERROR);
}else{
responseJson.put(ResponseKey.KEY_RESULT,
com.ricoh.rapp.unifiedPlatform.dsdkService.constant.HttpConstant.ResultCode.ERROR_INNER_ERROR);
}returnresponseJson;
}finally{if (httpsConnection != null) {
httpsConnection.disconnect();
}
}
}class TrustManager implementsX509TrustManager {
@Overridepublic void checkClientTrusted(X509Certificate[] arg0, String arg1) throwsCertificateException {
}
@Overridepublic void checkServerTrusted(X509Certificate[] arg0, String arg1) throwsCertificateException {
}
@OverridepublicX509Certificate[] getAcceptedIssuers() {return newX509Certificate[] {};
}
}public static classResponseKey {public static final String KEY_RESULT = "result";public static final String KEY_REASON = "reason";public static final String KEY_DATA = "data";public static final String KEY_EXTRA = "extra";
}
}