簡介
官網(wǎng):http://hc.apache.org/httpclient-3.x/
HttpClient是 Apache Jakarta Common 下的子項目,用來提供高效的、最新的、功能豐富的支持 HTTP 協(xié)議的客戶端編程工具包,并且它支持 HTTP 協(xié)議最新的版本和建議。雖然在 JDK 的 java net包中已經(jīng)提供了訪問 HTTP 協(xié)議的基本功能,但是對于大部分應(yīng)用程序來說,JDK 庫本身提供的功能還不夠豐富和靈活。
HttpClient不是瀏覽器,它是一個HTTP通信庫,
HttpClient的主要功能:
實現(xiàn)了所有 HTTP 的方法(GET、POST、PUT、HEAD、DELETE、HEAD、OPTIONS 等)
支持 HTTPS 協(xié)議
支持代理服務(wù)器(Nginx等)等
支持自動(跳轉(zhuǎn))轉(zhuǎn)向
這些httpclient 要依賴moco框架內(nèi)容
HttpClientDemo
public class MyHttpClient {
? ? @Test
? ? public void test1() throws IOException {
? ? ? ? //用來存放我們的結(jié)果
? ? ? ? String result;
? ? ? ? HttpGet get = new HttpGet("http://www.baidu.com");
? ? ? ? //這個用來執(zhí)行g(shù)et方法
? ? ? ? //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對象,用來進(jìn)行方法的執(zhí)行,并設(shè)置cookies信息
? ? ? ? CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(this.store).build();
? ? ? ? //執(zhí)行g(shù)et方法,并獲得結(jié)果
? ? ? ? CloseableHttpResponse response = client.execute(get);
? ? ? ? //獲取響應(yīng)的狀態(tài)碼,判斷返回的結(jié)果是否符合預(yù)期
? ? ? ? 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對象,用來進(jìn)行方法的執(zhí)行
? ? ? ? CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(this.store).build();
? ? ? ? //聲明一個方法,這個方法就是post方法
? ? ? ? HttpPost post =new HttpPost(testUrl);
? ? ? ? //添加參數(shù);這個也可以優(yōu)化,用例里面很多參數(shù)
? ? ? ? JSONObject param =new JSONObject();
? ? ? ? param.put("name", "jane");
? ? ? ? param.put("age", "18");
? ? ? ? //設(shè)置請求頭信息 設(shè)置header信息;若公用的,可以優(yōu)化到一個header工具類方法中
? ? ? ? post.setHeader("content-type", "application/json");
? ? ? ? // 將參數(shù)信息添加到方法中
? ? ? ? StringEntity entity =new StringEntity(param.toString(), "utf-8");
? ? ? ? post.setEntity(entity);
? ? ? ? //聲明一個對象來進(jìn)行響應(yīng)結(jié)果的存儲
? ? ? ? String result;
? ? ? ? //設(shè)置cookies信息
//client.setCookieStore(this.store);
//執(zhí)行post方法
? ? ? ? HttpResponse response = client.execute(post);
? ? ? ? //獲取響應(yīng)結(jié)果
? ? ? ? result = EntityUtils.toString(response.getEntity(), "utf-8");
? ? ? ? System.out.println(result);
? ? ? ? //處理結(jié)果,判斷返回結(jié)果是否符合預(yù)期
//將返回的響應(yīng)結(jié)果字符串轉(zhuǎn)化為json對象
? ? ? ? JSONObject resultJson =new JSONObject(result);
? ? ? ? //具體判斷返回結(jié)果的值
//獲取到結(jié)果值
? ? ? ? String name = (String)resultJson.get("name");
? ? ? ? String status = (String)resultJson.get("status");
? ? ? ? Assert.assertEquals("success", name);
? ? ? ? Assert.assertEquals("1", status);
? ? }
}