package com.oying.utils; import com.alibaba.fastjson2.JSONObject; import com.oying.exception.BadRequestException; 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.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.ssl.SSLContexts; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; import javax.net.ssl.SSLContext; import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Map; /** * 功能描述: * * @author LIX * @date 创建时间 :2022/6/15 下午4:19 */ public class HttpRequest { /** * 读取请求数据流 * * @param request 请求数据 * @return String */ public static String getRequestBody(HttpServletRequest request) { StringBuilder sb = new StringBuilder(); try (ServletInputStream inputStream = request.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { String line; while ((line = reader.readLine()) != null) { sb.append(line); } } catch (IOException e) { throw new BadRequestException("读取数据流异常"); } return sb.toString(); } /** * 忽略ssl证书验证 * * @return HttpComponentsClientHttpRequestFactory */ public static HttpComponentsClientHttpRequestFactory getFactory() { HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); try { TrustStrategy acceptingTrustStrategy = (chain, authType) -> true; SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build(); SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE); HttpClientBuilder clientBuilder = HttpClients.custom(); CloseableHttpClient httpClient = clientBuilder.setSSLSocketFactory(socketFactory).build(); requestFactory.setHttpClient(httpClient); } catch (Exception e) { throw new BadRequestException("忽略ssl证书验证失败!!!"); } return requestFactory; } private static HttpHeaders getHeaders() { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); return headers; } private static HttpHeaders get() { HttpHeaders headers = new HttpHeaders(); headers.set("accept", "*/*"); headers.set("connection", "Keep-Alive"); headers.set("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); return headers; } /** * http返回String */ public static String exchangeString(HttpMethod httpMethod, String url, Map map) { HttpHeaders headers = get(); HttpEntity httpEntity = new HttpEntity<>(map, headers); RestTemplate restTemplate = new RestTemplate(); return restTemplate.exchange(url, httpMethod, httpEntity, String.class).getBody(); } /** * http返回JSONObject */ public static JSONObject exchangeJsonObject(HttpMethod httpMethod, String url, Map map) { HttpHeaders headers = getHeaders(); HttpEntity httpEntity = new HttpEntity<>(map, headers); RestTemplate restTemplate = new RestTemplate(); return restTemplate.exchange(url, httpMethod, httpEntity, JSONObject.class).getBody(); } }