在使用的HttpClient的之前先了解一下它是什么,为了尽可能的展示其最为原味的介绍,我们就去他的官网看看。http://hc.apache.org/httpcomponents-client-ga/  首页繁体繁体内容如下:

该图片内容是通过谷歌游览器自带翻译插件进行翻译的内容。

我在实际工作中一般进行后台跨域和不同服务接口调用使用比较频繁,自己拿他做过一些简单的爬虫。后面会给大家写一篇实现百度搜索的爬虫。

开始我们的博客主题内容:。获取请求示例,在开始介绍之前我们需要进行一写准备工作确保已经安装JDK和Maven的环境,并对JDK和行家有一定的了解另外开发工具这里我这里使用的是:Spring Tool Suite(STS)(当然你也可以使用其他的开发工具进行。环境和版本说明大致如下:

开发工具:Spring Tool Suite(STS)   3.9.6.RELEASE

maven版本:3.2.5

jdk版本: 1.8.0_144

首先添加的的HttpClient的依赖

<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.3</version>
</dependency>

引入的log4j的的依赖

<dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.1.1</version>
</dependency>
<dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version>
</dependency>

log4j.properties

log4j.rootLogger=INFO,stdout### \u8F93\u51FA\u5230\u63A7\u5236\u53F0 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern =  %d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n

获取请求示例操作流程

1我们要先创建HttpClient的对象

2创建HTTPGET对象并通过构造设置访问的网址以及调用参数

3执行的HttpClient的执行方法发起GET请求并返回CloseableHttpResponse对象

4从CloseableHttpResponse对象中获取响应状态以及响应内容。

获得请求示例演示程序详细代码:

package cn.zhuoqianmingyue.getoperation;import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;public class SimpleGetHttpClientDemo {private static Log log  =  LogFactory.getLog(SimpleGetHttpClientDemo.class );   /***  无参数的get访问*/@Testpublic void withoutParameters() {//创建HttpClinetCloseableHttpClient httpClient = HttpClients.createDefault();//添加HTTP GET请求 访问百度首页HttpGet httpGet = new HttpGet("https://www.baidu.com");CloseableHttpResponse response = null;try {//执行请求访问response = httpClient.execute(httpGet);//获取返回HTTP状态码int satausCode = response.getStatusLine().getStatusCode();if(satausCode == 200 ){String content = EntityUtils.toString(response.getEntity(),"UTF-8");log.info("百度首页页面:"+content);}} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}/***  有参数的访问* @throws URISyntaxException*/@Testpublic void withParameters() throws URISyntaxException {//创建HttpClinetCloseableHttpClient httpClient = HttpClients.createDefault();//拼接访问url 进行URI uri = new URI("http://www.baidu.com/s");//拼接搜索内容 ?wd=httpclinetURIBuilder uriBuilder = new URIBuilder(uri);uriBuilder.setParameter("wd", "httpclient");URI uriParma = uriBuilder.build();//添加HTTP GET请求 访问百度搜索httpclient相关信息HttpGet httpGet = new HttpGet(uriParma);CloseableHttpResponse response = null;try {response = httpClient.execute(httpGet);int satausCode = response.getStatusLine().getStatusCode();if(satausCode == 200 ){String content = EntityUtils.toString(response.getEntity(),"UTF-8");log.info(content);}} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}/***  访问https://www.baidu.com 搜索需要设置请求头的 Host:www.baidu.com* @throws URISyntaxException*/@Testpublic void withParametersByHttps() throws URISyntaxException {//创建HttpClinetCloseableHttpClient httpClient = HttpClients.createDefault();//拼接访问url 进行URI uri = new URI("https://www.baidu.com/s");//拼接搜索内容 ?wd=httpclinetURIBuilder uriBuilder = new URIBuilder(uri);uriBuilder.setParameter("wd", "httpclient");URI uriParma = uriBuilder.build();//添加HTTP GET请求 访问百度搜索httpclient相关信息HttpGet httpGet = new HttpGet(uriParma);httpGet.addHeader("Host","www.baidu.com");httpGet.addHeader("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36");CloseableHttpResponse response = null;try {response = httpClient.execute(httpGet);int satausCode = response.getStatusLine().getStatusCode();if(satausCode == 200 ){String content = EntityUtils.toString(response.getEntity(),"UTF-8");log.info(content);}} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}

源码地址:https://github.com/zhuoqianmingyue/httpclientexamples

HttpClient4.x之Get请求示例相关推荐

  1. HttpClient4.x之Post请求示例

    Post操作相对于Get操作变化并不是很大,我们只是需要将原来的HttpGet改成HttpPost.不了解获取提交操作的可以参看我的另一篇博客HttpClient4.x之获取请求示例  .但是如果需要 ...

  2. 通过RobotFramework简单的接口请求示例

    为什么80%的码农都做不了架构师?>>>    一个简单的接口请求示例,接口测试将在此基础上进行! 库文件的导入: 需要导入的库 Collections 和 RequestsLibr ...

  3. jQuery Ajax: $.post请求示例

    jQuery Ajax: $.post请求示例 leyangjun.html页面 <html> <head> <meta http-equiv="Content ...

  4. koa ajax,nodejs之koa2请求示例(GET,POST)

    nodejs之koa2请求示例(GET,POST) 发布时间:2020-10-12 14:04:56 来源:脚本之家 阅读:99 作者:骑乌龟赛跑 GET 请求 在 koa 中,GET请求获取请求数据 ...

  5. 梦网云通讯获取状态报告API接口get_rpt说明POST请求示例

    Post请求示例 urlencode userid=J10003&pwd=26dad7f364507df18f3841cc9c4ff94d&timestamp=0803192020&a ...

  6. golang使用http client发起get和post请求示例

    golang使用http client发起get和post请求示例 golang要请求远程网页,可以使用net/http包中的client提供的方法实现.查看了官方网站有一些示例,没有太全面的例子,于 ...

  7. PHP请求示例API接口,商品详情数据的获取

    PHP(PHP: Hypertext Preprocessor)即"超文本预处理器",是在服务器端执行的脚本语言,尤其适用于Web开发并可嵌入HTML中.PHP语法学习了C语言,吸 ...

  8. API请求示例汇总,亲测有效,taobaotmall,1688,pinduoduo等平台数据,信息化产业

    请求示例 Curl PHP PHPsdk JAVA C# Python -- 请求示例 url 默认请求参数已经URL编码处理 curl -i "https://api-gw.oneboun ...

  9. 微信域名检测php,微信域名检测接口(官方api)——PHP请求示例

    域名检测接口是腾讯发布的微信域名状态查询接口,可实时查询域名在微信中的状态,如果已被拦截返回结果提示"域名被封",如果未有异常则返回结果提示"域名正常". 域名 ...

最新文章

  1. 【转】Oozie4.2.0配置安装实战
  2. wordpress图片水印插件DX-Watermark
  3. Simulink仿真 第七节 关系运算符及逻辑运算符模块
  4. JMeter 性能测试进阶实战
  5. 复习深入笔记02:魔法方法/cookie,session,token/异常
  6. 四大组件之Activity
  7. 时间函数java_Java时间函数整理
  8. 状态压缩DP 图文详解(一)
  9. 解决gbk转utf8乱码
  10. 开发QQ桌球瞄准器(1):桌球瞄准器介绍与使用方法
  11. 岁月划过生命线(从阿里到微店)
  12. 美国交通安全管理局出台更加严格机场安全检查新规
  13. Android设备实现语音视频通话
  14. Leetcode. Largest Rectangle in Histogram
  15. 形容词,名词记忆(三):ment, ent后缀常用词
  16. 心法利器[78] | 端到端任务的拆解设计
  17. 新学期、新目标、迎接新的自己
  18. 修复打开Excel提示
  19. 【华为云技术分享】华为云弹性云服务器ECS搭建FTP服务实践
  20. yDAI受创 Curve“喜”收意外之财

热门文章

  1. 计算机汉字的输入和编辑教案,计算机汉字录入教案
  2. mysql前两个月_MySQL数据库表始终保持最近两个月的记录
  3. 使用XLocalizer进行ASP.NET Core本地化
  4. 可以将道德条款纳入开源许可证吗?
  5. 百度地图手机和电脑不一致_手机能下载北斗星导航吗?比高德地图和百度地图好用吗?...
  6. 计算机一级考试第一套题电子表格,计算机等级考试一级上机试题(第一套)
  7. php xingnengfenxi_PHP 性能分析第三篇: 性能调优实战
  8. 关于mybatis的mapper和mapper.xml注入spring托管的方法 超详细
  9. layui遇见的问题
  10. padding、margin百分比