httpclient灏辨槸妯℃嫙http鍗忚锛屽鎴风鐨勪竴绉嶆妧鏈紝get銆乸ost锛宧ttp://hc.apache.org/聽 瀹樻柟缃戠珯銆傚湪pom鏂囦欢涓紩鍏ヤ緷璧栧寘
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1.2</version>
</dependency>
绗竴涓猟emo聽
public class MyHttpClient {
@Test
public void test1() throws IOException {
//鐢ㄦ潵瀛樻斁鎴戜滑鐨勭粨鏋? String result;
HttpGet get = new HttpGet("http://www.baidu.com");
//杩欎釜鏄敤鏉ユ墽琛実et鏂规硶鐨? HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(get);
result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);
}
}
甯ookie鐨刧et璇锋眰
public class MyCookiesForGet {
private String url;
/**java util鍖呴噷鑷甫鐨勬柟娉?bundle*/
private ResourceBundle bundle;
//鐢ㄦ潵瀛樺偍cookies淇℃伅鐨勫彉閲? private CookieStore store;
@BeforeTest
public void beforeTest(){
/**榛樿璇嗗埆properties鏂囦欢,鐩存帴鍐檃pplication鍚嶅氨鍙互锛屽繀椤诲湪resources鐩綍涓嬶紝缂栫爜鏍煎紡*/
bundle = ResourceBundle.getBundle("application", Locale.CHINA);
/**鍐欓厤缃枃浠堕噷鐨刱ey锛屼細鍙栦粬鐨勫€?/
url = bundle.getString("test.url");
}
@Test
public void testGetCookies() throws IOException {
String result;
/**浠庨厤缃枃浠朵腑鑾峰彇uri*/
String uri = bundle.getString("getCookies.uri");
/**鎷兼帴*/
String testUrl = this.url+uri;
// 娴嬭瘯閫昏緫浠g爜涔﹀啓
HttpGet get = new HttpGet(testUrl);
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(get);
result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(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 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);
}
}
}
甯ookie鐨刾ost璇锋眰
public class MyCookiesForPost {
private String url;
private ResourceBundle bundle;
//鐢ㄦ潵瀛樺偍cookies淇℃伅鐨勫彉閲? private CookieStore store;
@BeforeTest
public void beforeTest(){
bundle = ResourceBundle.getBundle("application", Locale.CHINA);
url = bundle.getString("test.url");
}
@Test
public void testGetCookies() throws IOException {
String result;
// 浠庨厤缃枃浠朵腑 鎷兼帴娴嬭瘯鐨剈rl
String uri = bundle.getString("getCookies.uri");
String testUrl = this.url+uri;
// 娴嬭瘯閫昏緫浠g爜涔﹀啓
HttpGet get = new HttpGet(testUrl);
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(get);
result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(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 testPostMethod() throws IOException {
String uri = bundle.getString("test.post.with.cookies");
//鎷兼帴鏈€缁堢殑娴嬭瘯鍦板潃
String testUrl = this.url + uri;
//澹版槑涓€涓狢lient瀵硅薄锛岀敤鏉ヨ繘琛屾柟娉曠殑鎵ц
DefaultHttpClient client = new DefaultHttpClient();
//澹版槑涓€涓柟娉曪紝杩欎釜鏂规硶灏辨槸post鏂规硶
HttpPost post = new HttpPost(testUrl);
//娣诲姞鍙傛暟
JSONObject param = new JSONObject();
param.put("name","xiaoqiang");
param.put("age","18");
//璁剧疆璇锋眰澶翠俊鎭?璁剧疆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);
//澶勭悊缁撴灉锛屽氨鏄垽鏂繑鍥炵粨鏋滄槸鍚︾鍚堥鏈? //灏嗚繑鍥炵殑鍝嶅簲缁撴灉瀛楃涓茶浆鍖栨垚涓簀son瀵硅薄
JSONObject resultJson = new JSONObject(result);
//鑾峰彇鍒扮粨鏋滃€? String success = (String) resultJson.get("xiaoqiang");
String status = (String) resultJson.get("status");
//鍏蜂綋鐨勫垽鏂繑鍥炵粨鏋滅殑鍊? Assert.assertEquals("success",success);
Assert.assertEquals("1",status);
}
}
鐢ㄤ箣鍓峬oco妗嗘灦mock鍑鸿繖鍑犱釜鎺ュ彛
[
{
"description":"杩欐槸涓€涓細杩斿洖cookies淇℃伅鐨刧et璇锋眰",
"request":{
"uri":"/getCookies",
"method":"get"
},
"response":{
"cookies":{
"login":"true"
},
"text":"鎭枩浣犺幏寰梒ookies淇℃伅鎴愬姛",
"headers":{
"Content-Type":"text/html;charset=gbk"
}
}
},
{
"description":"杩欐槸涓€涓甫cookies淇℃伅鐨刧et璇锋眰",
"request":{
"uri":"/get/with/cookies",
"method":"get",
"cookies":{
"login":"true"
}
},
"response":{
"text":"杩欐槸涓€涓渶瑕佹惡甯ookies淇℃伅鎵嶈兘璁块棶鐨刧et璇锋眰",
"headers":{
"Content-Type":"text/html;charset=gbk"
}
}
},
{
"description":"杩欐槸涓€涓甫cookies淇℃伅鐨刾ost璇锋眰",
"request":{
"uri":"/post/with/cookies",
"method":"post",
"cookies":{
"login":"true"
},
"json":{
"name":"xiaoqiang",
"age":"18"
}
},
"response":{
"status":200,
"json":{
"xiaoqiang":"success",
"status":"1"
},
"headers":{
"Content-Type":"text/html;charset=gbk"
}
}
}
]
鍚姩杩欎釜mock搴旂敤锛岀劧鍚庤繍琛宼est鏂规硶锛屽氨鍙互姝e父杩斿洖淇℃伅銆?