HttpUtil工具类
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import okhttp3.internal.platform.Platform;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
@Slf4j
public class HttpUtil {
/**
* 功能描述: post请求表单提交
* @Author: daiyg
* @date: 2022/9/30 14:21
* @param: path 请求地址
* @param: paramsMap 请求参数
* @return: java.lang.String 返回内容
*/
public static String sendDataPost(String path, HashMap<String, String> paramsMap) {
String results = "";
try {
final TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[]{};
}
}
};
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new SecureRandom());
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
X509TrustManager trustManager = Platform.get().platformTrustManager();
//跳过证书验证
OkHttpClient okHttpClient = new OkHttpClient.Builder().sslSocketFactory(sslSocketFactory, trustManager)
.connectTimeout(40, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.readTimeout(40, TimeUnit.SECONDS)
.build();
//创建一个FormBody.Builder
FormBody.Builder builder = new FormBody.Builder();
for (String key : paramsMap.keySet()) {
//追加表单信息
builder.add(key, paramsMap.get(key) == null ? "" : paramsMap.get(key));
}
//生成表单实体对象
RequestBody formBody = builder.build();
//创建一个请求
final Request request = new Request.Builder().url(path).post(formBody).build();
//创建一个Call
final Call call = okHttpClient.newCall(request);
//执行请求
Response response = call.execute();
if (response.isSuccessful()) {
results = response.body().string();
log.error("上传数据成功,返回信息:{}", results);
return results;
}
} catch (IOException e) {
log.error("上传数据失败:" + e.getMessage());
return null;
} catch (Exception e) {
log.error("上传数据失败:" + e.getMessage());
return null;
}
return null;
}
}
#引用的jar包
<okhttp.version>4.3.1</okhttp.version>
<okio.version>2.2.2</okio.version>
<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>${okhttp.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.squareup.okio/okio -->
<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
<version>${okio.version}</version>
</dependency>
应用实例
log.info("传入的参数:{}", shopModel);
String url = uploadUrl + "/importKhgl";
try {
HashMap<String, String> param = new HashMap<>();
param.put("kh_name", shopModel.getKh_name());
if (StringUtils.isNotEmpty(shopModel.getKh_address())) {
param.put("kh_address", shopModel.getKh_address());
}
param.put("kh_atten", shopModel.getKh_atten());
param.put("kh_atten_tel", shopModel.getKh_atten_tel());
param.put("fp_number", shopModel.getFp_number());
if (StringUtils.isNotEmpty(shopModel.getKh_jc())) {
param.put("kh_jc", shopModel.getKh_jc());
}
param.put("username", username);
param.put("password", password);
String result = HttpUtil.sendDataPost(url, param);
log.error("上传信息;返回信息:" + result);
JSONObject object = JSON.parseObject(result);
int code = object.getIntValue("code");
if (200 == code) {
JSONObject data = JSON.parseObject(object.getString("data"));
log.info("上传信息成功,返回信息:{}", result);
String khCode = data.getString("kh_code");
return khCode;
} else {
log.error("上传信息失败,返回信息:{}", result);
return null;
}
} catch (Exception e) {
log.error("上传信息失败:" + e.getMessage());
return null;
}