提交 | 用户 | 时间
|
a6de49
|
1 |
package com.iailab.module.data.common.utils; |
H |
2 |
|
149dd0
|
3 |
import com.iailab.framework.tenant.core.context.TenantContextHolder; |
ce910c
|
4 |
import org.apache.http.HttpEntity; |
H |
5 |
import org.apache.http.HttpResponse; |
|
6 |
import org.apache.http.client.methods.HttpPost; |
|
7 |
import org.apache.http.entity.StringEntity; |
|
8 |
import org.apache.http.util.EntityUtils; |
a6de49
|
9 |
import org.springframework.util.CollectionUtils; |
H |
10 |
|
|
11 |
import java.io.BufferedReader; |
|
12 |
import java.io.IOException; |
|
13 |
import java.io.InputStreamReader; |
|
14 |
import java.io.PrintWriter; |
|
15 |
import java.net.URL; |
|
16 |
import java.net.URLConnection; |
|
17 |
import java.util.List; |
|
18 |
import java.util.Map; |
|
19 |
|
|
20 |
/** |
|
21 |
* @author PanZhibao |
|
22 |
* @date 2021年06月03日 15:31 |
|
23 |
*/ |
|
24 |
public class HttpRequest { |
ce910c
|
25 |
|
H |
26 |
/** |
|
27 |
* |
|
28 |
* @param url |
|
29 |
* @param json |
|
30 |
* @param charset |
|
31 |
* @param token |
|
32 |
* @return |
|
33 |
*/ |
|
34 |
public static String doPost(String url, String json, String charset, String token) { |
08b6a5
|
35 |
Long tenantId = TenantContextHolder.getTenantId(); |
ce910c
|
36 |
org.apache.http.client.HttpClient httpClient = null; |
H |
37 |
HttpPost httpPost = null; |
|
38 |
String result = null; |
|
39 |
try { |
|
40 |
httpClient = new SSLClient(); |
|
41 |
httpPost = new HttpPost(url); |
|
42 |
//设置参数 |
|
43 |
httpPost.addHeader("Accept", "application/json"); |
|
44 |
httpPost.addHeader("Content-Type", "application/json;charset=UTF-8"); |
|
45 |
httpPost.addHeader("token", token); |
08b6a5
|
46 |
httpPost.addHeader("Tenant-Id", String.valueOf(tenantId)); |
ce910c
|
47 |
StringEntity stringEntity = new StringEntity(json); |
H |
48 |
stringEntity.setContentEncoding("UTF-8"); |
|
49 |
stringEntity.setContentType("application/json"); |
|
50 |
httpPost.setEntity(stringEntity); |
|
51 |
HttpResponse response = httpClient.execute(httpPost); |
|
52 |
if (response != null) { |
|
53 |
HttpEntity resEntity = response.getEntity(); |
|
54 |
if (resEntity != null) { |
|
55 |
result = EntityUtils.toString(resEntity, charset); |
|
56 |
} |
|
57 |
} |
|
58 |
} catch (Exception ex) { |
|
59 |
ex.printStackTrace(); |
|
60 |
} |
|
61 |
return result; |
|
62 |
} |
a6de49
|
63 |
} |