简单备注下Apache HttpClient常用封装使用包括get,post及发送文件相关,直接上代码。
public static void closeHttpResource(CloseableHttpClient closeableHttpClient,
CloseableHttpResponse closeableHttpResponse) {
try {
if (null != closeableHttpClient) {
closeableHttpClient.close();
}
} catch (Exception e) {
LOGGER.warn("closeableHttpClient error {}", e);
}
try {
if (null != closeableHttpResponse) {
closeableHttpResponse.close();
}
} catch (Exception e) {
LOGGER.warn("closeableHttpResponse error {}", e);
}
}
public static CloseableHttpResponse sendHttpGet(CloseableHttpClient httpClient, String url, Map<String, String> headers) throws Exception {
try {
HttpGet httpGet = new HttpGet(url);
httpGet.setConfig(config);
headers.forEach(httpGet::addHeader);
return httpClient.execute(httpGet);
} catch (Exception e) {
throw e;
}
}
public static CloseableHttpResponse sendHttpGet(CloseableHttpClient httpClient, String url, Map<String, String> params, Map<String, String> headers) throws Exception {
try {
List<NameValuePair> list = Lists.newArrayList();
params.forEach((k, v) -> list.add(new BasicNameValuePair(k, v)));
URIBuilder uriBuilder = new URIBuilder(url).addParameters(list);
HttpGet httpGet = new HttpGet(uriBuilder.build());
httpGet.setConfig(config);
headers.forEach(httpGet::addHeader);
return httpClient.execute(httpGet);
} catch (Exception e) {
throw e;
}
}
public static ApiResponse<String> getResponse(CloseableHttpResponse response, String charset) throws IOException {
ApiResponse<String> apiResponse = new ApiResponse<>();
if (response.getStatusLine().getStatusCode() == Constants.HTTP_OK) {
apiResponse.setData(EntityUtils.toString(response.getEntity(), charset));
return apiResponse;
}
return new ApiResponse<>(response.getStatusLine().getStatusCode(), "网络异常");
}
public static CloseableHttpResponse sendHttpPost(CloseableHttpClient httpClient, String url, Map<String, String> headers) throws Exception {
try {
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(config);
headers.forEach(httpPost::addHeader);
return httpClient.execute(httpPost);
} catch (Exception e) {
throw e;
}
}
public static CloseableHttpResponse sendHttpPost(CloseableHttpClient httpClient, String url, Map<String, String> params, Map<String, String> headers) throws Exception {
try {
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(config);
headers.forEach(httpPost::addHeader);
List<NameValuePair> list = Lists.newArrayList();
params.forEach((k, v) -> list.add(new BasicNameValuePair(k, v)));
UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(list);
httpPost.setEntity(encodedFormEntity);
return httpClient.execute(httpPost);
} catch (Exception e) {
throw e;
}
}
public static CloseableHttpResponse sendHttpPost(CloseableHttpClient httpClient, String url, Map<String, String> params, Map<String, String> headers, List<MultipartFile> files) throws Exception {
try {
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(config);
headers.forEach(httpPost::addHeader);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setCharset(Charset.forName(Constants.DEFAULT_CHARSET));
int idx = 1;
for (MultipartFile file : files) {
builder.addBinaryBody("file".concat(String.valueOf(idx)), file.getInputStream(), ContentType.MULTIPART_FORM_DATA, file.getName());
idx++;
}
params.forEach(builder::addTextBody);
httpPost.setEntity(builder.build());
return httpClient.execute(httpPost);
} catch (Exception e) {
throw e;
}
}