派生自 projectDept/qhighschool

EricsHu
2023-04-10 5196a1d1e00ced009a2d4e8ea0aa5f3bb7cefe33
src/main/java/com/qxueyou/scc/wx/utils/HttpClientUtil.java
@@ -1,5 +1,7 @@
package com.qxueyou.scc.wx.utils;
import com.alibaba.fastjson.JSONObject;
import net.sf.json.JSONString;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
@@ -78,6 +80,8 @@
            if (param != null) {
                List<NameValuePair> paramList = new ArrayList<>();
                for (String key : param.keySet()) {
                    System.out.println(key);
                    System.out.println(param.get(key));
                    paramList.add(new BasicNameValuePair(key, param.get(key)));
                }
                // 模拟表单
@@ -100,6 +104,36 @@
        return resultString;
    }
    public static String doPostUrl(String url, JSONObject jsonCode) {
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            String s = jsonCode.toJSONString(jsonCode);
            StringEntity entity=new StringEntity(s);
            httpPost.setEntity(entity);
            // 执行http请求
            response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resultString;
    }
    public static String doPost(String url) {
        return doPost(url, null);
    }