Java-接口自动化测试-HttpClient的使用(Get请求中的Cookie、Header、与参数的处理)

1:HttpClient模拟一个get请求的Demo

1.1:Maven添加HttpClient依赖


	org.apache.httpcomponents
	httpclient
	4.5.3

1.2:代码实现 模拟baidu的Get请求

package cn.Demo.HttpClient;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.Test;

/**
 * HttpClient get请求 模拟请求baidu 获取返回值
 * */

public class GetDemo {
	
	@Test
	public void baiduReturn() throws Exception, IOException{
		
		//创建Get对象 请求URL要写全 否则会报Host的异常
		HttpGet get = new HttpGet("http://www.baidu.com");
		//创建get方法的执行对象
		//HttpClient client = new DefaultHttpClient();
        // 创建get方法的执行对象 HttpClient4.X之后是这样创建client对象的
		CloseableHttpClient client = HttpClients.createDefault();
		//获取response对象
		HttpResponse response = client.execute(get);
		//将response对象转换成String类型
		String responseStr = EntityUtils.toString(response.getEntity(),"utf-8");
		
		System.out.println(responseStr);
	}
}

1.3:运行查看打印

查看打印 可以看到response的html返回页全部都打印出来了

2:获取Cookies信息

2.1:编写Test.json文件

实际这是两个接口 /GetDemo/withCookies接口是获取Cookies,/GetDemo/useCookies接口是使用cookies才能访问

[
	{		
		"description":"Mock模拟接口,返回Cookies",
		"request":{			
			"uri":"/GetDemo/withCookies",
			"method":"get"
		},
		
		"response":{
			"json":{
				"Code":"Success",
				"Data":{
					"Link":"./locatin/xxx.jpg",
					"Message":"Mock模拟的带Header信息的请求"
				}			
			},
			
			"cookies":{				
				"sessionID":"AABBCCDD"
				}
		}	
	},
	
	{
	
		"description":"Mock模拟接口,使用sessionID才能访问成功",
		"request":{
			"uri":"/GetDemo/useCookies",
			"method":"get",
			"cookies":{
				"sessionID":"AABBCCDD"
				}
			},
			
		"response":{
			"json":{
				"Link":"/GetDemo/useCookies",
				"Data":{
					"name":"Anndy",
					"age":18,
					"Time":"2018-9-9"
				}
			}
		}
	}
]

2.2:HttpClient4.x获取Cookie

	public static void main(String[] args) {

		try {
			// 创建Get对象 请求URL要写全 否则会报Host的异常
			HttpGet get = new HttpGet("http://localhost:8888/GetDemo/withCookies");
			//创建CookieStore对象用来获取cookie
			CookieStore cookieStore = new BasicCookieStore();			
			// 创建get方法的执行对象 HttpClient4.X之后是这样创建client对象的
			CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
			
			// 获取response对象
			HttpResponse response= client.execute(get);

			// 将response对象转换成String类型
			String responseStr = EntityUtils.toString(response.getEntity(), "utf-8");
			System.out.println(responseStr);
			
			//获取到的Cookie是应该Cookie为泛型的List集合
			List cookies = cookieStore.getCookies();
			
			for (Cookie cookie : cookies) {
				
				System.out.println(cookie.toString());
				System.out.println(cookie.getName()+"--"+cookie.getValue());
			}

		} catch (Exception e) {

			e.printStackTrace();
		}
	}

2.3:执行结果

3:HttpClient4.x设置访问是添加设置Cookies信息

同样还是2.1的Mock文件,携带Cookie访问的代码如下

	public static void main(String[] args) {

		try {
			// 创建get访问对象
			HttpGet get = new HttpGet("http://localhost:8889/GetDemo/useCookies");
			// 创建CookieStore对象用来管理cookie
			CookieStore cookieStore = new BasicCookieStore();
			//new BasicClientCookie对象 用来注入cookie
			BasicClientCookie cookie = new BasicClientCookie("sessionID", "AABBCCDD");
			
			cookie.setDomain("localhost");//设置cookie的作用域
			
			cookieStore.addCookie(cookie);//将cookie添加到cookieStore中
			// 创建get方法的执行对象 HttpClient4.X之后是这样创建client对象 设置cookies和header信息
			CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(cookieStore).build();

			HttpResponse response = client.execute(get);

			// 将response对象转换成String类型
			String responseStr = EntityUtils.toString(response.getEntity(), "utf-8");
			System.out.println(responseStr);
			
		} catch (Exception e) {

			e.printStackTrace();
		}
	}

4:Get请求中设置Header信息

4.1:Mock接口实现的json文件

[
	{		
		"description":"这是Mock接口,带Header和参数的Demo",
		"request":{			
			"uri":"/GetDemo/withHeaders",
			"method":"get",
			"headers":{
				"context-Type":"AA",
				"Angent":"BB"
			},
			"queries":{
				"name":"Anndy",
				"age":"18"
			}
		},
		
		"response":{
			"json":{
				"Code":"Success",
				"Data":{
					"Link":"./locatin/xxx.jpg",
					"Message":"Mock模拟的带Header信息的请求"
				}			
			}
		}			
	}
]

4.2:代码实现

	public static void main(String[] args) {

		try {
			//创建URLBuilder对象
			URIBuilder uriBuilder = new URIBuilder("http://localhost:8889/GetDemo/withHeaders");
			//创建集合 添加参数
			List list = new LinkedList<>();
			BasicNameValuePair param1 = new BasicNameValuePair("name", "Anndy");
			BasicNameValuePair param2 = new BasicNameValuePair("age", "18");
			list.add(param1);
			list.add(param2);
			uriBuilder.addParameters(list);

			// 创建get访问对象
			HttpGet get = new HttpGet(uriBuilder.build());
			//设置Headers头信息
			get.setHeader("context-Type", "AA");
			get.setHeader("Angent", "BB");

			CloseableHttpClient client = HttpClients.createDefault();

			HttpResponse response = client.execute(get);

			// 将response对象转换成String类型
			String responseStr = EntityUtils.toString(response.getEntity(), "utf-8");
			System.out.println(responseStr);

		} catch (Exception e) {

			e.printStackTrace();
		}
	}

 

 

你可能感兴趣的:(Java接口自动化测试)