package com.iailab.module.data.common.utils;
|
|
import com.iailab.framework.tenant.core.context.TenantContextHolder;
|
import org.apache.http.HttpEntity;
|
import org.apache.http.HttpResponse;
|
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.entity.StringEntity;
|
import org.apache.http.util.EntityUtils;
|
import org.springframework.util.CollectionUtils;
|
|
import java.io.BufferedReader;
|
import java.io.IOException;
|
import java.io.InputStreamReader;
|
import java.io.PrintWriter;
|
import java.net.URL;
|
import java.net.URLConnection;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* @author PanZhibao
|
* @date 2021年06月03日 15:31
|
*/
|
public class HttpRequest {
|
|
/**
|
*
|
* @param url
|
* @param json
|
* @param charset
|
* @param token
|
* @return
|
*/
|
public static String doPost(String url, String json, String charset, String token) {
|
Long tenantId = TenantContextHolder.getTenantId();
|
org.apache.http.client.HttpClient httpClient = null;
|
HttpPost httpPost = null;
|
String result = null;
|
try {
|
httpClient = new SSLClient();
|
httpPost = new HttpPost(url);
|
//设置参数
|
httpPost.addHeader("Accept", "application/json");
|
httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
|
httpPost.addHeader("token", token);
|
httpPost.addHeader("Tenant-Id", String.valueOf(tenantId));
|
StringEntity stringEntity = new StringEntity(json);
|
stringEntity.setContentEncoding("UTF-8");
|
stringEntity.setContentType("application/json");
|
httpPost.setEntity(stringEntity);
|
HttpResponse response = httpClient.execute(httpPost);
|
if (response != null) {
|
HttpEntity resEntity = response.getEntity();
|
if (resEntity != null) {
|
result = EntityUtils.toString(resEntity, charset);
|
}
|
}
|
} catch (Exception ex) {
|
ex.printStackTrace();
|
}
|
return result;
|
}
|
}
|