簡(jiǎn)介
官網(wǎng):http://hc.apache.org/httpclient-3.x/
HttpClient是 Apache Jakarta Common 下的子項(xiàng)目,用來提供高效的盖文、最新的、功能豐富的支持 HTTP 協(xié)議的客戶端編程工具包,并且它支持 HTTP 協(xié)議最新的版本和建議姥芥。雖然在 JDK 的 java net包中已經(jīng)提供了訪問 HTTP 協(xié)議的基本功能据悔,但是對(duì)于大部分應(yīng)用程序來說,JDK 庫本身提供的功能還不夠豐富和靈活艾杏。
HttpClient不是瀏覽器韧衣,它是一個(gè)HTTP通信庫,
HttpClient的主要功能:
實(shí)現(xiàn)了所有 HTTP 的方法(GET购桑、POST畅铭、PUT、HEAD勃蜘、DELETE硕噩、HEAD、OPTIONS 等)
支持 HTTPS 協(xié)議
支持代理服務(wù)器(Nginx等)等
支持自動(dòng)(跳轉(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");
? ? ? ? //這個(gè)用來執(zhí)行g(shù)et方法
? ? ? ? //HttpClient client = new DefaultHttpClient(); 這個(gè)過時(shí)了
? ? ? 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;
? ? //用來存儲(chǔ)cookie信息的變量
? ? private CookieStorestore ;
? ? @BeforeTest
? ? public void beforeTest(){
//會(huì)自動(dòng)找到resource中application文件
? ? ? ? bundle = ResourceBundle.getBundle("application", Locale.CHINA);
? ? ? ? url =bundle.getString("test.url");
? ? }
@Test
? ? public void testGetCookies()throws IOException {
String result;
? ? ? ? //從配置文件中 拼接測(cè)試的url
? ? ? ? String uri =bundle.getString ("getCookies.uri");
? ? ? ? String testUrl =this.url + uri;
? ? ? ? // 測(cè)試邏輯代碼書寫
? ? ? ? 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;
? ? ? ? //聲明一個(gè)get的方法
? ? ? ? HttpGet get =new HttpGet(testUrl);
? ? ? ? //聲明一個(gè)client對(duì)象缭贡,用來進(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;
? ? //用來存儲(chǔ)cookie信息的變量
? ? private CookieStorestore ;
? ? @BeforeTest
? ? public void beforeTest(){
//會(huì)自動(dòng)找到resource中application文件
? ? ? ? bundle = ResourceBundle.getBundle("application", Locale.CHINA);
? ? ? ? url =bundle.getString("test.url");
? ? }
//獲取cookies信息
? ? @Test
? ? public void testGetCookies()throws IOException {
String result;
? ? ? ? //從配置文件中 拼接測(cè)試的url
? ? ? ? String uri =bundle.getString ("getCookies.uri");
? ? ? ? String testUrl =this.url + uri;
? ? ? ? // 測(cè)試邏輯代碼書寫
? ? ? ? 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");
? ? ? ? //拼接最終的測(cè)試地址
? ? ? ? String testUrl =this.url + uri;
? ? ? ? //聲明一個(gè)Client對(duì)象坑资,用來進(jìn)行方法的執(zhí)行
? ? ? ? CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(this.store).build();
? ? ? ? //聲明一個(gè)方法,這個(gè)方法就是post方法
? ? ? ? HttpPost post =new HttpPost(testUrl);
? ? ? ? //添加參數(shù)穆端;這個(gè)也可以優(yōu)化袱贮,用例里面很多參數(shù)
? ? ? ? JSONObject param =new JSONObject();
? ? ? ? param.put("name", "jane");
? ? ? ? param.put("age", "18");
? ? ? ? //設(shè)置請(qǐng)求頭信息 設(shè)置header信息;若公用的,可以優(yōu)化到一個(gè)header工具類方法中
? ? ? ? post.setHeader("content-type", "application/json");
? ? ? ? // 將參數(shù)信息添加到方法中
? ? ? ? StringEntity entity =new StringEntity(param.toString(), "utf-8");
? ? ? ? post.setEntity(entity);
? ? ? ? //聲明一個(gè)對(duì)象來進(jìn)行響應(yīng)結(jié)果的存儲(chǔ)
? ? ? ? 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對(duì)象
? ? ? ? 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);
? ? }
}