1、Apache开源框架HttpClient的使用,官网:http://hc.apache.org/
2、DefaultHttpClient过时,新的HttpClient对象的创建方式
旧:HttpClient client = new DefaultHttpClient();
新:HttpClient client = HttpClientBuilder.create().build();
3、HttpClient Get方法的实现
3.1、Moco返回一个Cookies信息的请求
[
{
“description”: “这是一个反馈cookie信息的get请求”,
“request”: {
“uri”: “/getCookies”,
“method”: “get”
},
“response”: {
“headers”: {
“Content-Type”: “text/html;chartset:utf8”
},
“cookies”: {
“login”:“true”
},
“text”: “获得cookie成功”
}
}
]
3.1.1 moco模拟get接口
[
{
“description”: “这是一个反馈cookie信息的get请求”,
“request”: {
“uri”: “/getCookies”,
“method”: “get”
},
“response”: {
“cookies”: {
“login”:“true”
},
“text”: “响应成功”
}
},
{
“description”: “这是一个带Cookie信息的get请求”,
“request”: {
“uri”: “/getWithCookies”,
“method”: “get”,
“cookies”: {
“login”: “true”
}
},
“response”: {
“headers”: {
“Content-Type”: “text/html;chartset:utf8”
},
“status”: “200”,
“text”: “这是一个带cookie信息的get请求”
}
}
]
3.1.2 moco模拟post接口
[
{
“description”:“不带参数的Post请求”,
“request”: {
“uri”: “/getCookie”,
“method”: “get”
},
“response”:{
“cookies”: {
“login”: “true”
},
“text”: “获取cookie成功”
}
},
{
“description”:“不带参数的Post请求”,
“request”: {
“uri”: “/post/no/param”,
“method”: “post”
},
“response”:{
“text”: “sucess”
}
},
{
“description”:“带参数的Post请求”,
“request”: {
“uri”: “/post/with/Param”,
“method”: “post”,
“cookies”:{
“login”:“true”
},
“forms”:{
“loginname”: “post”,
“password”: “123456”
}
},
“response”:{
“status”:200,
“json”:{
“post”:“success”,
“status”:“1”
}
}
},
{
“description”: “带Cookies信息的Post请求”,
“request”: {
“uri”: “/postWithCookie”,
“method”: “post”,
“cookies”: {
“login”: “true”
},
“json”: {
“username”: “post”,
“password”: “123456”
}
},
“response”: {
"json": {
"status": "200",
"result": "sucess"
}
}
}
]
3.2、get请求获取Cookie及响应结果
package com.httpclient.cookies;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.io.IOException;
import java.util.List;
import java.util.ResourceBundle;
public class CookiesForGet {
private String RequestURL ;
private ResourceBundle bundle;
private CookieStore store;
@BeforeTest
public String getRequesturl(){
//获取配置文件
bundle = ResourceBundle.getBundle("application");
RequestURL = bundle.getString("test_uri") + bundle.getString("test_method_getCookies");
return RequestURL;
}
@Test
public void getCookie() throws IOException {
//创建get请求
HttpGet httpGet = new HttpGet(RequestURL) ;
//创建httpclient对象
DefaultHttpClient client = new DefaultHttpClient();
//httpclient对象执行get请求
HttpResponse response = client.execute(httpGet);
//等到响应对象结果
HttpEntity entity = response.getEntity();
//响应结果转换为字符串
String result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);
CookieStore store = client.getCookieStore();
List<Cookie> cookies = store.getCookies();
//循环遍历打印Cookies信息
for(Cookie cookie:cookies){
System.out.println("cookiename: " + cookie.getName() + ";"
+ "cookieValue: " + cookie.getValue()
);
}
}
@Test
public void getWithCookie() throws IOException {
//
HttpGet get = new HttpGet(RequestURL);
DefaultHttpClient client = new DefaultHttpClient();
client.setCookieStore(this.store);
HttpResponse response = client.execute(get);
String result =EntityUtils.toString(response.getEntity(),"utf8");
int stateNum = response.getStatusLine().getStatusCode();
System.out.println("result: "+result+"; 状态码 :"+ stateNum);
}
}
3.3 获取Cookie及使用cookie发送post请求
package com.httpclient.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.JSONException;
import org.json.JSONObject;
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 CookiesForPost {
public String requestUrl;
public String uri1;
public ResourceBundle bundle;
public CookieStore store;
public String responseRes;
@BeforeTest
public void LoadProperties(){
bundle = ResourceBundle.getBundle("application", Locale.CHINA);
uri1 = bundle.getString("test_uri") ;
}
@Test//不带Cookie不带param信息的Post的请求
public void PostNoCookie() throws IOException {
String uri2 = bundle.getString("test_postnoParam");
requestUrl = uri1 +uri2;
System.out.println(requestUrl);
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(requestUrl);
HttpResponse response = client.execute(post);
String responseRes = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(responseRes);
}
@Test
//获取Cookie
public void getCookie() throws IOException, JSONException {
String uri2 = bundle.getString("test_getCookie");
requestUrl = uri1 + uri2;
HttpGet get = new HttpGet(requestUrl);
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(get);
store = client.getCookieStore();
List<Cookie> list = store.getCookies();
for(Cookie cookie:list){
System.out.println("cookiename "+ cookie.getName());
}
}
@Test(dependsOnMethods = {"getCookie"})
//带Param带Cookie的Post请求
public void postWithCookie() throws JSONException, IOException {
String uri3 = bundle.getString("test_postWithCookie");
requestUrl = uri1 + uri3;
HttpPost post = new HttpPost(requestUrl);
DefaultHttpClient client = new DefaultHttpClient();
//添加参数
JSONObject param = new JSONObject();
param.put("username","post");
param.put("password","123456");
//设置请求头信息 设置header
post.setHeader("content-type","application/json");
//将参数信息添加到方法中
StringEntity entity = new StringEntity(param.toString(),"utf-8");
post.setEntity(entity);
//设置cookies信息
client.setCookieStore(this.store);
//执行post方法
HttpResponse response = client.execute(post);
//声明对象 存储响应结果
String result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println("responseRes "+ result);
//将返回的响应结果字符串转化成为json对象
JSONObject resultJson = new JSONObject(result);
String success = (String) resultJson.get("status");
String status = (String) resultJson.get("result");
System.out.println( success + status);
}
}