liusheng
2023-12-04 926b0e68e108d0866d79c1a366e3d14d1cebac4b
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package com.ruoyi.common.utils;
 
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
 
public class HttpClientKit {
 
    public static String post(String url, JSONObject json) {
 
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(url);
 
        post.setHeader("Content-Type", "application/json");
        post.addHeader("Authorization", "Basic YWRtaW46");
        String result = "";
 
        try {
 
            StringEntity s = new StringEntity(json.toString(), "utf-8");
            s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
            post.setEntity(s);
 
            // 发送请求
            HttpResponse httpResponse = client.execute(post);
 
            // 获取响应输入流
            InputStream inStream = httpResponse.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "utf-8"));
            StringBuilder strber = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) strber.append(line + "\n");
            inStream.close();
 
            result = strber.toString();
            System.out.println(result);
 
            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SUCCESS) {
                // System.out.println("请求服务器成功,做相应处理");
            } else {
                System.out.println("请求服务端失败");
            }
        } catch (Exception e) {
            System.out.println("请求异常");
            throw new RuntimeException(e);
        } finally {
            //client.
        }
        return result;
    }
 
    public static String sendPostWithFile(File lrcFile, String urlStr) throws Exception {
        String BOUNDARY = java.util.UUID.randomUUID().toString();
        MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, "--------------------" + BOUNDARY, Charset.forName("UTF-8"));
        multipartEntity.addPart("lyricsUpload", new FileBody(lrcFile));    //lyricsUpload文件名
        HttpPost request = new HttpPost(urlStr);    //远程接口
        request.setEntity(multipartEntity);
        request.addHeader("Content-Type", "multipart/form-data; boundary=--------------------" + BOUNDARY + ";charset=UTF-8");
 
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = httpClient.execute(request);
        InputStream is = response.getEntity().getContent();
        BufferedReader in = new BufferedReader(new InputStreamReader(is));
        StringBuffer buffer = new StringBuffer();
        String line = "";
        while ((line = in.readLine()) != null) {
            buffer.append(line);
        }
        return new String(buffer.toString().getBytes(), "UTF-8");
    }
 
    //    public static String sendPostWithFile(File lrcFile, String urlStr) throws Exception {
//        String BOUNDARY = java.util.UUID.randomUUID().toString();
//        MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, "--------------------" + BOUNDARY, Charset.defaultCharset());
//        multipartEntity.addPart("lyricsUpload", new FileBody(lrcFile));    //lyricsUpload文件名
//        HttpPost request = new HttpPost(urlStr);    //远程接口
//        request.setEntity(multipartEntity);
//        request.addHeader("Content-Type", "multipart/form-data; boundary=--------------------" + BOUNDARY);
//
//        DefaultHttpClient httpClient = new DefaultHttpClient();
//        HttpResponse response = httpClient.execute(request);
//        InputStream is = response.getEntity().getContent();
//        BufferedReader in = new BufferedReader(new InputStreamReader(is));
//        StringBuffer buffer = new StringBuffer();
//        String line = "";
//        while ((line = in.readLine()) != null) {
//            buffer.append(line);
//        }
//        //Flogger.info("发送消息收到的返回:" + buffer.toString());
//        String remark = "";
//        return  buffer.toString();
//       // org.json.JSONObject JSONObject = new org.json.JSONObject(buffer.toString());
//    }
    public static String postOpr(String url, String json) {
        //HttpClient httpClient = new HttpClient();
        org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
        PostMethod httpPost = new PostMethod(url);
        httpPost.setRequestHeader("Content-Type", "application/json;charset=utf-8");
 
        try {
            RequestEntity entity = new StringRequestEntity(json, "Content-Type", "UTF-8");
            httpPost.setRequestEntity(entity);
            httpClient.executeMethod(httpPost);
 
            return httpPost.getResponseBodyAsString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
 
    public static String postMsg(String url, JSONObject json) {
 
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(url);
 
        post.setHeader("Content-Type", "application/json");
        //post.addHeader("Authorization", "Basic YWRtaW46");
        //post.addHeader("ApiKey", "hswl");
        post.addHeader("PostMethod", "POST");
        String result = "";
 
        try {
 
            StringEntity s = new StringEntity(json.toJSONString(), "utf-8");
            s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
            post.setEntity(s);
 
            // 发送请求
            HttpResponse httpResponse = client.execute(post);
 
            // 获取响应输入流
            InputStream inStream = httpResponse.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "utf-8"));
            StringBuilder strber = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) strber.append(line + "\n");
            inStream.close();
 
            result = strber.toString();
            System.out.println(result);
 
            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SUCCESS) {
                //System.out.println("请求服务器成功,做相应处理");
            } else {
                System.out.println("请求服务端失败");
            }
        } catch (Exception e) {
            System.out.println("请求异常");
            throw new RuntimeException(e);
        } finally {
            //client.
        }
        return result;
    }
}