使用Apache中的HttpClient的实例CloseableHttpClient的一个例子
来源:互联网 发布:电脑淘宝网页有问题 编辑:程序博客网 时间:2024/04/29 21:43
Apache的HttpClient可以被用于从客户端发送HTTP请求到服务器端,下面给出一个用HttpClient执行GET和POST请求的操作方法
使用maven构建依赖包:
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>${httpclient.version}</version> </dependency>
注意此httpClient包从4.4开始SSLContexts类就被移去了,我在使用4.3.1版本.其中的jar列表如下:
使用Apache的HttpClient发送GET和POST请求的步骤如下:
1. 使用帮助类HttpClients创建CloseableHttpClient对象.
2. 基于要发送的HTTP请求类型创建HttpGet或者HttpPost实例.
3. 使用addHeader方法添加请求头部,诸如User-Agent, Accept-Encoding等参数.
4. 对于POST请求,创建NameValuePair列表,并添加所有的表单参数.然后把它填充进HttpPost实体.
5. 通过执行此HttpGet或者HttpPost请求获取CloseableHttpResponse实例
6. 从此CloseableHttpResponse实例中获取状态码,错误信息,以及响应页面等等.
7. 最后关闭HttpClient资源.
下面给出示例代码:
package com.journaldev.utils;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;import org.apache.http.HttpEntity;import org.apache.http.NameValuePair;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;public class ApacheHttpClientExample { private static final String USER_AGENT = "Mozilla/5.0"; private static final String GET_URL = "http://localhost:9090/SpringMVCExample"; private static final String POST_URL = "http://localhost:9090/SpringMVCExample/home"; public static void main(String[] args) throws IOException { sendGET(); System.out.println("GET DONE"); sendPOST(); System.out.println("POST DONE"); } private static void sendGET() throws IOException { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(GET_URL); httpGet.addHeader("User-Agent", USER_AGENT); CloseableHttpResponse httpResponse = httpClient.execute(httpGet); System.out.println("GET Response Status:: " + httpResponse.getStatusLine().getStatusCode()); BufferedReader reader = new BufferedReader(new InputStreamReader( httpResponse.getEntity().getContent())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = reader.readLine()) != null) { response.append(inputLine); } reader.close(); // print result System.out.println(response.toString()); httpClient.close(); } private static void sendPOST() throws IOException { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(POST_URL); httpPost.addHeader("User-Agent", USER_AGENT); List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); urlParameters.add(new BasicNameValuePair("userName", "Pankaj Kumar")); HttpEntity postParams = new UrlEncodedFormEntity(urlParameters); httpPost.setEntity(postParams); CloseableHttpResponse httpResponse = httpClient.execute(httpPost); System.out.println("POST Response Status:: " + httpResponse.getStatusLine().getStatusCode()); BufferedReader reader = new BufferedReader(new InputStreamReader( httpResponse.getEntity().getContent())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = reader.readLine()) != null) { response.append(inputLine); } reader.close(); // print result System.out.println(response.toString()); httpClient.close(); }}
输出结果如下所示 :
GET Response Status:: 200<html><head> <title>Home</title></head><body><h1> Hello world! </h1><P> The time on the server is March 7, 2015 1:01:22 AM IST. </p></body></html>GET DONEPOST Response Status:: 200<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>User Home Page</title></head><body><h3>Hi Pankaj Kumar</h3></body></html>POST DONE
参考网址:看这里
0 1
- 使用Apache中的HttpClient的实例CloseableHttpClient的一个例子
- 使用Apache中的HttpClient的实例CloseableHttpClient进行http请求
- Apache的HttpClient创建CloseableHttpClient的例子
- apache httpclient 使用例子
- apache HttpClient的使用
- 使用Apache HttpClient实现多线程下载的小例子
- 使用Apache HttpClient实现多线程下载的小例子
- HttpClient的使用实例
- Apache Lucence 使用的一个简单例子
- org.apache.commons.httpclient.HttpClient的使用
- org.apache.commons.httpclient.HttpClient的使用
- CloseableHttpClient类的源码
- 一个使用HttpClient get post请求的实例
- 一个httpclient post 的例子
- Apache MINA 的一个例子
- Apache的httpclient的简单使用
- 忽略证书验证的CloseableHttpClient
- HttpClient读取页面的使用例子
- 使用SharedPreferences实现Activity和Service之间数据同步问题
- 计算两个日期相差的天数
- Fresco内存管理机制学习
- linux下软中断[si]负载均衡
- 机器学习流行算法一览
- 使用Apache中的HttpClient的实例CloseableHttpClient的一个例子
- Unity UGUI基础之Toggle
- House Robber
- memcached学习总结
- Python学习第〇篇-主要概念及示例
- mac 下mysql root密码问题
- TrinityCore中的PreparedStatement
- HDU 3533 BFS+优先队列
- Spring(AbstractRoutingDataSource)实现动态数据源切换