本文偏重使用,简单讲述httpclient,httpclient其实就是模拟浏览器发起想服务器端的请求,而这种更加类似于JS的请求或页面的POST、GET,来完成系统间的接口调用,方便做其他的交互,本文是对httpclient进行二次封装完成的常用方法工具包:
package com.jusfoun.util.http;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
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.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;
/**
* @ClassName: HttpUtilImpl
* @Description: HttpClinet请求工具包
* @author liuheming
* @date 2017年2月1日 下午12:40:16
*
*/
@Service
public class HttpUtilImpl implements HttpUtil {
@Override
public String get(String uri, String chatset) {
HttpClientBuilder create = HttpClientBuilder.create();
HttpClient client = create.build();
RequestConfig requestConfig = getRequestConfig();
HttpGet httpGet = new HttpGet(uri);
httpGet.setConfig(requestConfig);
try {
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
byte[] contentBytes = EntityUtils.toByteArray(entity);
return new String(contentBytes, chatset);
} catch (Exception e) {
e.printStackTrace();
} finally {
httpGet.abort();
httpGet.releaseConnection();
}
return null;
}
@Override
public String post(String uri, String chatset, String jsonParameters) {
HttpClientBuilder create = HttpClientBuilder.create();
HttpClient client = create.build();
RequestConfig requestConfig = getRequestConfig();
HttpPost post = new HttpPost(uri);
post.setConfig(requestConfig);
try {
StringEntity stringEntity = new StringEntity(jsonParameters, chatset);
post.setEntity(stringEntity);
HttpResponse httpResponse = client.execute(post);
HttpEntity responseEntity = httpResponse.getEntity();
byte[] contentBytes = EntityUtils.toByteArray(responseEntity);
return new String(contentBytes, chatset);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
post.abort();
post.releaseConnection();
}
return null;
}
private RequestConfig getRequestConfig() {
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(10000).setConnectTimeout(10000).setSocketTimeout(10000).build();
return requestConfig;
}
@Override
public String post(String uri, String chatset, File file,String mediaType) {
HttpClientBuilder create = HttpClientBuilder.create();
HttpClient client = create.build();
RequestConfig requestConfig = getRequestConfig();
HttpPost post = new HttpPost(uri);
post.setConfig(requestConfig);
try {
FileBody bin = new FileBody(file,ContentType.create(mediaType));
MultipartEntityBuilder mpEntityBuilder = MultipartEntityBuilder.create(); // 文件传输
mpEntityBuilder.addPart("media", bin);
mpEntityBuilder.addTextBody("description", "{\"title\":\"title\",\"introduction\":\"introduction\"}");
HttpEntity httpEntity = mpEntityBuilder.build();
System.out.println(httpEntity.getClass());
System.out.println("contentType : " + httpEntity.getContentType());
System.out.println("contentLength : " + httpEntity.getContentLength());
System.out.println("encoding : " + httpEntity.getContentEncoding());
post.setEntity(httpEntity);
HttpResponse httpResponse = client.execute(post);
HttpEntity responseEntity = httpResponse.getEntity();
byte[] contentBytes = EntityUtils.toByteArray(responseEntity);
return new String(contentBytes, chatset);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
post.abort();
post.releaseConnection();
}
return null;
}
@Override
public String post(String url, String charset, File file, String mediaType, Map<String, String> parameters) {
HttpClientBuilder create = HttpClientBuilder.create();
HttpClient client = create.build();
RequestConfig requestConfig = getRequestConfig();
HttpPost post = new HttpPost(url);
post.setConfig(requestConfig);
try {
FileBody bin = new FileBody(file,ContentType.create(mediaType));
MultipartEntityBuilder mpEntityBuilder = MultipartEntityBuilder.create(); // 文件传输
mpEntityBuilder.addPart("file", bin);
for(String key : parameters.keySet()){
mpEntityBuilder.addTextBody(key, parameters.get(key));
}
HttpEntity httpEntity = mpEntityBuilder.build();
post.setEntity(httpEntity);
HttpResponse httpResponse = client.execute(post);
HttpEntity responseEntity = httpResponse.getEntity();
byte[] contentBytes = EntityUtils.toByteArray(responseEntity);
return new String(contentBytes, charset);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
post.abort();
post.releaseConnection();
}
return null;
}
}