HTTPS GET | POST | DELETE 请求

依赖:

<dependency>  <groupId>com.squareup.okhttp3</groupId>  <artifactId>okhttp</artifactId>  <version>3.10.0</version> </dependency>

Example:

public class MQHttpClient { 
/**
* GET | POST | DELETE
*
* @param paramsMap if null and the request is Get,otherwise dependent on requestMethodEnum
* @param paramsMap if the request is post,and you can put the params in the map otherwise to null
* @param cookie if you need cookie to veriy,you can use it, otherwise to null
* @return responseJsonString
*/
public static String sendRequestURL(String url, HashMap<String, Object> paramsMap, RequestMethodEnum requestMethodEnum, String cookie) {
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON, com.alibaba.fastjson.JSON.toJSONString(paramsMap, true));
Request.Builder request = new Request.Builder();
Response response;
try {
OkHttpClient client = new OkHttpClient.Builder().build();
OkHttpClient httpClient = trustAllSslClient(client);
// GET
if (paramsMap == null) {
if (cookie != null) {
response = httpClient.newCall(request.url(url).addHeader("cookie", cookie).get().build()).execute();
} else {
response = httpClient.newCall(request.url(url).get().build()).execute();
}
} else { // POST || DELETE
if (requestMethodEnum.getValue().equals(RequestMethodEnum.POST.getValue())) {
if (cookie != null) {
response = httpClient.newCall((request.url(url).post(body)).addHeader("cookie", cookie).build()).execute();
} else {
response = httpClient.newCall((request.url(url).post(body)).build()).execute();
}
} else {
if (cookie != null) {
response = httpClient.newCall((request.url(url).post(body)).addHeader("cookie", cookie).build()).execute();
} else {
response = httpClient.newCall((request.url(url).post(body)).build()).execute();
}
}
}
String responseBody = response.body().string();
logger.info("responseBody:" + responseBody + ", Code:" + response.code());
return responseBody;
} catch (Exception e) {
logger.info("get exception:" + e.toString());
e.printStackTrace();
return null;
}
}

private static final TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}

public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}

public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
}
};

private static final SSLContext trustAllSslContext;

static {
try {
trustAllSslContext = SSLContext.getInstance("SSL");
trustAllSslContext.init(null, trustAllCerts, new java.security.SecureRandom());
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private static final SSLSocketFactory trustAllSslSocketFactory = trustAllSslContext.getSocketFactory();

public static OkHttpClient trustAllSslClient(OkHttpClient client) {
OkHttpClient.Builder builder = client.newBuilder();
builder.sslSocketFactory(trustAllSslSocketFactory, (X509TrustManager) trustAllCerts[0]);
builder.hostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
return builder.build();
}
}

 

猜你喜欢

转载自www.cnblogs.com/huaiheng/p/12817230.html