httpclient介绍与使用
什么是httpclient
HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。
下载地址:http://hc.apache.org/
使用范例
- public class HttpClientTest {
- @Test
- public void doGet() throws Exception {
- //创建一个httpclient对象
- CloseableHttpClient httpClient = HttpClients.createDefault();
- //创建一个GET对象
- HttpGet get = new HttpGet("http://www.sogou.com");
- //执行请求
- CloseableHttpResponse response = httpClient.execute(get);
- //取响应的结果
- int statusCode = response.getStatusLine().getStatusCode();
- System.out.println(statusCode);//返回200
- HttpEntity entity = response.getEntity();
- String string = EntityUtils.toString(entity, "utf-8");
- System.out.println(string);//返回sogou主页的页面代码
- //关闭httpclient
- response.close();
- httpClient.close();
- }
- @Test
- public void doGetWithParam() throws Exception{
- CloseableHttpClient httpClient = HttpClients.createDefault();
- //创建一个uri对象
- URIBuilder uriBuilder = new URIBuilder("http://www.sogou.com/web");
- uriBuilder.addParameter("query", "花千骨");
- HttpGet get = new HttpGet(uriBuilder.build());
- CloseableHttpResponse response = httpClient.execute(get);
- int statusCode = response.getStatusLine().getStatusCode();
- System.out.println(statusCode);
- HttpEntity entity = response.getEntity();
- String string = EntityUtils.toString(entity, "utf-8");
- System.out.println(string);
- response.close();
- httpClient.close();
- }
- @Test
- public void doPost() throws Exception {
- CloseableHttpClient httpClient = HttpClients.createDefault();
- //创建一个post对象
- HttpPost post = new HttpPost("http://localhost:8082/httpclient/post.action");
- //执行post请求
- CloseableHttpResponse response = httpClient.execute(post);
- String string = EntityUtils.toString(response.getEntity());
- System.out.println(string);
- response.close();
- httpClient.close();
- }
- @Test
- public void doPostWithParam() throws Exception{
- CloseableHttpClient httpClient = HttpClients.createDefault();
- //创建一个post对象
- HttpPost post = new HttpPost("http://localhost:8082/httpclient/post.action");
- //创建一个Entity。模拟一个表单
- List<NameValuePair> kvList = new ArrayList<>();
- kvList.add(new BasicNameValuePair("username", "张三"));
- kvList.add(new BasicNameValuePair("password", "123"));
- //包装成一个Entity对象
- StringEntity entity = new UrlEncodedFormEntity(kvList, "utf-8");
- //设置请求的内容
- post.setEntity(entity);
- //执行post请求
- CloseableHttpResponse response = httpClient.execute(post);
- String string = EntityUtils.toString(response.getEntity());
- System.out.println(string);
- response.close();
- httpClient.close();
- }
- }
封装工具类
- public class HttpClientUtil {
- public static String doGet(String url, Map<String, String> param) {
- // 创建Httpclient对象
- CloseableHttpClient httpclient = HttpClients.createDefault();
- String resultString = "";
- CloseableHttpResponse response = null;
- try {
- // 创建uri
- URIBuilder builder = new URIBuilder(url);
- if (param != null) {
- for (String key : param.keySet()) {
- builder.addParameter(key, param.get(key));
- }
- }
- URI uri = builder.build();
- // 创建http GET请求
- HttpGet httpGet = new HttpGet(uri);
- // 执行请求
- response = httpclient.execute(httpGet);
- // 判断返回状态是否为200
- if (response.getStatusLine().getStatusCode() == 200) {
- resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (response != null) {
- response.close();
- }
- httpclient.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return resultString;
- }
- public static String doGet(String url) {
- return doGet(url, null);
- }
- public static String doPost(String url, Map<String, String> param) {
- // 创建Httpclient对象
- CloseableHttpClient httpClient = HttpClients.createDefault();
- CloseableHttpResponse response = null;
- String resultString = "";
- try {
- // 创建Http Post请求
- HttpPost httpPost = new HttpPost(url);
- // 创建参数列表
- if (param != null) {
- List<NameValuePair> paramList = new ArrayList<>();
- for (String key : param.keySet()) {
- paramList.add(new BasicNameValuePair(key, param.get(key)));
- }
- // 模拟表单
- UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
- httpPost.setEntity(entity);
- }
- // 执行http请求
- response = httpClient.execute(httpPost);
- resultString = EntityUtils.toString(response.getEntity(), "utf-8");
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- response.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return resultString;
- }
- public static String doPost(String url) {
- return doPost(url, null);
- }
- public static String doPostJson(String url, String json) {
- // 创建Httpclient对象
- CloseableHttpClient httpClient = HttpClients.createDefault();
- CloseableHttpResponse response = null;
- String resultString = "";
- try {
- // 创建Http Post请求
- HttpPost httpPost = new HttpPost(url);
- // 创建请求内容
- StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
- httpPost.setEntity(entity);
- // 执行http请求
- response = httpClient.execute(httpPost);
- resultString = EntityUtils.toString(response.getEntity(), "utf-8");
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- response.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return resultString;
- }
- }
HttpClientUtil
httpclient介绍与使用的更多相关文章
- HttpClient介绍和使用
HttpClient介绍和使用 今天有一个需求:后台访问一个接口,获取返回的数据.于是找到了HttpClient 1.介绍 SpringCloud中服务和服务之间的调用全部是使用HttpClient, ...
- 接口测试——httpclient介绍与请求方式详解
httpClient工具介绍 HTTP协议可能是现在lntemet上使用得最多.最重要的协议了,越来越多的Java应用程序需要直接通过HTTP协议来访问网络资源.虽然在JDK的java.net包中已经 ...
- httpclient介绍与请求方式详解
httpClient工具介绍 HTTP协议可能是现在lntemet上使用得最多.最重要的协议了,越来越多的Java应用程序需要直接通过HTTP协议来访问网络资源.虽然在JDK的java.net包中已经 ...
- httpclient介绍
前言 超文本传输协议(HTTP)也许是当今互联网上使用的最重要的协议了.Web服务,有网络功能的设备和网络计算的发展,都持续扩展了HTTP协议的角色,超越了用户使用的Web浏览器范畴,同时,也增加了需 ...
- HttpClient介绍和简单使用流程
HttpClient SpringCloud中服务和服务之间的调用全部是使用HttpClient,还有前面使用SolrJ中就封装了HttpClient,在调用SolrTemplate的saveBean ...
- 【JAVA】通过HttpClient发送HTTP请求的方法
HttpClient介绍 HttpClient 不是一个浏览器.它是一个客户端的 HTTP 通信实现库.HttpClient的目标是发 送和接收HTTP 报文.HttpClient不会去缓存内容,执行 ...
- 理解并使用.NET 4.5中的HttpClient
HttpClient介绍 HttpClient是.NET4.5引入的一个HTTP客户端库,其命名空间为System.Net.Http..NET 4.5之前我们可能使用WebClient和HttpWeb ...
- Android入门:用HttpClient模拟HTTP的GET和POST请求
一.HttpClient介绍 HttpClient是用来模拟HTTP请求的,其实实质就是把HTTP请求模拟后发给Web服务器: Android已经集成了HttpClient,因此可以直接使用: ...
- HttpClient中异步方法的同步调用
在System.Net.Http中,提供了使用Http与远程服务器通讯的httpClient,但是里面都是异步方法,有时候我们并不需要使用异步操作.这个时候可以使用如下的方式来进行同步调用. clas ...
随机推荐
- 微信小程序点击图片放大预览
微信小程序点击图片放大预览使用到 wx.previewImage 接口,可以放大.上/下一张 上代码 wxml代码 <view class='content-img' wx:if="{ ...
- PHP 绘制验证码
使用PHP绘制验证码 可直接使用 // 验证码 <?php $checkCode = ""; for ($i=0; $i < 4; $i++) { // 十进制转换为 ...
- ubuntu16.4+nginx+uwsgi+Django 部署上线
Nginx概述 Nginx是一款轻量级的HTTP服务器,采用事件驱动和异步非阻塞处理方式框架,这让其具有极好的IO性能,市场用于服务端的反向代理和负载均衡 Nginx优点 高并发连接:官方测试Ngin ...
- Android工程中javax annotation Nullable找不到的替代方案
我们在某些Android开源库中会遇到下面的引用找不到的问题:import javax.annotation.Nonnull;import javax.annotation.Nullable; 其实A ...
- JS截取字符串方法实例
// JS截取字符串可使用 substring()或者slice() // // 函数:substring() // 定义:substring(start,end)表示从start到end之间的字符串 ...
- 韩顺平php从入门到精通
37 整型细节说明 $a; echo $a; var_dump($a) //NULL 一个数总是要占用内存空间(字节),在php中一个整数一般占用四个字节(与平台相关),一个字节占用8bit php的 ...
- CSS3新特性,兼容性,兼容方法总结
css3手册css3手册 边框 border-radius 用于添加圆角效果 语法: border-radius:[ <length> | <percentage> ]{1,4 ...
- VS插件VisualSVN v5.2.3.0 破解文件
分享一个VisualSVN v5.2.3的破解文件: >>>> 点此下载 <<<< 下载后,找到VisualSVN的安装目录,例如:C:\Program ...
- HttpWebRequest抓取网页内容与直接输入URL得到的内容不一致!球大神帮忙!!
一.前言 我在做一个百度收录情况查询的软件,就是通过软件来批量查询文章链接是否被百度收录,主要是用来查询某个网址的收录次数还有网站的排行数,思路是借鉴别人的. 二.问题描述 首先需要考虑的是能够支持哪 ...
- Windows 7 任务栏图标消失(变透明,仍然占有地方,但是点击无反应)的解决方法
解决方案:清理资源管理器缓存(重启资源管理器) 1.打开程序管理器(ctrl+shift+esc) 2.在进程那里找到"explorer.exe",然后按结束进程 3.然后在文件( ...