HttpClient 是Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。
1、需要的依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
</dependency>
2、测试类
package com.qianfeng;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.message.BufferedHeader;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class HttpClientTest {
@Test
public void testGetString() throws IOException {
//创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建HttpGet
HttpGet httpGet = new HttpGet("https://www.baidu.com");
//请求配置
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(5000)//建立连接的时间
.setSocketTimeout(5000)//
.setConnectionRequestTimeout(5000)
.build();
//设置配置
httpGet.setConfig(config);
//执行请求,返回响应
CloseableHttpResponse response = httpClient.execute(httpGet);
//判断响应状态码,如果时200继续操作
if(response.getStatusLine().getStatusCode()==200){
HttpEntity entity = response.getEntity();
//获取响应头
BufferedHeader header = (BufferedHeader) entity.getContentType();
//header.getValue()获取的是contentType的类型,比如application/json
if("application/json".equals(header.getValue())||"text/html".equals(header.getValue())){
String s = EntityUtils.toString(entity, "UTF-8");
System.out.println(s);
}
//如果是图片类型就存起来
else if("image/jpeg".equals(header.getValue())){
//EntityUtils这个工具类可以很方便的帮我们转换entity为字符串或者字节数组
byte[] bytes = EntityUtils.toByteArray(entity);
FileOutputStream fos = new FileOutputStream("test.jpg");
fos.write(bytes);
fos.close();
}
}
//最后关闭response与httpClient
response.close();
httpClient.close();
}
@Test
public void testGetByte() throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3984473917,238095211&fm=26&gp=0.jpg");
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(5000)
.setSocketTimeout(5000)
.setConnectionRequestTimeout(5000)
.build();
httpGet.setConfig(config);
CloseableHttpResponse response = httpClient.execute(httpGet);
if(response.getStatusLine().getStatusCode()==200){
HttpEntity entity = response.getEntity();
byte[] bytes = EntityUtils.toByteArray(entity);
FileOutputStream fos = new FileOutputStream("test.jpg");
fos.write(bytes);
fos.close();
}
response.close();
httpClient.close();
}
@Test
public void testPostString() throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://www.httpbin.org/post");
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(5000)
.setSocketTimeout(5000)
.setConnectionRequestTimeout(5000)
.build();
httpPost.setConfig(config);
httpPost.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36");
List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
parameters.add(new BasicNameValuePair("name","zhangsan"));
parameters.add(new BasicNameValuePair("password","zhangsan123"));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters,"UTF-8");
httpPost.setEntity(formEntity);
CloseableHttpResponse response = httpClient.execute(httpPost);
if(response.getStatusLine().getStatusCode()==200){
HttpEntity entity = response.getEntity();
String s = EntityUtils.toString(entity, "UTF-8");
//byte[] bytes = EntityUtils.toByteArray(entity);
System.out.println(s);
}
response.close();
httpClient.close();
}
}
这三个方法分别模拟了get方式请求普通网页,get方式请求图片、post方式添加参数请求的三种方式
网友评论