|
@@ -0,0 +1,152 @@
|
|
|
+package com.ruoyi.common.utils.http;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.client.config.RequestConfig;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.client.utils.URIBuilder;
|
|
|
+import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.message.BasicHeader;
|
|
|
+import org.apache.http.protocol.HTTP;
|
|
|
+import org.apache.http.ssl.SSLContexts;
|
|
|
+import org.apache.http.ssl.TrustStrategy;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+import javax.net.ssl.SSLContext;
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.security.KeyManagementException;
|
|
|
+import java.security.KeyStoreException;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.security.cert.CertificateException;
|
|
|
+import java.security.cert.X509Certificate;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @ClassName HttpUtil
|
|
|
+ * @Description: TODO
|
|
|
+ * @Author LX
|
|
|
+ * @Date 2023/6/19
|
|
|
+ * @Version V1.0
|
|
|
+ **/
|
|
|
+public class HttpUtil {
|
|
|
+
|
|
|
+ private final static String mStrClassName = "HttpUtil";
|
|
|
+
|
|
|
+ private final static Logger logger = LoggerFactory.getLogger(HttpUtil.class);
|
|
|
+
|
|
|
+ public static String postJosn(JSONObject jsonObject, String url){
|
|
|
+ try {
|
|
|
+ String body = "";
|
|
|
+//创建httpclient对象
|
|
|
+ CloseableHttpClient client = HttpClients.createDefault();
|
|
|
+ RequestConfig requestConfig = RequestConfig.custom()
|
|
|
+ .setConnectionRequestTimeout(20000)
|
|
|
+ .setSocketTimeout(10000)
|
|
|
+ .setConnectTimeout(20000).build();
|
|
|
+//创建post方式请求对象
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ httpPost.setConfig(requestConfig);
|
|
|
+//装填参数
|
|
|
+// jsonObject 是需要封装的参数
|
|
|
+ System.out.println("http:"+jsonObject.toJSONString());
|
|
|
+ StringEntity s = new StringEntity(jsonObject.toString(), "utf-8");
|
|
|
+ s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
|
|
|
+//设置参数到请求对象中
|
|
|
+ httpPost.setEntity(s);
|
|
|
+//设置header信息
|
|
|
+//指定报文头【Content-type】、【User-Agent】
|
|
|
+//httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
|
|
|
+ httpPost.setHeader("Content-type", "application/json");
|
|
|
+ httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
|
|
|
+//执行请求操作,并拿到结果(同步阻塞)
|
|
|
+ CloseableHttpResponse response = client.execute(httpPost);
|
|
|
+//获取结果实体
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ if (entity != null) {
|
|
|
+//按指定编码转换结果实体为String类型
|
|
|
+ body = EntityUtils.toString(entity, "utf-8");
|
|
|
+ }
|
|
|
+ EntityUtils.consume(entity);
|
|
|
+//释放链接
|
|
|
+ response.close();
|
|
|
+ return body;
|
|
|
+ }catch(Exception ex){
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getJson(String url) {
|
|
|
+ logger.info("=====================HTTP GET 请求参数 url=" + url);
|
|
|
+ InputStream is = null;
|
|
|
+ String body = null;
|
|
|
+ StringBuilder res = new StringBuilder();
|
|
|
+ // 实例化CloseableHttpClient
|
|
|
+// CloseableHttpClient client = HttpClients.createDefault();
|
|
|
+ CloseableHttpClient client = getScontractHttpClient();
|
|
|
+ CloseableHttpResponse response = null;
|
|
|
+ try {
|
|
|
+ // 添加URL和请求参数
|
|
|
+ //替换空格;get请求,url拼接参数时,参数值可能存在空格,需要对空格做处理,不然接口会调不通
|
|
|
+ url = url.replaceAll(" ", "%20");
|
|
|
+ URIBuilder ub = new URIBuilder(url);
|
|
|
+ // 使用get方法添加URL
|
|
|
+ HttpGet get = new HttpGet(ub.build());
|
|
|
+ // 设置请求超时时间
|
|
|
+ RequestConfig config = RequestConfig.custom().setConnectTimeout(1500).build();
|
|
|
+ get.setConfig(config);
|
|
|
+ //使用http调用远程,获取相应信息
|
|
|
+ response = client.execute(get);
|
|
|
+ // 获取响应状态码,可根据是否响应正常来判断是否需要进行下一步
|
|
|
+ int statusCode = response.getStatusLine().getStatusCode();
|
|
|
+ if (statusCode != 200) {
|
|
|
+ return "请求失败 statusCode=" + statusCode;
|
|
|
+ }
|
|
|
+ // 获取响应实体
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ // 读取响应内容
|
|
|
+ if (entity != null) {
|
|
|
+ is = entity.getContent();
|
|
|
+ BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
|
|
+ while ((body = br.readLine()) != null) {
|
|
|
+ res.append(body);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error(e.getMessage());
|
|
|
+ }
|
|
|
+ logger.info("=====================HTTP GET 请求响应参数 result=" + res.toString());
|
|
|
+ return res.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ //TODO 跳过HTTPS检查
|
|
|
+ public static CloseableHttpClient getScontractHttpClient() {
|
|
|
+ SSLContext sslContext = null;
|
|
|
+ try {
|
|
|
+ sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
|
|
|
+ @Override
|
|
|
+ public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }).build();
|
|
|
+ } catch (NoSuchAlgorithmException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (KeyManagementException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (KeyStoreException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ //创建httpClient
|
|
|
+ return HttpClients.custom().setSSLContext(sslContext).
|
|
|
+ setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
|
|
|
+
|
|
|
+ }
|
|
|
+}
|