yangys
2024-05-07 9b677ea5c6978788d135fc15da3d78c5a93789c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.qianwen.smartman.modules.dnc.util;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Map;
import java.util.Objects;
import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import com.qianwen.smartman.common.constant.DncConstant;
 
public class HttpClientUtils {
    public static JSONObject postForForm(String url, Map<String, String> parms) {
        HttpPost httpPost = new HttpPost(url);
        ArrayList<BasicNameValuePair> list = new ArrayList<>();
        parms.forEach((key, value) -> {
            list.add(new BasicNameValuePair(key, value));
        });
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            try {
                if (parms.size() > 0) {
                    httpPost.setEntity(new UrlEncodedFormEntity(list, DncConstant.CHARSET));
                }
                InputStream content = httpPost.getEntity().getContent();
                InputStreamReader inputStreamReader = new InputStreamReader(content, StandardCharsets.UTF_8);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String readLine = bufferedReader.readLine();
                String s = URLDecoder.decode(readLine, DncConstant.CHARSET);
                System.out.println("readLine===================================" + readLine);
                System.out.println("s==========================================" + s);
                HttpEntity entity = httpClient.execute(httpPost).getEntity();
                JSONObject jsonObject = JSON.parseObject(EntityUtils.toString(entity, DncConstant.CHARSET));
                if (Objects.nonNull(httpClient)) {
                    try {
                        httpClient.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return jsonObject;
            } catch (IOException e2) {
                e2.printStackTrace();
                if (Objects.nonNull(httpClient)) {
                    try {
                        httpClient.close();
                        return null;
                    } catch (IOException e3) {
                        e3.printStackTrace();
                        return null;
                    }
                }
                return null;
            }
        } catch (Throwable th) {
            if (Objects.nonNull(httpClient)) {
                try {
                    httpClient.close();
                } catch (IOException e4) {
                    e4.printStackTrace();
                }
            }
            throw th;
        }
    }
}