fhx
2024-05-22 e6dfe7752f934d8e9f166daa7f338614288aecd9
新增请求方法
1个文件已修改
81 ■■■■■ 已修改文件
src/main/java/com/hx/util/HttpMethodUtil.java 81 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/hx/util/HttpMethodUtil.java
@@ -1,10 +1,12 @@
package com.hx.util;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
/**
@@ -76,4 +78,83 @@
        return wxMsgXml;
    }
    /** 请求http协议 获取信息工具
     * @param url 请求链接
     * @param data 请求数据(body)
     * @param keyValues form表单数据 key参数名称,value参数值
     * @param header 请求头
     * @param requestMethod 请求头方法,默认POST
     * @return
     */
    public static String request(String url, String data,Map<String,Object> keyValues,Map<String,String> header,String requestMethod, String contentType) {
        HttpURLConnection con = null;
        URL u = null;
        String wxMsgXml = null;
        try {
            StringBuilder dataP = new StringBuilder();
            if (keyValues != null && !keyValues.isEmpty()) {
                for (Map.Entry<String, Object> entry : keyValues.entrySet()) {
                    dataP.append((String)entry.getKey()).append("=");
                    dataP.append(entry.getValue());
                    dataP.append("&");
                }
                System.out.println("dataP:"+dataP.toString());
                dataP.deleteCharAt(dataP.length() - 1);
                url = url+"?"+dataP;
            }
            if(StringUtils.isEmpty(requestMethod)){
                requestMethod = "POST";
            }
            if(StringUtils.isEmpty(contentType)){
                contentType = "application/json";
            }
            u = new URL(url);
            con = (HttpURLConnection) u.openConnection();
            con.setRequestMethod(requestMethod);
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setUseCaches(false);
            con.setReadTimeout(300000);
            con.setRequestProperty("Charset", "UTF-8");
            con.setRequestProperty("Content-Type", contentType);
            if(header != null){
                for (Map.Entry<String, String> entry : header.entrySet()) {
                    con.setRequestProperty(entry.getKey(),entry.getValue());
                }
            }
            if (data != null) {
                OutputStream os = con.getOutputStream();
                os.write(data.getBytes("utf-8"));
            }
            if(HttpURLConnection.HTTP_OK != con.getResponseCode() && HttpURLConnection.HTTP_CREATED != con.getResponseCode()){
                throw new RuntimeException("请求url失败:"+con.getResponseCode());
            }
            // 读取返回内容
            wxMsgXml = IOUtils.toString(con.getInputStream(), "utf-8");
            // //System.out.println("HttpURLUtil:"+wxMsgXml);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (con != null) {
                con.disconnect();
            }
        }
        return wxMsgXml;
    }
    public static void main(String args[]){
        String url = "https://test6.phiskin.com/phi_appt/fdd/callback/event";
        JSONObject json = new JSONObject();
        json.put("key", "value");
        String data = "bizContent=" + json.toString();
        Map<String, String> header = new HashMap<>();
        header.put("test", "123");
        String repStr = request(url, data, null, header, null, "application/x-www-form-urlencoded");
    }
}