You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
4.4 KiB
131 lines
4.4 KiB
|
4 years ago
|
/*
|
||
|
|
* Copyright (c) 2021
|
||
|
|
* User:王绍全
|
||
|
|
* File:RequestHelper.java
|
||
|
|
* Date:2021/07/23 12:02:23
|
||
|
|
*/
|
||
|
|
|
||
|
|
package com.ynxbd.push.helper;
|
||
|
|
|
||
|
|
import com.alibaba.fastjson.JSON;
|
||
|
|
import lombok.extern.slf4j.Slf4j;
|
||
|
|
import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
||
|
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||
|
|
import org.apache.http.conn.ssl.TrustStrategy;
|
||
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||
|
|
import org.apache.http.impl.client.HttpClients;
|
||
|
|
import org.apache.http.ssl.SSLContextBuilder;
|
||
|
|
import org.springframework.http.HttpEntity;
|
||
|
|
import org.springframework.http.HttpHeaders;
|
||
|
|
import org.springframework.http.ResponseEntity;
|
||
|
|
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||
|
|
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||
|
|
import org.springframework.util.MultiValueMap;
|
||
|
|
import org.springframework.web.client.RestTemplate;
|
||
|
|
|
||
|
|
import javax.net.ssl.SSLContext;
|
||
|
|
import java.security.KeyManagementException;
|
||
|
|
import java.security.KeyStoreException;
|
||
|
|
import java.security.NoSuchAlgorithmException;
|
||
|
|
import java.util.List;
|
||
|
|
import java.util.Map;
|
||
|
|
|
||
|
|
@Slf4j
|
||
|
|
public class RequestHelper extends SimpleClientHttpRequestFactory {
|
||
|
|
|
||
|
|
|
||
|
|
public static RestTemplate getRestTemplate() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
|
||
|
|
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (TrustStrategy) (arg0, arg1) -> true).build();
|
||
|
|
|
||
|
|
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext,
|
||
|
|
new String[]{"TLSv1.2", "TLSv1.1"},
|
||
|
|
null,
|
||
|
|
NoopHostnameVerifier.INSTANCE);
|
||
|
|
|
||
|
|
CloseableHttpClient httpClient = HttpClients.custom()
|
||
|
|
.setSSLSocketFactory(csf)
|
||
|
|
.build();
|
||
|
|
|
||
|
|
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
|
||
|
|
requestFactory.setHttpClient(httpClient);
|
||
|
|
return new RestTemplate(requestFactory);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 向目的URL发送post请求
|
||
|
|
*
|
||
|
|
* @param url 目的url
|
||
|
|
* @param params 发送的参数
|
||
|
|
* @return ResultVO
|
||
|
|
*/
|
||
|
|
public static <T> T post(String url, MultiValueMap<String, Object> params, Class<T> clazz) throws Exception {
|
||
|
|
HttpHeaders headers = new HttpHeaders();
|
||
|
|
headers.add("Content-Type", "application/x-www-form-urlencoded");
|
||
|
|
HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(params, headers);
|
||
|
|
return getRestTemplate().postForObject(url, request, clazz);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 向目的URL发送get请求
|
||
|
|
*
|
||
|
|
* @param url 请求地址
|
||
|
|
* @param params 请求参数
|
||
|
|
* @return ResultVO
|
||
|
|
*/
|
||
|
|
public static <T> T get(String url, Map<String, String> params, Class<T> clazz) {
|
||
|
|
RestTemplate client = new RestTemplate();
|
||
|
|
HttpHeaders headers = new HttpHeaders();
|
||
|
|
headers.add("Content-Type", "application/x-www-form-urlencoded");
|
||
|
|
HttpEntity<Map<String,String>> request = new HttpEntity<>(params,headers);
|
||
|
|
ResponseEntity<String> responseEntity = client.getForEntity(url, String.class, request);
|
||
|
|
String data = responseEntity.getBody();
|
||
|
|
return JSON.parseObject(data, clazz);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取post流中的参数
|
||
|
|
*
|
||
|
|
* @param name 参数名称
|
||
|
|
* @param paramsMap 请求参数
|
||
|
|
* @param clazz 集合类型
|
||
|
|
* @return list
|
||
|
|
*/
|
||
|
|
public static <T> List<T> getBodyList(Map<String, Object> paramsMap, String name, Class<T> clazz) {
|
||
|
|
Object param = paramsMap.get(name);
|
||
|
|
if (param == null) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
String json = JSON.toJSONString(param);
|
||
|
|
if ("".equals(json)) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
return JSON.parseArray(json, clazz);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取post流中的参数
|
||
|
|
*
|
||
|
|
* @param name 参数名称
|
||
|
|
* @param paramsMap 请求参数
|
||
|
|
* @return list
|
||
|
|
*/
|
||
|
|
public static String getBodyString(Map<String, Object> paramsMap, String name) {
|
||
|
|
Object param = paramsMap.get(name);
|
||
|
|
if (param == null) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
String paramStr = String.valueOf(param);
|
||
|
|
if (!"null".equals(paramStr)) {
|
||
|
|
return paramStr;
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
public static void main(String[] args) {
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|