1.添加pom依赖
<!-- jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.2</version>
</dependency>
2.添加HttpUtil工具类
package com.elazine.mall.admin.util;
/**
* @Author: yanyezhiting
* @Date: 2019/6/4 10:53
* @Description:
*/
import java.io.Closeable;
import java.io.IOException;
import java.io.Serializable;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.List;
import java.util.Map;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.X509TrustManager;
import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
/**
* Http发送post请求工具,兼容http和https两种请求类型
*/
public class HttpUtils implements Serializable {
/**
* 请求超时时间
*/
private static final int TIME_OUT = 120000;
/**
* Https请求
*/
private static final String HTTPS = "https";
/**
* Content-Type
*/
private static final String CONTENT_TYPE = "Content-Type";
/**
* JSON提交方式Content-Type
*/
private static final String JSON_TYPE = "application/json;charset=UTF-8";
/**
* 发送JSON格式参数POST请求
*
* @param url 请求路径
* @param headers 请求头中设置的参数
* @param params JSON格式请求参数
* @return HTTP响应对象
* @throws IOException 程序异常时抛出,由调用者处理
*/
public static Response post(String url, Map <String, String> headers, String params) throws IOException {
return doPostRequest(url, headers, params);
}
public static Response get(String url, Map <String, String> headers, String params) throws IOException {
return doGetRequest(url, headers, params);
}
/**
* 执行post请求
*
* @param url 请求URL地址
* @param headers 请求头
* @param jsonParams 请求JSON格式字符串参数
* @return HTTP响应对象
* @throws IOException 程序异常时抛出,由调用者处理
*/
private static Response doPostRequest(String url, Map <String, String> headers, String jsonParams) throws IOException {
if (null == url || url.isEmpty()) {
throw new RuntimeException("请求参数为空");
}
// 如果是Https请求
if (url.startsWith(HTTPS)) {
getTrust();
}
Connection connection = Jsoup.connect(url);
connection.method(Method.POST);
connection.timeout(TIME_OUT);
connection.ignoreHttpErrors(true);
connection.ignoreContentType(true);
connection.maxBodySize(0);
if (null != headers) {
connection.headers(headers);
}
connection.header(CONTENT_TYPE, JSON_TYPE);
connection.requestBody(jsonParams);
Response response = connection.execute();
return response;
}
/**
* 执行get请求
*
* @param url 请求URL地址
* @param headers 请求头
* @param jsonParams 请求JSON格式字符串参数
* @return HTTP响应对象
* @throws IOException 程序异常时抛出,由调用者处理
*/
private static Response doGetRequest(String url, Map <String, String> headers, String jsonParams) throws IOException {
if (null == url || url.isEmpty()) {
throw new RuntimeException("请求参数为空");
}
// 如果是Https请求
if (url.startsWith(HTTPS)) {
getTrust();
}
Connection connection = Jsoup.connect(url);
connection.method(Method.GET);
connection.timeout(TIME_OUT);
connection.ignoreHttpErrors(true);
connection.ignoreContentType(true);
connection.maxBodySize(0);
if (null != headers) {
connection.headers(headers);
}
connection.header(CONTENT_TYPE, JSON_TYPE);
connection.requestBody(jsonParams);
Response response = connection.execute();
return response;
}
/**
* 关流
*
* @param streamList 流集合
*/
private static void closeStream(List <? extends Closeable> streamList) {
if (null != streamList) {
for (Closeable stream : streamList) {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 获取服务器信任
*/
private static void getTrust() {
try {
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, new X509TrustManager[]{new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}}, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
} catch (Exception e) {
e.printStackTrace();
}
}
}
GET/POST调用:
HttpUtils.get("https://search.yehui.cn/esGoods/create/" + id, null, null);
HttpUtils.post("https://search.yehui.cn/esGoods/delete/" + id + id, null, null);