后端发送Http请求、忽略SSL检查、OKHttp 跳过SSL检查
1.OKhttp依赖
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.10.0</version>
</dependency>
2.忽略SSL检查的工具类
package com.maam.personnel.utils;
import javax.net.ssl.*;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.Arrays;
public class SSLSocketClient {
public static SSLSocketFactory getSSLSocketFactory() {
try {
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, getTrustManager(), new SecureRandom());
return sslContext.getSocketFactory();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static TrustManager[] getTrustManager() {
return new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[]{};
}
}
};
}
public static HostnameVerifier getHostnameVerifier() {
return (s, sslSession) -> true;
}
public static X509TrustManager getX509TrustManager() {
X509TrustManager trustManager = null;
try {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init((KeyStore) null);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
}
trustManager = (X509TrustManager) trustManagers[0];
} catch (Exception e) {
e.printStackTrace();
}
return trustManager;
}
}
3. 发送请求
@Slf4j
public class HttpUtil {
private final static String APPLICATION_ID = "1498989898989";
private final static String APPLICATION_NAME = "人员管理 ";
private final static String UPLOAD_URL = "https://maam-test-maam-caffile-caffile.entry.wanweibiyi.com/caf/file/v1/upload";
private final static String DOWN_URL = "https://maam-test-maam-caffile-caffile.entry.wanweibiyi.com/caf/file/v1/";
public static String fileUploadHttp(String base64){
FileDTO fileDTO = new FileDTO();
fileDTO.setApplicationId(APPLICATION_ID);
fileDTO.setApplicationName(APPLICATION_NAME);
fileDTO.setBase64(base64);
String jsonString = JSON.toJSONString(fileDTO);
Response response = null;
String responseBody = "";
String fileId = "";
try {
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonString);
Request request = new Request.Builder()
.url(UPLOAD_URL)
.post(requestBody)
.build();
OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS)
.sslSocketFactory(SSLSocketClient.getSSLSocketFactory(), SSLSocketClient.getX509TrustManager())
.hostnameVerifier(SSLSocketClient.getHostnameVerifier())
.build();
response = okHttpClient.newCall(request).execute();
int status = response.code();
if (status == 200 && response.body() != null) {
responseBody = response.body().string();
JsonDTO jsonDTO = JSON.parseObject(responseBody, JsonDTO.class);
fileId= jsonDTO.getData().getId();
}else {
responseBody = "error:"+ JSONObject.toJSONString(response);
}
} catch (Exception e) {
log.error("文件服务器接口调用异常:调用地址url={},error = {}",UPLOAD_URL, e.getMessage());
throw new BusinessException("文件服务器调用异常");
} finally {
if (response != null) {
response.close();
}
}
return fileId;
}
public static String fileDownLoadHttp(String fileId){
String url = DOWN_URL + fileId;
Request request = new Request.Builder()
.url(url)
.get()
.build();
Response response = null;
String responseBody = null;
String imgUrl = "";
try {
OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS)
.sslSocketFactory(SSLSocketClient.getSSLSocketFactory(), SSLSocketClient.getX509TrustManager())
.hostnameVerifier(SSLSocketClient.getHostnameVerifier())
.build();
response = okHttpClient.newCall(request).execute();
int status = response.code();
if (status == 200 && response.body() !=null){
responseBody = response.body().string();
JsonUrlDTO jsonUrlDTO = JSON.parseObject(responseBody, JsonUrlDTO.class);
imgUrl = jsonUrlDTO.getData();
}else {
responseBody = "error:"+ JSONObject.toJSONString(response);
}
} catch (Exception e) {
log.error("文件服务器接口调用异常:调用地址url={},error = {}",UPLOAD_URL, e.getMessage());
throw new BusinessException("文件服务器调用异常");
}
finally {
if (response != null) {
response.close();
}
}
return imgUrl;
}
public static Boolean deleteFileHttp(String fileId){
String url = DOWN_URL + fileId;
Request request = new Request.Builder()
.url(url)
.delete()
.build();
Response response = null;
String responseBody = null;
String imgUrl = "";
try {
OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS)
.sslSocketFactory(SSLSocketClient.getSSLSocketFactory(), SSLSocketClient.getX509TrustManager())
.hostnameVerifier(SSLSocketClient.getHostnameVerifier())
.build();
response = okHttpClient.newCall(request).execute();
int status = response.code();
if (status == 200 && response.body() !=null){
return true;
}else {
responseBody = "error:"+ JSONObject.toJSONString(response);
return false;
}
} catch (Exception e) {
log.error("文件服务器接口调用异常:调用地址url={},error = {}",UPLOAD_URL, e.getMessage());
throw new BusinessException("文件服务器调用异常");
}
finally {
if (response != null) {
response.close();
}
}
}
}
4. 测试
public static void main(String[] args) {
Boolean aBoolean = HttpUtil.deleteFileHttp("b8e44ec45efd44c3bfe96e0606a66bcc");
System.out.println(aBoolean);
String s = HttpUtil.fileDownLoadHttp("332b8655b27d4d509a0dd196754c6684");
System.out.println(s);
}
|