https.get请求

该博客主要介绍了一个用于处理HTTPS GET/POST请求的Java类,包括SSL上下文的创建、信任管理器的实现以及超时和错误处理。代码中设置了自定义的HostnameVerifier以允许所有主机,并实现了不验证服务器证书的TrustManager。此代码适用于内部服务间的通信或者测试环境,但在生产环境中应谨慎使用,确保安全策略符合标准。
摘要由CSDN通过智能技术生成

package com.wl.webservice;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ricoh.rapp.ezcx.iwbservice.util.Constant;
import com.ricoh.rapp.ezcx.iwbservice.util.Utils;

public class HttpsRequest {

private final Logger logger = LoggerFactory.getLogger(HttpsRequest.class);

/**
* 处理https GET/POST请求
* @param requestUrl 请求地址
* @param requestMethod 请求方法
* @param requestStr 请求参数
* @return
*/
public JSONObject httpsRequest(String requestUrl, String requestMethod, String requestStr) {
logger.info("req---->:" + requestMethod + requestStr);
HttpsURLConnection httpsConnection = null;
try {
// 创建SSLContext
SSLContext sslContext = SSLContext.getInstance("SSL");
TrustManager[] tm = { new TrustManager() };
// 初始化
sslContext.init(null, tm, new java.security.SecureRandom());

// 获取SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
HostnameVerifier HostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String var1, SSLSession var2) {
return true;
}

};

URL url = new URL(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 = new JSONObject();
responseJson.put(ResponseKey.KEY_RESULT,
com.ricoh.rapp.unifiedPlatform.dsdkService.constant.HttpConstant.ResultCode.ERROR_SERVER_HTTP_ERROR);
return responseJson;
}
String response = Utils.convertStreamToString(inputStream, Constant.ENCODING_UTF_8);
logger.debug("response from ezcs service: " + response);
JSONObject responseJson = JSON.parseObject(response);
return responseJson;
} catch (Exception e) {
e.printStackTrace();
logger.debug("connect local ezcs service exception: " + e.getMessage());
JSONObject responseJson = new JSONObject();
if (e instanceof SocketTimeoutException || e instanceof SocketException) {
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);
}
return responseJson;
} finally {
if (httpsConnection != null) {
httpsConnection.disconnect();
}
}
}

class TrustManager implements X509TrustManager {

@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {

}

@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {

}

@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[] {};
}

}

public static class ResponseKey {
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";
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值