httpclient就是模拟http协议,客户端的一种技术,get、post,http://hc.apache.org/ 官方网站。在pom文件中引入依赖包
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1.2</version>
</dependency>
第一个demo
public class MyHttpClient {
@Test
public void test1() throws IOException {
//用来存放我们的结果
String result;
HttpGet get = new HttpGet("http://www.baidu.com");
//这个是用来执行get方法的
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(get);
result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);
}
}
带cookie的get请求
public class MyCookiesForGet {
private String url;
/**java util包里自带的方法 bundle*/
private ResourceBundle bundle;
//用来存储cookies信息的变量
private CookieStore store;
@BeforeTest
public void beforeTest(){
/**默认识别properties文件,直接写application名就可以,必须在resources目录下,编码格式*/
bundle = ResourceBundle.getBundle("application", Locale.CHINA);
/**写配置文件里的key,会取他的值*/
url = bundle.getString("test.url");
}
@Test
public void testGetCookies() throws IOException {
String result;
/**从配置文件中获取uri*/
String uri = bundle.getString("getCookies.uri");
/**拼接*/
String testUrl = this.url+uri;
// 测试逻辑代码书写
HttpGet get = new HttpGet(testUrl);
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(get);
result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);
//获取cookies信息
this.store = client.getCookieStore();
List<Cookie> cookieList = 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 {
String uri = bundle.getString("test.get.with.cookies");
String testUrl = this.url+uri;
HttpGet get = new HttpGet(testUrl);
DefaultHttpClient client = new DefaultHttpClient();
//设置cookies信息
client.setCookieStore(this.store);
HttpResponse response = client.execute(get);
//获取响应的状态码
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("statusCode = " + statusCode);
if(statusCode == 200){
String result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);
}
}
}
带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("test.url");
}
@Test
public void testGetCookies() throws IOException {
String result;
// 从配置文件中 拼接测试的url
String uri = bundle.getString("getCookies.uri");
String testUrl = this.url+uri;
// 测试逻辑代码书写
HttpGet get = new HttpGet(testUrl);
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(get);
result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);
//获取cookies信息
this.store = client.getCookieStore();
List<Cookie> cookieList = 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);
//添加参数
JSONObject param = new JSONObject();
param.put("name","xiaoqiang");
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("xiaoqiang");
String status = (String) resultJson.get("status");
//具体的判断返回结果的值
Assert.assertEquals("success",success);
Assert.assertEquals("1",status);
}
}
用之前moco框架mock出这几个接口
[
{
"description":"这是一个会返回cookies信息的get请求",
"request":{
"uri":"/getCookies",
"method":"get"
},
"response":{
"cookies":{
"login":"true"
},
"text":"恭喜你获得cookies信息成功",
"headers":{
"Content-Type":"text/html;charset=gbk"
}
}
},
{
"description":"这是一个带cookies信息的get请求",
"request":{
"uri":"/get/with/cookies",
"method":"get",
"cookies":{
"login":"true"
}
},
"response":{
"text":"这是一个需要携带cookies信息才能访问的get请求",
"headers":{
"Content-Type":"text/html;charset=gbk"
}
}
},
{
"description":"这是一个带cookies信息的post请求",
"request":{
"uri":"/post/with/cookies",
"method":"post",
"cookies":{
"login":"true"
},
"json":{
"name":"xiaoqiang",
"age":"18"
}
},
"response":{
"status":200,
"json":{
"xiaoqiang":"success",
"status":"1"
},
"headers":{
"Content-Type":"text/html;charset=gbk"
}
}
}
]
启动这个mock应用,然后运行test方法,就可以正常返回信息。