目錄
一,get實戰
第一節 HttpClient簡介及第一個demo
1.模擬get及post請求
2.引用httpCllient
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1.2</version>
</dependency>
3.demo
public class MyHttpClient {
@Test
public void test1(){
try {
//放置響應結果
String result ;
//get方法-請求http://www.baidu.com
HttpGet get = new HttpGet("http://www.baidu.com");
//獲取DefaultHttpClient請求
HttpClient client = HttpClientBuilder.create().build();
//執行get方法
HttpResponse response = client.execute(get);
//拿到響應結果的實體entity
result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}
}
}
執行結果:
第2節 mock一個返回cookie信息的請求
1.response中帶有cookie信息的get請求
[
{
"description": "這是一個沒有請求參數的get方法",
"request": {
"uri": "/getdemo",
"method": "get",
"cookies":{
"login":"true"
}
},
"response": {
"text": "發送cookie信息成功"
}
},
{
"description": "這是一個會返回cookie信息的get請求",
"request": {
"uri": "/getCookies",
"method": "get"
},
"response": {
"text":"get cookies success",
"cookies":{
"login":"true",
"status":"1000"
}
}
}
]
執行後
第3節 配置優化方法
代碼
package com.course.httpClient.cookies;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.io.IOException;
import java.util.Locale;
import java.util.ResourceBundle;
public class MyCookieForGet {
//優化http請求--調用請求方法的配置化--詳見application.properties文件
//讀取配置文件
private String url ;
private ResourceBundle bundle ;
@BeforeTest
public void beforeTest() throws Exception{
//自動獲取resources路徑下的後綴名爲properties的文件名爲“application“的文件(相對路徑)
//字符編碼 Locale.CHINA
bundle = ResourceBundle.getBundle("application", Locale.CHINESE);
url = bundle.getString("test.url");
}
@Test
public void test() throws IOException {
String result;
//從配置文件中拼接url
String uri = bundle.getString("getCookies.uri");
String testUrl = this.url+uri;
HttpGet get = new HttpGet(testUrl);
//獲取DefaultHttpClient請求
HttpClient client = HttpClientBuilder.create().build();
//執行get方法
HttpResponse response = client.execute(get);
//拿到響應結果的實體entity
result = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println("result "+result);
}
}
配置文件:
test.url=http://localhost:8888
getCookies.uri=/getCookies
getDemo.uri=/getdemo
執行方法後得到如上圖示結果
第4節 獲取cookies信息
//用來存儲cookies信息的變量
private CookieStore store;
@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請求
DefaultHttpClient client = new DefaultHttpClient();
//執行get方法
HttpResponse response = client.execute(get);
//拿到響應結果的實體entity
result = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println("result "+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);
}
}
第5節 攜帶cookies信息訪問get請求
@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);
}
}
執行結果:
在學習過程中遇到的問題:
在讀取配置文件的時候總是提示:
java.util.MissingResourceException: Can’t find bundle for base name application, locale zh_CN
搜索了一番都是說路徑不對要寫絕對路徑,但是試了也不行,新建了一個項目後,配置一樣路徑一樣再次測試就成功了,不知道之前是由於什麼原因導致的,目前還沒有解決
二,post實戰
第6節 post方法的訪問實戰
1.post方法mock
[
{
"description": "帶有cookies的post請求",
"request": {
"uri": "/post/withCookie",
"method": "post",
"cookies": {
"start": "end"
},
"json": {
"name": "ella",
"age": "18"
}
},
"response": {
"json": {
"name": "ella",
"age": "18",
"code": "success"
}
}
},
{
"description": "這是一個返回cookie信息的get方法",
"request": {
"uri": "/get/returnCookie",
"method": "get"
},
"response": {
"text": "return cookie success",
"cookies": {
"start": "end"
}
}
}
]
2.訪問post方法步驟詳解(在不知道怎麼着手寫代碼的時候可以通過這種方式把每一步要做什麼都寫下來)
獲取訪問地址:從配置文件中獲取並拼接完整地址
聲明一個client對象,用來進行方法的執行
聲明一個方法(post方法)
添加入參
設置請求頭信息(json格式–contentType)
將入參添加到方法中
聲明一個對象用於存儲相應結果
設置cookies信息
執行post方法
獲取響應結果
處理結果,斷言,驗證結果是否符合預期
3.代碼
package com.course.cookies;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
public class MyCookieForPost {
//優化http請求--調用請求方法的配置化--詳見application.properties文件
//讀取配置文件
private String url;
private ResourceBundle bundle;
//用來存儲cookies信息的變量
private CookieStore store;
@BeforeTest
public void beforeTest() {
//自動獲取resources路徑下的後綴名爲properties的文件名爲“application“的文件(相對路徑)
//字符編碼 Locale.CHINA
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請求
DefaultHttpClient client = new DefaultHttpClient();
//執行get方法
HttpResponse response = client.execute(get);
//拿到響應結果的實體entity
result = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println("result: " + 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 testPostWithCookie() throws IOException {
//獲取訪問地址:從配置文件中獲取並拼接完整地址
String uri = bundle.getString("test.post.with.cookies");
String testUrl = this.url + uri;
//聲明一個client對象,用來進行方法的執行
DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
//聲明一個方法(post方法)
HttpPost post = new HttpPost(testUrl);
//添加入參
JSONObject param = new JSONObject();
param.put("name", "ella");
param.put("age", "18");
//設置請求頭信息(json格式--contentType)
post.setHeader("content-type", "application/json");
//將入參添加到方法中
StringEntity entity = new StringEntity(param.toString(), "utf-8");
post.setEntity(entity);
//聲明一個對象用於存儲相應結果
String result;
//設置cookies信息
defaultHttpClient.setCookieStore(this.store);
//執行post方法
HttpResponse response = defaultHttpClient.execute(post);
//獲取響應結果
result = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println(result);
//處理結果,斷言,驗證結果是否符合預期
//將返回的響應結果字符串轉化爲json對象
JSONObject object = new JSONObject(result);
String name = (String) object.getString("code");
Assert.assertEquals("success", name);
}
}
執行結果:
注意:沒有json對象要先在pom文件引入json對象;要在properties文件中配置post方法的uri路徑
可以優化點:
1.獲取訪問的url
2.請求頭信息優化
3.入參添加優化
優化後代碼:
package com.course.cookies;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
public class MyCookieForPost {
//優化http請求--調用請求方法的配置化--詳見application.properties文件
//讀取配置文件
private String url;
private ResourceBundle bundle;
//用來存儲cookies信息的變量
private CookieStore store;
@BeforeTest
public void beforeTest() {
//自動獲取resources路徑下的後綴名爲properties的文件名爲“application“的文件(相對路徑)
//字符編碼 Locale.CHINA
}
@Test
public void testGetCookies() throws IOException {
String result;
//從配置文件中拼接url
String envUrl = "test.url";
String uri = "getCookies.uri";
String testUrl = getUrl(envUrl, uri);
//獲取DefaultHttpClient請求
DefaultHttpClient client = new DefaultHttpClient();
//執行get方法
HttpGet get = new HttpGet(testUrl);
HttpResponse response = client.execute(get);
//拿到響應結果的實體entity
result = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println("result: " + 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);
}
}
public String getUrl(String envUrl, String uriPath) {
bundle = ResourceBundle.getBundle("application", Locale.CHINA);
url = bundle.getString(envUrl);
String uri = bundle.getString(uriPath);
String testUrl = this.url + uri;
return testUrl;
}
public StringEntity addParamWithJson() {
StringEntity entity = null;
try {
JSONObject param = new JSONObject();
param.put("name", "ella");
param.put("age", "18");
entity = new StringEntity(param.toString(), "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return entity;
}
@Test(dependsOnMethods = {"testGetCookies"})
public void testPostWithCookie() throws IOException {
//獲取訪問地址:從配置文件中獲取並拼接完整地址
String envUrl = "test.url";
String uriPath = "test.post.with.cookies";
String testUrl = getUrl(envUrl, uriPath);
//聲明一個client對象,用來進行方法的執行
DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
//聲明一個方法(post方法)
HttpPost post = new HttpPost(testUrl);
//設置請求頭信息(json格式--contentType)
post.setHeader("content-type", "application/json");
//添加入參
//將入參添加到方法中
StringEntity entity = addParamWithJson();
post.setEntity(entity);
//設置cookies信息
defaultHttpClient.setCookieStore(this.store);
//執行post方法
HttpResponse response = defaultHttpClient.execute(post);
//聲明一個對象用於存儲相應結果
String result;
//獲取響應結果
result = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println(result);
//處理結果,斷言,驗證結果是否符合預期
//將返回的響應結果字符串轉化爲json對象
JSONObject object = new JSONObject(result);
String name = object.getString("code");
Assert.assertEquals("success", name);
}
}