zoukankan      html  css  js  c++  java
  • http请求数据封装

    package com.wdm.utils;
    
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.Map;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class HttpClientUtils {
    
    
        private static Logger LOGGER = LoggerFactory.getLogger(HttpClientUtils.class);
    
        private static int CONNECT_TIME_OUT = 1000;
    
        private static int READ_TIME_OUT = 1000;
    
        private static byte[] BUFFER = new byte[1024];
    
        public static final String DEFAULT_CHARSET = "UTF-8";
    
        public static String get(String url) {
            return get(url, null, DEFAULT_CHARSET);
        }
    
        public static String get(String url, String charset) {
            return get(url, null, charset);
        }
    
        public static String get(String url, Map<String, String> header, String charset) {
            return get(url, header, charset, CONNECT_TIME_OUT, READ_TIME_OUT);
        }
    
        public static String get(String url, Map<String, String> header,  String charset, 
                int connectTimeout, int readTimeout) {
            String result = "";
            try {
                HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
                connection.setUseCaches(false);
                connection.setConnectTimeout(connectTimeout);
                connection.setReadTimeout(readTimeout);
    
                if (header != null) {
                    for (Map.Entry<String, String> entry : header.entrySet()) {
                        connection.setRequestProperty(entry.getKey(), entry.getValue());
                    }
                }
    
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                int responseCode = connection.getResponseCode();
                if (responseCode < HttpURLConnection.HTTP_BAD_REQUEST) {
                    InputStream is = connection.getInputStream();
                    int readCount;
                    while ((readCount = is.read(BUFFER)) > 0) {
                        out.write(BUFFER, 0, readCount);
                    }
                    is.close();
                } else {
                    LOGGER.warn("{} http response code is {}", url, responseCode);
                }
                connection.disconnect();
                result = out.toString();
            } catch (IOException e) {
                LOGGER.error("{}", e.getMessage(), e);
            }
            return result;
        }
    
        public static String post(String url, Map<String, String> params) {
            return post(url, params, DEFAULT_CHARSET);
        }
    
        public static String post(String url, Map<String, String> params, String charset) {
            return post(url, params, null, charset);
        }
    
        public static String post(String url, Map<String, String> params, Map<String, String> header, String charset) {
            return post(url, params, header, charset, CONNECT_TIME_OUT, READ_TIME_OUT);
        }
    
        public static String post(String url, Map<String, String> params, Map<String, String> header, 
                String charset, int connectTimeout, int readTimeout) {
            String result = "";
            try {
                HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
                connection.setRequestMethod("POST");
                connection.setDoOutput(true);
                connection.setUseCaches(false);
                connection.setConnectTimeout(connectTimeout);
                connection.setReadTimeout(readTimeout);
    
                if (header != null) {
                    for (Map.Entry<String, String> entry : header.entrySet()) {
                        connection.setRequestProperty(entry.getKey(), entry.getValue());
                    }
                }
    
                StringBuilder builder = new StringBuilder();
                if (params != null) {
                    for (Map.Entry<String, String> entry : params.entrySet()) {
                        builder.append(entry.getKey());
                        builder.append("=");
                        builder.append(entry.getValue());
                        builder.append("&");
                    }
                }
    
                OutputStream out = connection.getOutputStream();
                out.write(builder.toString().getBytes(charset));
                out.flush();
    
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                if (connection.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) {
                    InputStream is = connection.getInputStream();
                    int readCount;
                    while ((readCount = is.read(BUFFER)) > 0) {
                        bout.write(BUFFER, 0, readCount);
                    }
                    is.close();
                }
                connection.disconnect();
                result = bout.toString();
            } catch (IOException e) {
                LOGGER.error("{}", e.getMessage(), e);
            }
            return result;
        }
    }
  • 相关阅读:
    关于云原生应用的思考
    动手实现 LRU 算法,以及 Caffeine 和 Redis 中的缓存淘汰策略
    Spring5-Reactor函数式编程
    架构简洁之道:从阿里开源应用架构 COLA 说起
    如何优雅地运用位运算实现产品需求?
    如何优雅地运用位运算实现产品需求?
    图形处理:给 Canvas 文本填充线性渐变
    深入理解EnableAutoConfiguration原理
    pwnable.tw之3x17
    WebRTC之完整搭建Jitsi Meet指南
  • 原文地址:https://www.cnblogs.com/hawk-whu/p/6759234.html
Copyright © 2011-2022 走看看