美文网首页
使用spring的RestTemplate发送HTTP请求

使用spring的RestTemplate发送HTTP请求

作者: piupiu_7da3 | 来源:发表于2020-08-18 14:47 被阅读0次

    在一个spring-boot项目中,需要请求第三方API,使用JDK原生的URLConnection过于繁琐,功能强大的Apache.HttpClient又需要额外引入,导致微服务程序变大。搜索发现spring原生的RestTemplate已经内置到了spring-boot-starter-web,可以开箱即用,所以实现了一个基于RestTemplate的工具类,如下

    WebHelper
    
    import org.apache.commons.lang.IllegalClassException;
    import org.apache.http.impl.client.HttpClients;
    import org.springframework.http.*;
    import org.springframework.http.client.SimpleClientHttpRequestFactory;
    import org.springframework.http.converter.HttpMessageConverter;
    import org.springframework.http.converter.StringHttpMessageConverter;
    import org.springframework.lang.NonNull;
    import org.springframework.lang.Nullable;
    import org.springframework.web.client.RestTemplate;
    
    import javax.net.ssl.HttpsURLConnection;
    import javax.net.ssl.SSLContext;
    import javax.net.ssl.TrustManager;
    import javax.net.ssl.X509TrustManager;
    import javax.servlet.http.HttpServletRequest;
    import java.io.IOException;
    import java.net.HttpURLConnection;
    import java.nio.charset.StandardCharsets;
    import java.security.cert.X509Certificate;
    import java.util.List;
    import java.util.Map;
    
    /**
     * @author frank
     * @date 2020-07-31 15:34
     */
    public class WebHelper {
    
        private static final String[] IP_HEADER_CANDIDATES = {
                "X-Forwarded-For",
                "Proxy-Client-IP",
                "WL-Proxy-Client-IP",
                "HTTP_X_FORWARDED_FOR",
                "HTTP_X_FORWARDED",
                "HTTP_X_CLUSTER_CLIENT_IP",
                "HTTP_CLIENT_IP",
                "HTTP_FORWARDED_FOR",
                "HTTP_FORWARDED",
                "HTTP_VIA",
                "REMOTE_ADDR"
        };
    
        /**
         * 获取客户端 IP 地址
         *
         * @param request 请求实例
         * @return 客户端 IP
         */
        public static @Nullable
        String getClientIpAddress(HttpServletRequest request) {
            for (String header : IP_HEADER_CANDIDATES) {
                String ip = request.getHeader(header);
                if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
                    return ip;
                }
            }
            return request.getRemoteAddr();
        }
    
        /**
         * 发送 HTTPS.GET 请求
         *
         * @param url     请求地址
         * @param params  请求参数
         * @param headers 请求头
         * @return 响应实体,可能需要对响应结果的状态码进行处理,所以直接返回响应实体
         */
        public static ResponseEntity<String> sendGet(String url, Map<String, String> params, Map<String, String> headers) {
            return sendRequest(url, params, null, headers, HttpMethod.GET, Boolean.TRUE);
        }
    
        /**
         * 发送 HTTPS.POST 请求
         *
         * @param url     请求地址
         * @param body    请求体
         * @param headers 请求头
         * @return 响应实体,可能需要对响应结果的状态码进行处理,所以直接返回响应实体
         */
        public static ResponseEntity<String> sendPost(String url, String body, Map<String, String> headers) {
            HttpClients.createDefault();
            return sendRequest(url, null, body, headers, HttpMethod.POST, Boolean.TRUE);
        }
    
        /**
         * 使用 springboot 内置的 RestTemplate 发送 http 请求
         *
         * @param url     请求地址
         * @param params  请求参数
         * @param body    请求体
         * @param headers 请求头
         * @param method  请求方法
         * @param useSSL  是否使用安全套接字
         * @return 响应实体,可能需要对响应结果的状态码进行处理,所以直接返回响应实体
         */
        public static ResponseEntity<String> sendRequest(String url, Map<String, String> params, String body,
                                                         Map<String, String> headers, HttpMethod method, boolean useSSL) {
            RestTemplate rt = useSSL
                    ? new RestTemplate(new HttpsClientRequestFactory())
                    : new RestTemplate();
    
            // 字符编码设为 UTF-8
            List<HttpMessageConverter<?>> converters = rt.getMessageConverters();
            converters.set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
            rt.setMessageConverters(converters);
    
            HttpHeaders httpHeaders = new HttpHeaders();
            if (!MapHelper.isEmpty(headers)) {
                httpHeaders.setAll(headers);
            }
    
            HttpEntity<String> httpEntity = new HttpEntity<>(body, httpHeaders);
            if (!MapHelper.isEmpty(params)) {
                url = StringHelper.append(url, "?", MapHelper.toUrl(params, "UTF-8"));
            }
    
            return rt.exchange(url, method, httpEntity, String.class);
        }
    
        /**
         * 覆盖 SimpleClientHttpRequestFactory 的安全套接字层SSL
         */
        public static class HttpsClientRequestFactory extends SimpleClientHttpRequestFactory {
            @Override
            protected void prepareConnection(@NonNull HttpURLConnection connection, @NonNull String httpMethod) throws IOException {
    
                // to https connection
                if (!(connection instanceof HttpsURLConnection)) {
                    throw new IllegalClassException("An instance of HttpsURLConnection is expected");
                }
                HttpsURLConnection httpsConnection = (HttpsURLConnection) connection;
    
                ExceptionExecutor.withTry(() -> {
    
                    // new empty X509TrustManager
                    TrustManager[] trustAllCerts = new TrustManager[]{
                            new X509TrustManager() {
                                @Override
                                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                                    return null;
                                }
    
                                @Override
                                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                                }
    
                                @Override
                                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                                }
                            }
                    };
                    SSLContext sslContext = SSLContext.getInstance("SSL");
                    sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
    
                    // set to connection
                    httpsConnection.setSSLSocketFactory(sslContext.getSocketFactory());
                    httpsConnection.setHostnameVerifier((s, sslSession) -> true);
                    return null;
                });
    
                super.prepareConnection(httpsConnection, httpMethod);
            }
        }
    
    }
    
    MapHelper
    
    import java.io.UnsupportedEncodingException;
    import java.net.URLEncoder;
    import java.util.Map;
    import java.util.stream.Collectors;
    
    public class MapHelper {
        /**
         * 根据指定字符集完成 URL 编码,构造 URL 参数字符串
         *
         * @param map     {@code {"name":"frank", "gender":"male"}}
         * @param charset URLEncode 使用的字符集
         * @return name=frank&gender=male
         */
        public static <K, V> String toUrl(Map<K, V> map, String charset) {
            return map.entrySet().stream()
                    .map(e -> {
                        K k = e.getKey();
                        String val = "";
                        try {
                            URLEncoder.encode(StringHelper.toStr(e.getValue()), charset);
                        } catch (UnsupportedEncodingException ex) {
                            ex.printStackTrace();
                        }
                        return StringHelper.join("=", k, val);
                    })
                    .collect(Collectors.joining("&"));
        }
    }
    
    StringHelper
    
    import java.util.Arrays;
    import java.util.Objects;
    import java.util.stream.Collectors;
    
    public class StringHelper {
    
        private static final String EMPTY = "";
    
        /**
         * 以字符串形式拼接多个参数
         *
         * @param params 如果整个数组为空,返回空串;如果其中值为空,将转为空串
         */
        public static String append(Object... params) {
            return join(EMPTY, params);
        }
    
        /**
         * 以字符串形式拼接多个参数,以指定字符串分隔
         *
         * @param separator 分隔参数的字符串
         * @param params    如果整个数组为空,返回空串;如果其中值为空,将转为空串
         */
        public static String join(String separator, Object... params) {
            if (params == null || params.length == 0) {
                return EMPTY;
            }
            return Arrays.stream(params)
                    .map(StringHelper::toStr)
                    .collect(Collectors.joining(separator));
        }
    
        /**
         * Object 转 String,如果为 null 时返回值为空串而不是 "null"
         */
        public static String toStr(Object obj) {
            return Objects.toString(obj, EMPTY);
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:使用spring的RestTemplate发送HTTP请求

          本文链接:https://www.haomeiwen.com/subject/svisrktx.html