接口自動化HttpClient-基礎篇1
基於java+httpclient的接口自動化測試。
一.環境配置
下載+安裝 (Java環境+httpclient包)
httpclient包下載地址:https://hc.apache.org/downloads.cgi
1.Java工程中添加libs並將httpclient的jar全部導入進來
2.在Libraries裏面添加如下三個jar包
二.基本方法使用
//創建HttpClient對象
CloseableHttpClient httpclient = HttpClients.createDefault();
// 創建帶請求地址的HttpGet對象
HttpGet httpGet = new HttpGet("http://
//設置HttpGet對象屬性,如header,cookie等(根據實際情況)
httpget.setHeader("Content-Type","application-json");
// 執行httpGet請求
httpclient.execute(httpget);
//斷開連接
httpclient.close();
================
//獲取get請求的響應
CloseableHttpResponse response = httpclient.execute(httpget);
// 獲取相應狀態碼
response.getStatusLine();
// 獲取響應實體
HttpEntity entity = response.getEntity();
// 獲取響應內容
EntityUtils.toString(entity, "UTF-8"));
//釋放資源
EntityUtils.consume(entity);
三.百度實例demo
接口自動化HttpClient-基礎篇1