All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.net.wanmo.common.http.jdk.HttpJdkUtil Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
package cn.net.wanmo.common.http.jdk;

import cn.net.wanmo.common.charset.CharsetUtil;
import cn.net.wanmo.common.codec.CodecUtil;
import cn.net.wanmo.common.http.jdk.enums.Method;
import cn.net.wanmo.common.http.jdk.pojo.ResData;
import cn.net.wanmo.common.http.jdk.pojo.ResObj;
import cn.net.wanmo.common.http.jdk.pojo.UploadFile;
import cn.net.wanmo.common.http.jdk.pojo.Uploader;
import cn.net.wanmo.common.http.jdk.ssl.NullHostNameVerifier;
import cn.net.wanmo.common.http.jdk.ssl.SSLContextFactory;
import cn.net.wanmo.common.http.jdk.util.ResUtil;
import cn.net.wanmo.common.result.HttpResult;
import cn.net.wanmo.common.util.DateUtil;
import cn.net.wanmo.common.util.IdGen;
import cn.net.wanmo.common.util.MapUtil;
import cn.net.wanmo.common.util.StringUtil;
import com.alibaba.fastjson.JSON;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * HTTP 请求
 */
public class HttpJdkUtil {
    private final Logger logger = LoggerFactory.getLogger(getClass());


    private final String CRLF = "\r\n";
    private final String TWO_HYPHEN = "--";
    private final String BOUNDARY = "----WebKitFormBoundary" + IdGen.uuid();

    /**
     * 创建请求
     */
    private static HttpJdkUtil create() {
        HttpJdkUtil httpJdkUtil = new HttpJdkUtil();
        return httpJdkUtil;
    }

    /**
     * 创建请求
     */
    public static HttpJdkUtil get(String url) {
        HttpJdkUtil req = create();
        req.method = Method.GET;
        req.url = url;
        return req;
    }


    /**
     * 创建请求
     */
    public static HttpJdkUtil post(String url) {
        HttpJdkUtil req = create();
        req.method = Method.POST;
        req.url = url;

        req.resJson();
        return req;
    }

    /**
     * 发送请求
     */
    public HttpResult send() {
        return send(false);
    }

    /**
     * 发送请求
     */
    public HttpResult send(boolean download) {
        { // 处理 get 请求的 url
            if (this.method == Method.GET && this.params.isEmpty() == false) {
                String s = toParamStr(this.params);

                if (this.url.contains("?")) {
                    this.url = this.url + "&" + s;
                } else {
                    this.url = this.url + "?" + s;
                }
            }
        }

        HttpResult result;
        if (download) {
            result = sendResAsFile();
        } else {
            result = sendResAsString();
        }


        return result;
    }

    /**
     * 发送请求
     */
    public  HttpResult> send(Obj obj) {
        { // 处理 get 请求的 url
            if (this.method == Method.GET && this.params.isEmpty() == false) {
                String s = toParamStr(this.params);

                if (this.url.contains("?")) {
                    this.url = this.url + "&" + s;
                } else {
                    this.url = this.url + "?" + s;
                }
            }
        }

        HttpResult> res = new HttpResult<>();

        {
            HttpResult r = sendResAsString();

            {
                res.setCode(r.getCode());
                res.setMsg(r.getMsg());
                res.setStartTime(r.getStartTime());
                res.setConsumeTime(r.getConsumeTime());
                res.setDesc(r.getDesc());
            }

            {
                ResData data = r.getData();
                obj.parse(data.getBody());
                data.setObj(obj);

                res.setData(data);
            }
        }

        return res;
    }

    // ---------------------===============---------------------------

    /**
     * 响应 json 数据
     */
    public HttpJdkUtil resJson() {
        // 代表发送端(客户端|服务器)发送的实体数据的数据类型
        this.header.put("Content-Type", "application/json; charset=UTF-8");
        // 代表发送端(客户端)希望接受的数据类型
        this.header.put("accept", "application/json");
        return this;
    }


    // ---------------------===============---------------------------
    /**
     * 请求URL地址
     */
    private String url;
    /**
     * 设置连接主机超时
     */
    private Integer connectTimeout = 3000;
    /**
     * 设置从主机读取数据超时
     */
    private Integer readTimeout = 10000;
    /**
     * 响应解析,默认编码
     */
    private Charset charset = CharsetUtil.UTF8;
    /**
     * 请求方式
     */
    private Method method = Method.GET;
    /**
     * 请求头信息
     */
    private Map header = new HashMap<>();
    /**
     * 请求参数
     */
    private Map params = new HashMap<>();
    /**
     * 上传文件
     */
    private Map uploaders = new HashMap<>();
    /**
     * 请求体
     */
    private String body = StringUtil.EMPTY;
    /**
     * 是否 multipart
     */
    private boolean isMultipart = false;

    /**
     * HTTPS 服务器证书文件
     */
    private String certFilePath = "";
    /**
     * HTTPS 服务器证书密码
     */
    private String certFilePassword = "";

    public HttpJdkUtil() {
    }

    public HttpJdkUtil connectTimeout(Integer connectTimeout) {
        this.connectTimeout = connectTimeout;
        return this;
    }

    public HttpJdkUtil readTimeout(Integer readTimeout) {
        this.readTimeout = readTimeout;
        return this;
    }


    public HttpJdkUtil charset(Charset charset) {
        this.charset = charset;
        return this;
    }


    public HttpJdkUtil header(Map header) {
        this.header = header;
        return this;
    }

    public HttpJdkUtil form(String name, Object value) {
        if (this.method == Method.GET) {
            this.params.put(name, CodecUtil.urlEncode(String.valueOf(value)));
        } else {
            this.params.put(name, value);
        }
        return this;
    }

    public HttpJdkUtil form(String name, File file) {
        return form(name, file, file.getName());
    }

    public HttpJdkUtil form(String name, File file, String filename) {
        Uploader uploader = uploaders.getOrDefault(name, new Uploader());

        uploader.setName(name);
        {
            UploadFile uploadFile = new UploadFile();
            uploadFile.setFile(file);
            uploadFile.setFilename(filename);

            uploader.getFiles().add(uploadFile);
        }
        uploaders.put(name, uploader);

        this.isMultipart = true;
        return this;
    }

    public HttpJdkUtil body(String body) {
        this.body = body;
        return this;
    }

    public HttpJdkUtil certFilePath(String certFilePath) {
        this.certFilePath = certFilePath;
        return this;
    }

    public HttpJdkUtil certFilePassword(String certFilePassword) {
        this.certFilePassword = certFilePassword;
        return this;
    }


// ---------------------======= 发起请求 解析响应 ========---------------------------

    /**
     * 发起 HTTP 请求并获取结果
     */
    private HttpResult sendResAsString() {
        HttpResult result = new HttpResult();

        HttpURLConnection conn = null;
        try {
            conn = getConnection();
            result.setCode(conn.getResponseCode());
            result.setMsg("Http请求完成");

            String charset = ResUtil.getResponseCharset(conn.getContentType());
            String resBody = ResUtil.getResAsString(conn.getInputStream(), charset);
            Map> headerFields = conn.getHeaderFields();

            result.setData(ResData.build(resBody, headerFields));
            logger.debug("响应头: {}", headerFields);
            logger.debug("响应数据: {}", resBody);
        } catch (Exception e) {
            result.error("Http请求发生异常: " + e.getMessage());
            logger.error("Http请求发生异常: " + this.url, e);
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }

        logger.debug("响应结果:{} {}", result.getCode(), result.getMsg());
        return result;
    }

    /**
     * 发起 HTTP 请求并获取结果
     */
    private HttpResult sendResAsFile() {
        HttpResult result = new HttpResult();
        HttpURLConnection conn = null;
        long startTime = DateUtil.nowLong();

        try {
            conn = getConnection();
            result.setCode(conn.getResponseCode());
            result.setMsg("Http请求完成");

            InputStream is = conn.getInputStream(); // 请求后远程返回的数据
            File file = ResUtil.getResAsFile(is);
            Map> headerFields = conn.getHeaderFields();

            ResData resData = ResData.build(file, headerFields);
            resData.setBody(file.getAbsolutePath());
            result.setData(resData);
            logger.debug("响应数据: {}", result.getData());
        } catch (Exception e) {
            result.error("Http请求发生异常: " + e.getMessage());
            logger.error("Http请求发生异常: " + this.url, e);
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }

        result.setConsumeTime(DateUtil.nowLong() - startTime);
        logger.debug("响应结果:代码 {} , 消息 {}", result.getCode(), result.getMsg());
        return result;
    }


// ---------------------======= 初始化连接 ========---------------------------

    private void initConnection(HttpURLConnection conn) throws ProtocolException {
        {
            // 设置请求方式 GET
            conn.setRequestMethod(this.method.getValue());
        }
        { // 设置超时
            conn.setConnectTimeout(this.connectTimeout); // 设置连接主机超时(单位:毫秒)
            conn.setReadTimeout(this.readTimeout); // 设置从主机读取数据超时(单位:毫秒)
        }

        { // 设置常规属性
            /*
             * 设置请求头或响应头:HTTP请求允许一个key带多个用逗号分开的values,但是HttpURLConnection只提供了单个操作的方法:
             * setRequestProperty(key,value):会覆盖已经存在的key的所有values,有清零重新赋值的作用
             * addRequestProperty(key,value):是在原来key的基础上继续添加其他value
             */
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            conn.setRequestProperty("accept", "*/*"); // 设置接受的文件类型,*表示一切可以接受的
            conn.setRequestProperty("Accept-Charset", "UTF-8"); // 新添加的请求头编码
//            conn.setRequestProperty("connection", "Keep-Alive"); // 设置维持长连接
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

            // 设定传送的内容类型是可序列化的java对象
            // (如果不设此项,在传送序列化对象时,当WEB服务默认的不是这种类型时可能抛java.io.EOFException)
            // conn.setRequestProperty("Content-type", "application/x-java-serialized-object");
        }

        if (MapUtil.isNotEmpty(this.header)) { // 属性覆盖或新增
            for (Map.Entry entry : this.header.entrySet()) {
                conn.setRequestProperty(entry.getKey(), entry.getValue());
            }
        }
    }

    private void initGet(HttpURLConnection conn) {
        // 设置是否使用缓存 默认是true
        conn.setUseCaches(true);
    }


    private void initPost(HttpURLConnection conn) throws Exception {
        // 设置是否从 httpUrlConnection 允许读入,默认情况下是 true;
        conn.setDoInput(true);
        // 默认情况下是 false; 设置是否向 httpUrlConnection 允许输出,因为这个是 post 请求,参数要放在 HTTP 正文内,因此需要设为 true,
        conn.setDoOutput(true);
        // Post 请求不能使用缓存
        conn.setUseCaches(false);

        if (isMultipart == false) {
            if (MapUtil.isNotEmpty(params)) {
                { // 请求开始
                    conn.setRequestProperty("Content-Type", "multipart/form-data; charset=UTF-8; boundary=" + BOUNDARY); // 定义数据分隔线
                    conn.setRequestProperty("Content-Length", String.valueOf(BOUNDARY.length())); // 设置 boundary 的长度
                }

                OutputStream out = conn.getOutputStream();  // 获取输出流
                for (Map.Entry entry : this.params.entrySet()) {
                    StringBuilder paramData = new StringBuilder();
                    paramData.append(TWO_HYPHEN).append(BOUNDARY).append(CRLF);
                    paramData.append("Content-Disposition: form-data; name=" + entry.getKey()).append(CRLF);
                    paramData.append("Content-Type: text/plain; charset=utf-8").append(CRLF);
                    paramData.append("Content-Transfer-Encoding: 8bit").append(CRLF);
                    paramData.append(CRLF); // 参数头设置完以后需要两个换行,然后才是参数内容
                    paramData.append(entry.getValue());
                    paramData.append(CRLF);
                    String paramDataStr = paramData.toString();
                    out.write(paramDataStr.getBytes());
                    out.flush();
                    logger.debug("isMultipart paramData:\r\n {}", paramDataStr);
                }

                { // 请求截止
                    byte[] end_data = (TWO_HYPHEN + BOUNDARY + TWO_HYPHEN + CRLF).getBytes();
                    out.write(end_data);
                    out.flush();
                    IOUtils.closeQuietly(out);
                }
            }

            if (StringUtil.isNotBlank(this.body)) { // 如果请求体不为空
                byte[] paramsBytes = this.body.getBytes(this.charset);

                OutputStream out = conn.getOutputStream();  // 获取输出流
                { // 当有数据需要提交时,请求参数传给服务器
                    out.write(paramsBytes);
                    out.flush();
                }
            }

        }

        if (isMultipart == true) {

            { // 请求开始
                conn.setRequestProperty("Content-Type", "multipart/form-data; charset=UTF-8; boundary=" + BOUNDARY); // 定义数据分隔线
                conn.setRequestProperty("Content-Length", String.valueOf(BOUNDARY.length())); // 设置 boundary 的长度
                conn.setRequestProperty("Connection", "Keep-Alive");
            }

//            OutputStream out = conn.getOutputStream();  // 获取输出流
            OutputStream out = new DataOutputStream(conn.getOutputStream());
            { // 表单参数
                {
                    if (MapUtil.isEmpty(params)) {
                        params = new HashMap<>();
                    }
                    params.putIfAbsent("description", "文件上传");
                }

                for (Map.Entry entry : this.params.entrySet()) {
                    StringBuilder paramData = new StringBuilder();
                    paramData.append(TWO_HYPHEN).append(BOUNDARY).append(CRLF);
                    paramData.append("Content-Disposition: form-data; name=" + entry.getKey()).append(CRLF);
                    paramData.append("Content-Type: text/plain; charset=utf-8").append(CRLF);
                    paramData.append("Content-Transfer-Encoding: 8bit").append(CRLF);
                    paramData.append(CRLF); // 参数头设置完以后需要两个换行,然后才是参数内容
                    paramData.append(entry.getValue());
                    paramData.append(CRLF);
                    String paramDataStr = paramData.toString();
                    out.write(paramDataStr.getBytes());
                    out.flush();
                    logger.debug("isMultipart paramData:\r\n {}", paramDataStr);
                }
            }

            for (Uploader uploader : this.uploaders.values()) { // 文件参数
                String name = uploader.getName();
                List files = uploader.getFiles();

                for (UploadFile uploadFile : files) {
                    { // 开始构建 multipart/form-data 格式的数据包
                        StringBuilder mediaData = new StringBuilder();
                        mediaData.append(TWO_HYPHEN).append(BOUNDARY).append(CRLF);
                        mediaData.append("Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + uploadFile.getFilename() + "\"; filelength=" + uploadFile.getFile().length() + "; digest=" + uploadFile.getDigest() + CRLF);
                        String contentType = URLConnection.guessContentTypeFromName(uploadFile.getFile().getName());
                        mediaData.append("Content-Type:" + (StringUtil.isBlank(contentType) ? "application/octet-stream" : contentType) + "; charset=UTF-8" + CRLF);
                        mediaData.append("Content-Transfer-Encoding: binary" + CRLF);
                        mediaData.append(CRLF); // 参数头设置完以后需要两个换行,然后才是参数内容
                        String mediaDataStr = mediaData.toString();
                        out.write(mediaDataStr.getBytes());
                        out.flush();
                        logger.debug("isMultipart mediaData: \r\n {}", mediaDataStr);
                    }
                    { // 文件上传
                        DataInputStream fs = new DataInputStream(new FileInputStream(uploadFile.getFile()));
                        int bytes = 0;
                        byte[] bufferOut = new byte[1024];
                        while ((bytes = fs.read(bufferOut)) != -1) {
                            out.write(bufferOut, 0, bytes);
                        }
                        out.flush();
                        IOUtils.closeQuietly(fs);
                    }
                    { // 换行
                        byte[] end_data = (CRLF).getBytes();
                        out.write(end_data);
                        out.flush();
                    }
                }
            }

            { // 请求截止
                byte[] end_data = (TWO_HYPHEN + BOUNDARY + TWO_HYPHEN + CRLF).getBytes();
                out.write(end_data);
                out.flush();
            }

            {
                IOUtils.closeQuietly(out);
            }
        }
    }


    // ---------------------======= 根据 http https 获取连接 ========---------------------------
    private HttpURLConnection getConnection() throws Exception {
        logger.debug("请求URL: {}", this.url);
        logger.debug("请求方式: {}", this.method);
        logger.debug("请求头: {}", JSON.toJSONString(this.header, true));
        logger.debug("请求数据: {}", this.body);
        logger.debug("请求文件: {}", JSON.toJSONString(this.uploaders.values(), true));

        URL url = new URL(this.url);

        HttpURLConnection conn = null;
        if ("HTTPS".equals(url.getProtocol().toUpperCase())) { // 如果是安全连接则需要
            conn = getConnectionHttps(url);
        } else {
            conn = getConnectionHttp(url);
        }

        initConnection(conn);

        switch (method) { // 根据请求方式,初始化 conn
            case GET:
                initGet(conn);
                break;
            case POST:
                initPost(conn);
                break;
            default:
                throw new RuntimeException("不支持的请求方式:" + method);
        }

        conn.connect();
        return conn;
    }

    /**
     * 获取 http 连接
     *
     * @param url
     * @return
     * @throws Exception
     */
    private HttpURLConnection getConnectionHttp(URL url) throws Exception {
        return (HttpURLConnection) url.openConnection();
    }

    /**
     * 获取 https 连接
     *
     * @param url
     * @return
     * @throws Exception
     */
    private HttpsURLConnection getConnectionHttps(URL url) throws Exception {
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setHostnameVerifier(new NullHostNameVerifier());
//			connectionHttps.setSSLSocketFactory(getSSLSocket());
        conn.setSSLSocketFactory(SSLContextFactory.createSSLContext(this.certFilePath, this.certFilePassword).getSocketFactory());
        return conn;
    }


// ---------------------======= 工具方法 ========---------------------------

    /**
     * 将map转为 参数字符串:name1=value1&name2=value2 的形式
     *
     * @param param
     * @return 字符串
     * @throws UnsupportedEncodingException
     */
    public static String toParamStr(Map param) {
        String reStr = "";

        if (MapUtil.isEmpty(param)) {
            return reStr;
        }

        Set> entrySet = param.entrySet();
        for (Map.Entry o : entrySet) {
            if (o.getValue() == null || "null".equals(o.getValue()) || "class".equals(o.getKey())) {
                continue;
            }
            String s = o.getKey() + "=" + o.getValue();
            reStr += (s + "&");
        }

        return StringUtil.isBlank(reStr) ? "" : reStr.substring(0, reStr.length() - 1);
    }

    /**
     * 将字符串 (name1=value1&name2=value2)的形式,转为 Map 形式
     *
     * @param param
     * @return Map
     * @throws UnsupportedEncodingException
     */
    public static Map toParamMap(String param) {
        Map paramMap = new HashMap<>();
        if (StringUtil.isBlank(param)) {
            return paramMap;
        }

        String[] paramPairsArr = param.split("&");
        for (String paramPair : paramPairsArr) {
            String[] paramPairArr = paramPair.split("=");
            if (paramPairArr.length == 2) {
                paramMap.put(paramPairArr[0].trim(), paramPairArr[1].trim());
            } else {
                paramMap.put(paramPairArr[0].trim(), "");
            }

        }

        return paramMap;
    }

    /**
     * 判断是否是 HTTP URL
     *
     * @param url 请求 url
     * @return 布尔值
     */
    public static Boolean isHttpUrl(String url) {
        boolean flag = false;

        try {
            flag = url.indexOf("://") != -1;
        } catch (Exception e) {
        }

        return flag;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy