httpclient几种请求方式
一、httpclient 模拟get请求,并获取cookie信息
public class MyCookiesForGet {
private String url;
//用来读取.properties的配置文件
private ResourceBundle bundle;
private CookieStore store;
@BeforeTest
public void beforeTest() {
//读取加载.properties的配置文件
bundle = ResourceBundle.getBundle("application",Locale.CHINA);
url = bundle.getString("test.url");
}
@Test
public void testGetCookies() throws IOException {
String result;
//从配置文件中 拼接测试的url
String uri = bundle.getString("getCookies.uri");
String testUrl = this.url + uri;
//HttpGet 相当于在浏览器地址栏输入一个地址
HttpGet get = new HttpGet(testUrl);
//DefaultHttpClient 相当于一个浏览器,先new一个浏览器
DefaultHttpClient client = new DefaultHttpClient();
//client.execute(get) 相当于用浏览器打开网页了,获得一个response结果
HttpResponse response = client.execute(get);
//response.getEntity() 打印请求的实体内容 返回json格式
//EntityUtils.toString() 将json格式实体内容转换成字符串String
result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);
//获取cookies信息
this.store = client.getCookieStore();
List<Cookie> cookieList = this.store.getCookies();
for (Cookie cookie : cookieList) {
String name = cookie.getName();
String value = cookie.getValue();
System.out.println("cookie name = "+name+",cookie value = "+value);
}
}
}
二、httpclient 模拟携带cookie信息进行的get请求
public class MyCookiesForGet {
private String url;
//用来读取.properties的配置文件
private ResourceBundle bundle;
private CookieStore store;
@BeforeTest
public void beforeTest() {
//读取加载.properties的配置文件
bundle = ResourceBundle.getBundle("application",Locale.CHINA);
url = bundle.getString("test.url");
}
@Test
public void testGetCookies() throws IOException {
String result;
//从配置文件中 拼接测试的url
String uri = bundle.getString("getCookies.uri");
String testUrl = this.url + uri;
//HttpGet 相当于在浏览器地址栏输入一个地址
HttpGet get = new HttpGet(testUrl);
//DefaultHttpClient 相当于一个浏览器,先new一个浏览器
DefaultHttpClient client = new DefaultHttpClient();
//client.execute(get) 相当于用浏览器打开网页了,获得一个response结果
HttpResponse response = client.execute(get);
//response.getEntity() 打印请求的实体内容 返回json格式
//EntityUtils.toString() 将json格式实体内容转换成字符串String
result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);
//获取cookies信息
this.store = client.getCookieStore();
List<Cookie> cookieList = this.store.getCookies();
for (Cookie cookie : cookieList) {
String name = cookie.getName();
String value = cookie.getValue();
System.out.println("cookie name = "+name+",cookie value = "+value);
}
}
@Test(dependsOnMethods = {"testGetCookies"})
public void testGetWithCookies() throws IOException {
//测试url拼接,使用ResourceBundle bundle 来读取.properties文件配置内容
String uri = bundle.getString("test.get.with.cookies");
String testUrl = this.url + uri;
//HttpGet 相当于在浏览器中输入url
HttpGet get = new HttpGet(testUrl);
//DefaultHttpClient 相当于创建打开一个浏览器
DefaultHttpClient client = new DefaultHttpClient();
//设置cookies信息
client.setCookieStore(this.store);
//HttpResponse 相当于浏览器打开网页,并或得了响应结果
HttpResponse response = client.execute(get);
//获取响应状态码
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("statusCode = "+statusCode);
if(statusCode == 200) {
//获取响应格式为json的实体内容,并转换为字符串
String result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);
}
}
}
三、httpclient模拟携带cookie信息的post请求(请求格式为json)
public class MyCookiesForPost {
private String url;
//用来读取.properties的配置文件
private ResourceBundle bundle;
private CookieStore store;
@BeforeTest
public void beforeTest() {
//读取加载.properties的配置文件
bundle = ResourceBundle.getBundle("application",Locale.CHINA);
url = bundle.getString("test.url");
}
@Test
public void testGetCookies() throws IOException {
String result;
//从配置文件中 拼接测试的url
String uri = bundle.getString("getCookies.uri");
String testUrl = this.url + uri;
//HttpGet 相当于在浏览器地址栏输入一个地址
HttpGet get = new HttpGet(testUrl);
//DefaultHttpClient 相当于一个浏览器,先new一个浏览器
DefaultHttpClient client = new DefaultHttpClient();
//client.execute(get) 相当于用浏览器打开网页了,获得一个response结果
HttpResponse response = client.execute(get);
//response.getEntity() 打印请求的实体内容 返回json格式
//EntityUtils.toString() 将json格式实体内容转换成字符串String
result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);
//获取cookies信息
this.store = client.getCookieStore();
List<Cookie> cookieList = this.store.getCookies();
for (Cookie cookie : cookieList) {
String name = cookie.getName();
String value = cookie.getValue();
System.out.println("cookie name = "+name+",cookie value = "+value);
}
}
@Test(dependsOnMethods = {"testGetCookies"})
public void testPostMethod() throws IOException {
String uri = bundle.getString("test.post.with.cookies");
//拼接最终的测试地址
String testUrl = this.url + uri;
//声明一个client对象,用来进行方法的执行;相当于打开一个浏览器
DefaultHttpClient client = new DefaultHttpClient();
//声明一个方法,这个方法就是post方法;相当于在浏览器中输入一个请求地址
HttpPost post = new HttpPost(testUrl);
//添加参数(json格式类型的请求参数)
JSONObject param = new JSONObject();
param.put("name","huhansan");
param.put("age","18");
//设置请求头信息 设置header
post.setHeader("content-type","application/json");
//将参数信息添加到方法中,将参数绑定到请求信息中
StringEntity entity = new StringEntity(param.toString(),"utf-8");
post.setEntity(entity);
//声明一个对象用来进行响应结果的存储
String result;
//设置cookies信息
client.setCookieStore(this.store);
//执行post方法
HttpResponse response = client.execute(post);
//获取响应结果
result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);
//处理结果,判断返回结果是否符合预期
//将返回的响应结果字符串转换成json对象
JSONObject resultJson = new JSONObject(result);
//判断具体的返回结果值
String success = (String) resultJson.get("huhansan");
String status = (String) resultJson.get("status");
Assert.assertEquals("success",success);
Assert.assertEquals("1",status);
}
}
四、httpclient模拟携带cookie信息的post请求(请求格式为表单)
public class MyCookiesForPost {
private String url;
private ResourceBundle bundle;
//用来存储cookies信息的变量
private CookieStore store;
@BeforeTest
public void beforeTest() {
bundle = ResourceBundle.getBundle("application", Locale.CHINA);
url = bundle.getString("wms.login.url");
}
@Test
public void testGetCookies() throws IOException {
String result;
String uri = bundle.getString("wms.login.uri");
String testUrl = this.url + uri;
//测试逻辑代码编写
HttpGet get = new HttpGet(testUrl);
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(get);
System.out.println(response);
result = EntityUtils.toString(response.getEntity(),"utf-8");
//System.out.println(result);
//获取cookies信息
this.store = client.getCookieStore();
List<Cookie> cookieList = this.store.getCookies();
for(Cookie cookie : cookieList) {
String name = cookie.getName();
String value = cookie.getValue();
System.out.println("cookie name = " + name + ";cookie value = "+value);
}
}
@Test(dependsOnMethods = {"testGetCookies"})
public void testPostMethod() throws IOException {
String uri = bundle.getString("wms.login.validate");
//拼接最终测试地址
String testUrl = this.url + uri;
//声明一个client对象,用来进行方法的执行
DefaultHttpClient client = new DefaultHttpClient();
//声明一个方法,这个方法就是post方法
HttpPost post = new HttpPost(testUrl);
//添加参数
List <NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("username","ldcx"));
nvps.add(new BasicNameValuePair("password","123456"));
//设置请求头信息 设置header
post.setHeader("content-type","application/x-www-form-urlencoded");
//将参数信息添加到方法中
HttpEntity entity = new UrlEncodedFormEntity(nvps,"utf-8");
//StringEntity entity = new StringEntity(nvps.toString(),"utf-8");
post.setEntity(entity);
System.out.println("请求url地址"+post.getURI());
//声明一个对象来进行响应结果的存储
String result;
//设置cookies信息
client.setCookieStore(this.store);
//执行post请求
HttpResponse response = client.execute(post);
//获取响应结果
result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);
}
@Test(dependsOnMethods = {"testPostMethod"})
public void testGetWareHouse() throws IOException {
String result;
String uri = bundle.getString("wms.warehouse");
String testUrl = this.url + uri;
//测试逻辑代码编写
HttpGet get = new HttpGet(testUrl);
DefaultHttpClient client = new DefaultHttpClient();
client.setCookieStore(this.store);
HttpResponse response = client.execute(get);
System.out.println(response);
result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);
}
}
httpclient几种请求方式的更多相关文章
- HttpwebClient的四种请求方式
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷. 本文旨在发布代码,供自己参考,也供大家参考,谢谢. 正题: Ht ...
- Ajax中的get和post两种请求方式的异同
Ajax中我们经常用到get和post请求.那么什么时候用get请求,什么时候用post方式请求呢? 在做回答前我们首先要了解get和post的区别. 1. get是把参数数据队列加到提交表单的A ...
- Ajax的get和post两种请求方式区别
Ajax的get和post两种请求方式区别 (摘录):http://ip-10000.blog.sohu.com/114437748.html 解get和post的区别. 1. get是把参数数据队列 ...
- SpringMVC的REST风格的四种请求方式
一. 在HTTP 协议里面,四个表示操作方式的动词:GET.POST.PUT.DELETE. ·它们分别对应四种基本操作: 1.GET ====== 获 取资源 2.POST ======新建资源 ...
- 【spring springmvc】这里有你想要的SpringMVC的REST风格的四种请求方式
概述 之前的文章springmvc使用注解声明控制器与请求映射有简单提到过控制器与请求映射,这一次就详细讲解一下SpringMVC的REST风格的四种请求方式及其使用方法. 你能get的知识点 1.什 ...
- 第三节:总结.Net下后端的几种请求方式(WebClient、WebRequest、HttpClient)
一. 前言 前端调用有Form表单提交,ajax提交,ajax一般是用Jquery的简化写法,在这里不再过多介绍: 后端调用大约有这些:WebCient.WebRequest.Httpclient.W ...
- jQuery中的Ajax几种请求方式
1. load( url, [data], [callback] ) :载入远程 HTML 文件代码并插入至 DOM 中. url (String) : 请求的HTML页的URL地址. data (M ...
- http8种请求方式
根据HTTP标准,HTTP请求可以使用多种请求方法. HTTP1.0定义了三种请求方法: GET, POST 和 HEAD方法. HTTP1.1新增了五种请求方法:OPTIONS, PUT, DELE ...
- 两种请求方式URLHttpconnection 和Httpclient提交表单 网络篇(二)
安卓有两种发送请求的方式:URLHttpconnection 和Httpclient 下面就来讲下这两种方式,这篇是最基础的使用 进阶请看第二篇 先占位 打扫卫生去了T T 快过年了 框架就放网络篇 ...
随机推荐
- Subline Text 3 安装
Subline Text 3 下载 下载链接 http://www.sublimetext.com/3 ,下载Subline Text3的安装包,这里以 64位的windows10为例,如果是其他操作 ...
- 开发中的你的Git提交规范吗?
1. 前言 目前大部分公司都在使用Git作为版本控制,每个程序员每天都要进行代码的提交.很多开发者也包括我自己,有时候赶时间或者图省事,就这么提交: git commit -m "修改bug ...
- [Poi2005]Piggy Banks小猪存钱罐
题目描述 Byteazar有 N 个小猪存钱罐. 每个存钱罐只能用钥匙打开或者砸开. Byteazar已经把每个存钱罐的钥匙放到了某些存钱罐里. Byteazar 现在想买一台汽车于是要把所有的钱都取 ...
- guava eventbus 原理+源码分析
前言: guava提供的eventbus可以很方便的处理一对多的事件问题, 最近正好使用到了,做个小结,使用的demo网上已经很多了,不再赘述,本文主要是源码分析+使用注意点+新老版本eventbus ...
- Git安装/VScode+Git+Github
Git安装/VScode+Git+Github 1. 相关简介 git 版本控制工具,支持该工具的网站有Github.BitBucket.Gitorious.国内的OS China仓库.Csdn仓库等 ...
- (11)-Python3之--os模块
1.模块介绍 os模块是路径处理模块,它提供了多数操作系统的功能接口函数.当os模块被导入后,它会自适应于不同的操作系统平台,根据不同的平台进行相应的操作,在python编程时,经常和文件.目录打交道 ...
- LVS负载均衡理论以及算法概要
一. LVS简介 LVS是Linux Virtual Server的简称,也就是Linux虚拟服务器, 由章文嵩博士发起的自由软件项目,它的官方站点是www.linuxvirtualserver.or ...
- 阿里云弹性公网IP那些事 阿里云云栖号 6月1日 弹性公网IP是独立的公网IP资源,可以绑定到阿里云专有网络VPC类型的ECS、NAT网关、私网负载均衡SLB上,并可以动态解绑,实现公网IP和ECS、NAT网关、SLB的解耦,满足灵活管理的要求。阿里云弹性公网IP那些事 阿里云云栖号 6月1日 弹性络VPC类型的E
阿里云弹性公网IP那些事 阿里云云栖号 6月1日 弹性公网IP是独立的公网关.私网负载均衡SLB上,并可以动态解绑,实现公网IP和ECS.NAT网关.SLB的解耦,满足灵活管理的要求.
- Why failover-based implementations are not enough Redis分布式锁实现 SET resource_name my_random_value NX PX 30000
核心 SET resource_name my_random_value NX PX 30000 Distributed locks with Redis – Redis https://redis. ...
- tcpdump 参数详解及使用案例
参数 -A 以ASCII码方式显示每一个数据包(不会显示数据包中链路层头部信息). 在抓取包含网页数据的数据包时, 可方便查看数据(nt: 即Handy for capturing web pages ...