packagecom.lemon;importjava.io.UnsupportedEncodingException;import java.util.*;importjava.util.regex.Matcher;importjava.util.regex.Pattern;importorg.apache.http.client.entity.UrlEncodedFormEntity;importorg.apache.http.client.methods.CloseableHttpResponse;importorg.apache.http.client.methods.HttpGet;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.client.utils.URLEncodedUtils;importorg.apache.http.impl.client.CloseableHttpClient;importorg.apache.http.impl.client.HttpClients;importorg.apache.http.message.BasicNameValuePair;importorg.apache.http.util.EntityUtils;importorg.testng.Assert;importorg.testng.annotations.DataProvider;importorg.testng.annotations.Test;public classDemo {
@Test(dataProvider="datas")public static void test(String url,String mobileCode,String userID,String type,String response) throwsException {
System.out.println("url:"+url+",mobileCode:"+mobileCode+",userID:"+userID+",type:"+type);if("post".equalsIgnoreCase(type)){
String resp=doPost(url,mobileCode,userID);
Assert.assertEquals(resp, response);
}else{
String resp=doGet(url,mobileCode,userID);
Assert.assertEquals(resp, response);
}
}
@DataProviderpublic staticObject [][] datas(){/*Object [][] datas = {
{"http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo","15578581","","post"},
{"http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo","18381485","","get"},
{"http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo","15084258","","post"}
};*/Object[][] datas= ExcelUtil.read(2, 5, 2, 6);returndatas;
}/** 实现get类型接口的调用*/
private static String doGet(String url,String mobileCode,String userID) throwsException {//准备参数
List params = new ArrayList();
BasicNameValuePair mobile= new BasicNameValuePair("mobileCode",mobileCode);
BasicNameValuePair ID= new BasicNameValuePair("userID",userID);
params.add(mobile);
params.add(ID);
String paramsString= URLEncodedUtils.format(params, "UTF-8");
url+= "?" +paramsString;//创建get对象
HttpGet get = newHttpGet(url);//创建Httpclient对象
CloseableHttpClient httpclient =HttpClients.createDefault();//提交请求
CloseableHttpResponse response = null;try{
response=httpclient.execute(get);//获取状态码及响应数据
int status =response.getStatusLine().getStatusCode();
System.out.println("状态码为:" +status);
String result=EntityUtils.toString(response.getEntity());
System.out.println("响应数据为:" +result);//创建Pattern对象
Pattern pat = Pattern.compile(">(.*)");//创建matcher对象
Matcher m =pat.matcher(result);if(m.find( )){return m.group(1);
}
}catch(Exception e) {
e.printStackTrace();
}finally{if (response != null) {
response.close();
}//相当于关闭浏览器
httpclient.close();
}return null;
}/** 实现post类型接口的调用*/
private static String doPost(String url,String mobileCode,String userID) throwsException {//创建post对象
HttpPost post = newHttpPost(url);//准备参数
List params = new ArrayList();
BasicNameValuePair mobile= new BasicNameValuePair("mobileCode",mobileCode);
BasicNameValuePair ID= new BasicNameValuePair("userID",userID);
params.add(mobile);
params.add(ID);//将参数封装到请求体当中
post.setEntity(newUrlEncodedFormEntity(params));//创建httpclient对象发送请求
CloseableHttpClient httpclient =HttpClients.createDefault();
CloseableHttpResponse response= null;try{
response=httpclient.execute(post);//获取状态码及响应数据
int status =response.getStatusLine().getStatusCode();
System.out.println("状态码为:" +status);
String result=EntityUtils.toString(response.getEntity());
System.out.println("响应数据为:" +result);//创建 Pattern对象
Pattern pat = Pattern.compile(">(.*)");//现在创建 matcher对象
Matcher m =pat.matcher(result);if(m.find( )) {return m.group(1);
}
}catch(UnsupportedEncodingException e) {
e.printStackTrace();
}finally{if (response != null) {
response.close();
}//相当于关闭浏览器
httpclient.close();
}return null;
}
}