HttpClient是Apache Jakarta Common下的子项目,用来提供高效的、最新的、功能丰富的支持HTTP协议的客户端编程工具包,并且它支持HTTP协议最新的版本和建议。HttpClient已经应用在很多的项目中,比如Apache Jakarta上很著名的另外两个开源项目Cactus和HTMLUnit都使用了HttpClient。
下载地址: http://hc.apache.org/downloads.cgi
基于标准、纯净的java语言。实现了Http1.0和Http1.1
以可扩展的面向对象的结构实现了Http全部的方法(GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE)。
支持HTTPS协议。
通过Http代理建立透明的连接。
利用CONNECT方法通过Http代理建立隧道的https连接。
Basic, Digest, NTLMv1, NTLMv2, NTLM2 Session, SNPNEGO/Kerberos认证方案。
插件式的自定义认证方案。
便携可靠的套接字工厂使它更容易的使用第三方解决方案。
连接管理器支持多线程应用。支持设置最大连接数,同时支持设置每个主机的最大连接数,发现并关闭过期的连接。
自动处理Set-Cookie中的Cookie。
插件式的自定义Cookie策略。
Request的输出流可以避免流中内容直接缓冲到socket服务器。
Response的输入流可以有效的从socket服务器直接读取相应内容。
在http1.0和http1.1中利用KeepAlive保持持久连接。
直接获取服务器发送的response code和 headers。
设置连接超时的能力。
实验性的支持http1.1 response caching。
源代码基于Apache License 可免费获取。
使用HttpClient发送请求、接收响应很简单,一般需要如下几步即可。
创建HttpClient对象。最新版的httpClient使用实现类的是closeableHTTPClient,以前的default作废了.
创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。
如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HetpParams params)方法来添加请求参数;对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。
调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。
调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头;调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。
释放连接。无论执行方法是否成功,都必须释放连接
环境说明:Eclipse、JDK1.8、SpringBoot
org.apache.httpcomponents
httpclient
4.5.12
com.alibaba
fastjson
1.2.71
注:本人引入此依赖的目的是,在后续示例中,会用到“将对象转化为json字符串的功能”,也可以引其他有此功能的依赖。
详细使用示例
声明:此示例中,以JAVA发送HttpClient(在test里面单元测试发送的);也是以JAVA接收的(在controller里面接收的)。
声明:下面的代码,本人亲测有效。
public static String doHttpGet(String url, Map<String,String> params,Map<String,String> headParams) {
String result = null;
//1.获取httpclient
CloseableHttpClient httpClient = HttpClients.createDefault();
//接口返回结果
CloseableHttpResponse response = null;
String paramStr = null;
try {
List<BasicNameValuePair> paramsList = new ArrayList<BasicNameValuePair>();
for (String key : params.keySet()) {
paramsList.add(new BasicNameValuePair(key,params.get(key)));
}
paramStr = EntityUtils.toString(new UrlEncodedFormEntity(paramsList));
//拼接参数
StringBuffer sb = new StringBuffer();
sb.append(url);
sb.append("?");
sb.append(paramStr);
//2.创建get请求
HttpGet httpGet = new HttpGet(sb.toString());
//3.设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();
httpGet.setConfig(requestConfig);
/*此处可以添加一些请求头信息,例如:
httpGet.addHeader("content-type","text/xml");*/
for (String head : headParams.keySet()) {
httpGet.addHeader(head,headParams.get(head));
}
//4.提交参数
response = httpClient.execute(httpGet);
//5.得到响应信息
int statusCode = response.getStatusLine().getStatusCode();
//6.判断响应信息是否正确
if (HttpStatus.SC_OK != statusCode) {
//终止并抛出异常
httpGet.abort();
throw new RuntimeException("HttpClient,error status code :" + statusCode);
}
//7.转换成实体类
HttpEntity entity = response.getEntity();
if (null != entity) {
result = EntityUtils.toString(entity);
}
EntityUtils.consume(entity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//8.关闭所有资源连接
if (null != response) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != httpClient) {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
/**
* http post 请求
*/
public static String doPost(String url, Map<String,String> params,Map<String,String> headParams) {
String result = null;
//1. 获取httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
try {
//2. 创建post请求
HttpPost httpPost = new HttpPost(url);
//3.设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(100000).setConnectTimeout(100000).build();
httpPost.setConfig(requestConfig);
//4.提交参数发送请求
List<BasicNameValuePair> paramsList = new ArrayList<>();
for (String key : params.keySet()) {
paramsList.add(new BasicNameValuePair(key,params.get(key)));
}
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(paramsList, HTTP.UTF_8);
httpPost.setEntity(urlEncodedFormEntity);
//设置请求头
for (String head : headParams.keySet()) {
httpPost.addHeader(head,headParams.get(head));
}
response = httpClient.execute(httpPost);
//5.得到响应信息
int statusCode = response.getStatusLine().getStatusCode();
//6. 判断响应信息是否正确
if (HttpStatus.SC_OK != statusCode) {
//结束请求并抛出异常
httpPost.abort();
throw new RuntimeException("HttpClient,error status code :" + statusCode);
}
//7. 转换成实体类
HttpEntity entity = response.getEntity();
if (null != entity) {
result = EntityUtils.toString(entity, "UTF-8");
}
EntityUtils.consume(entity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//8. 关闭所有资源连接
if (null != response) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != httpClient) {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
public static String doPostJson(String url, String params,Map<String,String> headParams) {
String result = null;
//1. 获取httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
try {
//2. 创建post请求
HttpPost httpPost = new HttpPost(url);
//3.设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(100000).setConnectTimeout(100000).build();
httpPost.setConfig(requestConfig);
//4.提交参数发送请求
httpPost.setEntity(new StringEntity(params, ContentType.create("application/json", "utf-8")));
//设置请求头
for (String head : headParams.keySet()) {
httpPost.addHeader(head,headParams.get(head));
}
response = httpClient.execute(httpPost);
//5.得到响应信息
int statusCode = response.getStatusLine().getStatusCode();
//6. 判断响应信息是否正确
if (HttpStatus.SC_OK != statusCode) {
//结束请求并抛出异常
httpPost.abort();
throw new RuntimeException("HttpClient,error status code :" + statusCode);
}
//7. 转换成实体类
HttpEntity entity = response.getEntity();
if (null != entity) {
result = EntityUtils.toString(entity, "UTF-8");
}
EntityUtils.consume(entity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//8. 关闭所有资源连接
if (null != response) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != httpClient) {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
<dependency>
<groupId>org.apache.httpcomponentsgroupId>
<artifactId>httpmimeartifactId>
<version>4.3version>
dependency>
/public static String httpPost(String url, File file) {
try {
// 创建 HttpClient 实例
CloseableHttpClient httpClient = HttpClients.custom()
.addInterceptorFirst(new HttpRequestInterceptor() {
//在这里加入拦截器,对文件请求的contentType做出处理
@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
if (request instanceof HttpEntityEnclosingRequest) {
HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request;
HttpEntity entity = entityRequest.getEntity();
if (entity != null && entity.getContentType() != null && entity.getContentType().getValue().startsWith("multipart/form-data")) {
String contentType = entity.getContentType().getValue();
String accept = MediaType.ALL.toString();
entityRequest.setHeader("x-tilake-app-key", "");
entityRequest.setHeader("x-tilake-ca-timestamp", "");
entityRequest.setHeader("x-tilake-ca-signature", "");
entityRequest.setHeader("Conetent-Type",contentType);
entityRequest.setHeader("Accept",accept);
}
}
}
})
.build();
// 创建 HTTP POST 请求
HttpPost httpPost = new HttpPost(url);
// 设置请求体内容
// 创建 MultipartEntityBuilder
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
// 添加其他参数
builder.addTextBody("id", "111");
// 添加文件流参数
builder.addBinaryBody("file", file, ContentType.DEFAULT_BINARY,file.getName());
/*绑定文件参数,传入文件流和contenttype,此处也可以继续添加其他formdata参数*/
// builder.addBinaryBody("file",is, ContentType.MULTIPART_FORM_DATA,fileName);
// 设置请求实体内容
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
// 执行请求并获取响应
HttpResponse response = httpClient.execute(httpPost);
// 解析响应
HttpEntity responseEntity = response.getEntity();
String responseBody = EntityUtils.toString(responseEntity);
// 处理响应结果
System.out.println(responseBody);
// 关闭 HttpClient
httpClient.close();
return responseBody;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
String fileUrl ="https://img1.doubanio.com/view/photo/l/public/p2537149328.webp";
String url = "";
String s = httpPost(url, new File(fileUrl));
System.out.println(s);
}
HttpClient client = new HttpClient();
// 设置代理服务器地址和端口
// client.getHostConfiguration().setProxy("proxy_host_addr",proxy_port);
// 使用 GET 方法 ,如果服务器需要通过 HTTPS 连接,那只需要将下面 URL 中的 http 换成 https
HttpMethodmethod = new GetMethod("http://java.sun.com");
// 使用POST方法
// HttpMethod method = new PostMethod("http://java.sun.com");
client.executeMethod(method);
// 打印服务器返回的状态
System.out.println(method.getStatusLine());
// 打印返回的信息
System.out.println(method.getResponseBodyAsString());
// 释放连接
method.releaseConnection();
public void getUrl(String url, String encoding) throws ClientProtocolException, IOException {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(instream, encoding));
System.out.println(reader.readLine());
} catch (Exception e) {
e.printStackTrace();
} finally {
instream.close();
}
}
// 关闭连接.
client.getConnectionManager().shutdown();
}
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public static String getResult(String urlStr) {
CloseableHttpClient httpClient = HttpClients.createDefault();
// HTTP Get请求
HttpGet httpGet = new HttpGet(urlStr);
// 设置请求和传输超时时间
// RequestConfig requestConfig =
// RequestConfig.custom().setSocketTimeout(TIME_OUT).setConnectTimeout(TIME_OUT).build();
// httpGet.setConfig(requestConfig);
String res = "";
try {
// 执行请求
HttpResponse getAddrResp = httpClient.execute(httpGet);
HttpEntity entity = getAddrResp.getEntity();
if (entity != null) {
res = EntityUtils.toString(entity);
}
log.info("响应" + getAddrResp.getStatusLine());
} catch (Exception e) {
log.error(e.getMessage(), e);
return res;
} finally {
try {
httpClient.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
return res;
}
}
return res;
}