一、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); } }