2021与蓝度共同重构项目,服务端
liuhaonan
2022-10-21 680ff9e2cca45bcb23e373aaa221a1e1dfab2472
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.sandu.ximon.admin.utils;
 
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
 
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
 
/**
 * 音柱用POST工具类
 */
public class BroadcastPostUtils {
 
 
    public static String Post(String json, String URL, String charset) {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(URL);
        post.setHeader("Content-Type", "application/json");
        String result = null;
        try {
            StringEntity s = new StringEntity(json, charset);
            s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                    "application/json"));
            post.setEntity(s);
            HttpResponse httpResponse = client.execute(post);
            InputStream inStream = httpResponse.getEntity().getContent();
 
            result = streamToString(inStream, charset);
 
            inStream.close();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return result;
    }
 
    /**
     * @param in
     * @param encoding
     * @return
     */
 
    public static String streamToString(InputStream in, String encoding) {
        // 将流转换为字符串
        try {
            StringBuffer sb = new StringBuffer();
            byte[] b = new byte[1024];
            for (int n; (n = in.read(b)) != -1; ) {
                sb.append(new String(b, 0, n, encoding));
            }
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("提取 requestBody 异常", e);
        }
    }
 
 
    public static String taskPostEncoding(String json, String URL) {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(URL);
        post.setHeader("Content-Type", "application/json");
        post.addHeader("Authorization", "Basic YWRtaW46");
        String result = null;
        List<String> list = new ArrayList<>();
        try {
            StringEntity s = new StringEntity(json, "utf-8");
            s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                    "application/json"));
            post.setEntity(s);
            HttpResponse httpResponse = client.execute(post);
            InputStream inStream = httpResponse.getEntity().getContent();
 
            result = streamToString(inStream, "utf-8");
 
 
            inStream.close();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return result;
    }
 
 
}