简介
官网:http://hc.apache.org/httpclient-3.x/
HttpClient是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。虽然在 JDK 的 java net包中已经提供了访问 HTTP 协议的基本功能,但是对于大部分应用程序来说,JDK 库本身提供的功能还不够丰富和灵活。
HttpClient不是浏览器,它是一个HTTP通信库,
HttpClient的主要功能:
实现了所有 HTTP 的方法(GET、POST、PUT、HEAD、DELETE、HEAD、OPTIONS 等)
支持 HTTPS 协议
支持代理服务器(Nginx等)等
支持自动(跳转)转向
这些httpclient 要依赖moco框架内容
HttpClientDemo
public class MyHttpClient {
@Test
public void test1() throws IOException {
//用来存放我们的结果
String result;
HttpGet get = new HttpGet("http://www.baidu.com");
//这个用来执行get方法
//HttpClient client = new DefaultHttpClient(); 这个过时了
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(get);
result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);
}
}
HttpClientForGet
public class MyCookiesForGet {
private Stringurl;
private ResourceBundlebundle;
//用来存储cookie信息的变量
private CookieStorestore ;
@BeforeTest
public void beforeTest(){
//会自动找到resource中application文件
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);
//HttpClient 没有办法获取cookie信息Default
// HttpClient client = new DefaultHttpClient();
//CookieStore store = new BasicCookieStore();
this.store =new BasicCookieStore();
CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(store).build();
// CloseableHttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(get);
result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);
//获取cookie信息
//CookieStore store = client.getCookiesStore();
//
List 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;
//声明一个get的方法
HttpGet get =new HttpGet(testUrl);
//声明一个client对象,用来进行方法的执行,并设置cookies信息
CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(this.store).build();
//执行get方法,并获得结果
CloseableHttpResponse 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);
}else {
System.out.println("访问getwithcookies失败");
}
}
}
HttpClientForPost
public class MyCookiesForPost {
private Stringurl;
private ResourceBundlebundle;
//用来存储cookie信息的变量
private CookieStorestore ;
@BeforeTest
public void beforeTest(){
//会自动找到resource中application文件
bundle = ResourceBundle.getBundle("application", Locale.CHINA);
url =bundle.getString("test.url");
}
//获取cookies信息
@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);
//HttpClient 没有办法获取cookie信息Default
// HttpClient client = new DefaultHttpClient();
//CookieStore store = new BasicCookieStore();
this.store =new BasicCookieStore();
CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(store).build();
// CloseableHttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(get);
result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);
//获取cookie信息
//CookieStore store = client.getCookiesStore();
List 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.wtih.cookies");
//拼接最终的测试地址
String testUrl =this.url + uri;
//声明一个Client对象,用来进行方法的执行
CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(this.store).build();
//声明一个方法,这个方法就是post方法
HttpPost post =new HttpPost(testUrl);
//添加参数;这个也可以优化,用例里面很多参数
JSONObject param =new JSONObject();
param.put("name", "jane");
param.put("age", "18");
//设置请求头信息 设置header信息;若公用的,可以优化到一个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 name = (String)resultJson.get("name");
String status = (String)resultJson.get("status");
Assert.assertEquals("success", name);
Assert.assertEquals("1", status);
}
}