接口自动化-testNG+httpclient(实例)

一、导入的坐标(pom.xml文件)

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>

二、HttpUtils(封装的HTTP工具类)

下面以json参数类型的post请求为例:

public static String jsonPost(String url,String params,boolean isToken) throws Exception{
    //1、创建request(请求)
    HttpPost post = new HttpPost(url);
    //2、添加请求头
    post.setHeader(Constants.HEADER_NAME, Constants.HEADER_VALUE);
    //2.1判断是否需要鉴权		
    if(isToken){
        VariableUtils.getToken(post);
    }
    //3、添加入参
    post.setEntity(new StringEntity(params, "UTF-8"));
    //4、发送接口
    CloseableHttpClient client = HttpClients.createDefault();
    //5、接收响应结果
    CloseableHttpResponse response = client.execute(post);
    //6、格式化响应结果
    HttpEntity entity = response.getEntity();
    String body = EntityUtils.toString(entity);
    return body;
}    

  

调用的获取鉴权函数(getToken())

public static void getToken(HttpRequest post){
    //1、从map中获取鉴权值
    String token = env.get("access_token");
    //2、判断值不为空,则传入到头参数中
    if(token != null){
        post.setHeader("access_token",token);
    }
}

  

原文地址:https://www.cnblogs.com/x495122903/p/12930231.html