<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.10</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5</version>
</dependency>
package com.demo.common.utils;
import com.alibaba.fastjson.JSON;
import java.io.*;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
@Slf4j
public class HttpClient {
public static void main(String[] args) throws Exception {
}
public static Object requestPostUrl(String url, String parameter) throws Exception {
InputStream is = null;
String body = null;
StringBuilder res = new StringBuilder();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json");
StringEntity stringEntity = new StringEntity(parameter, "utf-8");
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
RequestConfig config = RequestConfig.custom().setConnectTimeout(5000).build();
httpPost.setConfig(config);
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
if (entity != null) {
is = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is, Consts.UTF_8));
while ((body = br.readLine()) != null) {
res.append(body);
}
}
Object jsonMap = JSON.parse(res.toString());
return jsonMap;
}
public static Object postByFormData(String url, HashMap<String, Object> textMap) throws IOException {
InputStream is = null;
String body = null;
StringBuilder res = new StringBuilder();
HttpPost httpPost = new HttpPost(url);
ContentType contentType = ContentType.create("multipart/form-data", Charset.forName("UTF-8"));
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
CloseableHttpResponse httpResponse = null;
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(200000).setSocketTimeout(200000000).build();
httpPost.setConfig(requestConfig);
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntityBuilder.setCharset(Charset.forName("UTF-8"));
for (Map.Entry<String, Object> entry : textMap.entrySet()) {
Object o = entry.getValue();
if(o instanceof String || o instanceof Integer) {
multipartEntityBuilder.addTextBody(entry.getKey(), entry.getValue().toString(), contentType);
}else {
File file = (File) o;
multipartEntityBuilder.addBinaryBody(entry.getKey(),file);
}
}
HttpEntity httpEntity = multipartEntityBuilder.build();
httpPost.setEntity(httpEntity);
try {
httpResponse = httpClient.execute(httpPost);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
is = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is, Consts.UTF_8));
while ((body = br.readLine()) != null) {
res.append(body);
}
}
Object jsonMap = JSON.parse(res.toString());
log.info("调用地址:" + url + ",响应:" + jsonMap);
return jsonMap;
}
}