HttpClient中的各种请求
一:什么是HttpClient?
主要是模拟浏览器发送请求给server,监听响应信息,推断返回结果的正确性怎样,因为避开的浏览器的UI
,也就将浏览器中全部载入的时间(比方常常要载入图片啊)都省掉了,所以这个运行效率相当高
二. 使用HttpClient
1.get请求
@Testpublic 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);HttpEntity entity = response.getEntity();String string = EntityUtils.toString(entity, "utf-8");System.out.println(string);//关闭httpclientresponse.close();httpClient.close();}
2.带参数的get请求
@Testpublic void doGetWithParam() throws Exception{//创建一个httpclient对象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);//关闭httpclientresponse.close();httpClient.close();}
3.post请求
@Testpublic void doPost() throws Exception {CloseableHttpClient httpClient = HttpClients.createDefault();//创建一个post对象HttpPost post = new HttpPost("http://localhost:8082/httpclient/post.html");//执行post请求CloseableHttpResponse response = httpClient.execute(post);String string = EntityUtils.toString(response.getEntity());System.out.println(string);response.close();httpClient.close();}
4.带参数的post请求
@Testpublic void doPostWithParam() throws Exception{CloseableHttpClient httpClient = HttpClients.createDefault();//创建一个post对象HttpPost post = new HttpPost("http://localhost:8082/httpclient/post.html");//创建一个Entity。模拟一个表单List<NameValuePair> kvList = new ArrayList<>();kvList.add(new BasicNameValuePair("username", "zhangsan"));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();}
HttpClient中的各种请求相关推荐
- Android中的HTTP请求
在Android 中发送HTTP请求的方式一般有两种,HttpURLConnection和HttpClient,下面介绍的是HttpURLConnection的使用方法. 1.定义xml文件 < ...
- Blazor WebAssembly 应用程序中进行 HTTP 请求
翻译自 Waqas Anwar 2021年5月13日的文章 <Making HTTP Requests in Blazor WebAssembly Apps> [1] 在我的前篇文章< ...
- Blazor Server 应用程序中进行 HTTP 请求
翻译自 Waqas Anwar 2021年5月4日的文章 <Making HTTP Requests in Blazor Server Apps> [1] Blazor Server 应用 ...
- 小心 HttpClient 中的 FormUrlEncodeContent 的 bug
小心 HttpClient 中的 FormUrlEncodeContent 的 bug Intro 最近发现活动室预约项目里的上传图片有时候会有问题,周末找时间测试了一下,发现小图片的上传没问题,大图 ...
- java httpclient 跨域_13、HttpClient服务器跨域请求
1 回调 1.1 回调函数 1.1.1 回调的原理图 说明:在架构设计中,回调的机制经常会被使用,课下自行学习. 1.2 JSON的数据结构 1.2.1 JSON官网介绍 1.2.2 Object格式 ...
- HttpClient发送get post请求和数据解析
最近在跟app对接的时候有个业务是微信登录,在这里记录的不是如何一步步操作第三方的,因为是跟app对接,所以一部分代码不是由我写, 我只负责处理数据,但是整个微信第三方的流程大致都差不多,app端说要 ...
- 使用httpclient进行 Http get请求带参数cookie
使用httpclient进行 Http get请求带参数cookie 常用的工具类,所有的包配置如下 <!--解析json使用--><dependency><groupI ...
- 微服系列之Feign使用HttpClient和OkHttp网络请求框架
Feign实现了远程调用,底层默认使用的是HttpURLConnection网络请求框架 那Feign支不支持其他的网络请求框架呢,答案那是肯定的,Feign还支持HttpClient和OkHttp, ...
- 与第三方进行联调测试,对方将JSON形式的参数放到params中,接收请求后获取参数值为null
第三方将参数放入请求体中,请求过来之后,这边request.getParameter("params")获取不到参数. 第三方用的Content-Type是application/ ...
最新文章
- NAND Flash和NOR Flash的区别
- 第二阶段_第二小节_C#调试
- where is Fiori count server execution interval configured
- 紫色UI趣味测试微信小程序源码下载包含多种评测
- 三、索引优化(4)索引碎片
- wits数据格式_WITS标准
- 设计模式学习笔记--Flyweight享元模式
- Linux磁盘下面有个mpatha,Linux中如何使用vmstat命令
- POJ3641 UVA11287 HDU1905 Pseudoprime numbers【素数判定+快速模幂】
- 速达财务管理软件使用方法
- CSS 世界 阅读笔记:(Ch1-Ch3)
- 用acdsee制作html,ACDSee软件图像处理操作实用技巧
- theano java_Windows Theano GPU 版配置
- 禅道----产品经理创建产品
- DSP之LCD1602笔记
- 微信小程序——打开地图 选择位置 完整功能实现代码(定位,检索周边,可移动选点,可搜索,腾讯地图API)
- 根据身份证号(18/15)计算年龄、出生日期、性别
- js遍历后端传来的json化的list
- 使用icecast搭建在线电台并使用ffmpeg推流
- 【应急基础】————8、辅助脚本