java httpclient maven,Java + maven + httpclient + testng + poi实现接口自动化

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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在进行接口自动化测试时,我们通常需要对接口进行请求并获取返回结果。而获取返回结果的方式一般有两种,一种是通过 HTTP 响应获取,另一种是通过解析 JSON 格式的响应体获取。 对于第一种方式,我们可以利用 Java 提供的 HttpURLConnection 类或者 Apache 的 HttpClient 库进行操作。而对于第二种方式,我们可以使用 JSON 解析库来解析响应体。 下面介绍通过 HttpURLConnection 获取响应体的方法: ```java import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class HttpUtil { public static String doGet(String urlStr) { try { URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; StringBuilder result = new StringBuilder(); while ((line = in.readLine()) != null) { result.append(line); } in.close(); return result.toString(); } catch (Exception e) { e.printStackTrace(); } return null; } } ``` 上面的代码中,我们使用了 Java 提供的 HttpURLConnection 类来发送 GET 请求,并获取响应体。其中,URL 用于指定请求的地址,HttpURLConnection 用于建立与指定 URL 的连接,并发送请求。通过 BufferedReader 来读取响应体,并将其转换为字符串返回。 使用该方法可以轻松地获取到接口返回的响应体。接下来,我们可以通过 JSON 解析库来解析响应体,从而获取接口返回的具体数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值