HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源。虽然在 JDK 的 java net包中已经提供了访问 HTTP 协议的基本功能,但是对于大部分应用程序来说,JDK 库本身提供的功能还不够丰富和灵活。HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。HttpClient 已经应用在很多的项目中,比如 Apache Jakarta 上很著名的另外两个开源项目 Cactus 和 HTML Unit都使用了 HttpClient。
以下列出的是 HttpClient 提供的主要的功能,要知道更多详细的功能可以参见 HttpClient 的官网:
官方网站:http://hc.apache.org/httpcomponents-client-4.5.x/index.html。
导入Maven依赖:
org.apache.httpcomponents
httpclient
4.5.2
使用HttpClient发送请求、接收响应很简单,一般需要如下几步即可。
1. 创建HttpClient对象。
2. 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。
3. 如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HetpParams params)方法来添加请求参数;对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。
4. 调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。
5. 调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头;调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。
6. 释放连接。无论执行方法是否成功,都必须释放连接
代码示例:
1、发送Get请求无参:
@org.junit.Test
public void test() {
get();
//getWithParams();
//postLogin();
}
/**
* 发送 get请求
*/
public void get() {
//创建httpClient实例
CloseableHttpClient httpClient=HttpClients.createDefault();
//创建httpGet
HttpGet httpGet=new HttpGet("https://www.cnblogs.com/");
System.out.println("executing request:" + httpGet.getURI());
//设置http的状态参数
RequestConfig config=RequestConfig.custom()
.setSocketTimeout(10000) // 设置读取超时时间
.setConnectTimeout(5000) // 设置连接超时时间
.setConnectionRequestTimeout(5000)// 设置请求超时时间(单位毫秒)
.build();
httpGet.setConfig(config);
//创建response响应
CloseableHttpResponse response=null;
try {
//执行get请求
response=httpClient.execute(httpGet);
//获取响应的实体
HttpEntity entity=response.getEntity();
System.out.println("--------------------------------------");
// 打印响应内容长度
System.out.println("Response content length: " + entity.getContentLength());
// 打印响应状态
System.out.println(response.getStatusLine());
System.out.println(response.getStatusLine().getStatusCode());
if(entity!=null){
//输出响应结果
System.out.println("Response Content:"+"\n"+EntityUtils.toString(entity,"utf-8"));
}
System.out.println("------------------------------------");
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
//关闭连接,释放资源
try {
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
2、发送Get有参:
/**
* get请求有参数
*/
public void getWithParams() {
// 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// 参数
URI uri = null;
try {
// 将参数放入键值对类NameValuePair中,再放入集合中
List params = new ArrayList<>();
params.add(new BasicNameValuePair("userName", "admin"));
params.add(new BasicNameValuePair("password", "123456"));
// 设置uri信息,并将参数集合放入uri;
// 注:这里也支持一个键值对一个键值对地往里面放setParameter(String key, String value)
// http://localhost:8080/user/getWithParams.do?userName=admin&password=123456
uri = new URIBuilder().setScheme("http")
.setHost("localhost")
.setPort(8080)
.setPath("/user/getWithParams.do")
.setParameters(params)
.build();
} catch (URISyntaxException e1) {
e1.printStackTrace();
}
// 创建Get请求
HttpGet httpGet = new HttpGet(uri);
System.out.println("executing request:" + httpGet.getURI());
// 响应模型
CloseableHttpResponse response = null;
try {
// 由客户端执行(发送)Get请求
response = httpClient.execute(httpGet);
// 配置信息
RequestConfig requestConfig = RequestConfig.custom()
// 设置连接超时时间(单位毫秒)
.setConnectTimeout(5000)
// 设置请求超时时间(单位毫秒)
.setConnectionRequestTimeout(5000)
// socket读写超时时间(单位毫秒)
.setSocketTimeout(5000)
// 设置是否允许重定向(默认为true)
.setRedirectsEnabled(true).build();
// 将上面的配置信息 运用到这个Get请求里
httpGet.setConfig(requestConfig);
// 从响应模型中获取响应实体
HttpEntity entity = response.getEntity();
System.out.println("响应状态为:" + response.getStatusLine());
if (entity != null) {
System.out.println("响应内容长度为:" + entity.getContentLength());
System.out.println("响应内容为:" + EntityUtils.toString(entity,"utf-8"));
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
我的Controller测试代码如下:
@ResponseBody
@RequestMapping("/getWithParams")
public String testGet(String userName,String password){
return "userName"+userName+"password:"+password;
}
3、发送Post请求(模拟用户登录请求):
/**
* post方式提交表单(模拟用户登录请求)
*/
public void postLogin() {
// 创建httpClient实例
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建httppost
HttpPost httpPost = new HttpPost("http://localhost:8080/user/postWithParams.do");
// 创建参数队列
List list = new ArrayList();
list.add(new BasicNameValuePair("userName", "admin"));
list.add(new BasicNameValuePair("password", "123456"));
UrlEncodedFormEntity uefEntity;
try {
uefEntity = new UrlEncodedFormEntity(list, "UTF-8");
httpPost.setEntity(uefEntity);
System.out.println("executing request " + httpPost.getURI());
CloseableHttpResponse response = httpClient.execute(httpPost);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println("--------------------------------------");
System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8"));
System.out.println("--------------------------------------");
}
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭连接,释放资源
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@ResponseBody
@RequestMapping("/postWithParams")
public String testPost(String userName,String password){
return "userName="+userName+"----password="+password;
}
返回的结果为:
更多更详细可查看: https://blog.csdn.net/qq_42969074/article/details/85561394