java get https_java的https的get请求

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";

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,关于Java请求HTTPS证书异常的问题,可能是由于以下原因导致的: 1. 证书不被信任:您需要确保证书是有效且被Java信任的。您可以通过安装证书来解决此问题。 2. 证书链不完整:如果证书链不完整,则可能导致Java无法验证证书。您需要确保证书链完整且正确。 3. 服务器配置错误:如果服务器配置错误,则可能导致Java请求证书异常。您需要检查服务器配置是否正确。 以下是一个简单的Java HTTPS请求示例,您可以参考一下: ``` import javax.net.ssl.HttpsURLConnection; import java.net.URL; import java.security.cert.X509Certificate; import javax.net.ssl.X509TrustManager; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import java.security.SecureRandom; public class HttpsRequest { public static void main(String[] args) throws Exception { URL url = new URL("https://example.com"); SSLContext sslContext = SSLContext.getInstance("TLS"); TrustManager[] trustManagers = new TrustManager[] { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(X509Certificate[] certs, String authType) { } public void checkServerTrusted(X509Certificate[] certs, String authType) { } } }; sslContext.init(null, trustManagers, new SecureRandom()); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setSSLSocketFactory(sslContext.getSocketFactory()); conn.setRequestMethod("GET"); conn.connect(); System.out.println(conn.getResponseCode()); } } ``` 希望这能够帮助您解决问题。如果还有其他问题,请随时问我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值