- 依赖坐标
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.1</version>
</dependency>
- 完整代码
package http;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
public class HttpUtil {
private static final String POST_NOT_OK = "Post Request For Url[%s] is not ok. Response is null";
private static final String POST_ERROR_CODE = "Post Request For Url[%s] is not ok. Response Status Code is %s";
private static final int CONNECT_TIMEOUT = 10000;
private static final int CONNECTION_REQUEST_TIMEOUT = 3000;
private static final int SOCKET_TIMEOUT = 20000;
public static String postRequest(String url, Map<String, String> headers, Map<String, String> params,
Map<String, List<File>> files) throws Exception {
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT)
.setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build());
if (headers != null) {
for (String key : headers.keySet()) {
httpPost.setHeader(key, headers.get(key));
}
}
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
if (files != null) {
for (String key : files.keySet()) {
for (File file : files.get(key)) {
builder.addPart(key, new FileBody(file));
}
}
}
if (params != null) {
for (String key : params.keySet()) {
builder.addTextBody(key, params.get(key));
}
}
httpPost.setEntity(builder.build());
HttpResponse response = HttpClientBuilder.create().build().execute(httpPost);
if (response == null || response.getStatusLine() == null) {
throw new RuntimeException(String.format(POST_NOT_OK, url));
} else if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
throw new RuntimeException(String.format(POST_ERROR_CODE, url, response.getStatusLine().getStatusCode()));
}
return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8.name());
}
}
mark,后续再整理。。。
public static String getPostMethod(String url, Map<String, Object> map) throws Exception {
HttpClient httpClient = HttpClientBuilder.create().build();
httpClient = new DefaultSSLUtils();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json;charset=utf-8");
httpPost.setHeader("Accept", "application/json");
httpPost.setEntity(new StringEntity(JSON.toJSONString(map), StandardCharsets.UTF_8));
HttpResponse response = httpClient.execute(httpPost);
if (ObjectUtils.isNull(response) || ObjectUtils.isNull(response.getStatusLine())) {
throw new Exception("Post Request For Url[{}] is not ok. Response is null");
} else if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
logger.info("异常状态码 :" + response.getStatusLine().getStatusCode());
throw new Exception("Post Request For Url[{}] is not ok. Response Status Code is {}");
}
return EntityUtils.toString(response.getEntity());
}