HTTP POST接口带参数的HttpClient请求方法和调用

接口自动化测试,今天遇到POST接口带参数,参数在url上,发现原来的工具类中没有该方法,重新调试加上。

 doPost方法如下:

    /**
     * 发送POST请求,带请求参数
     * @param url 请求地址
     * @param headers 请求头
     * @param params 请求params
     * @return getHttpClientResult getHttpClientResult方法
     */

    //public static String doPost(String url, JSONObject json,String token) {
    public  HttpClientResult doPost(String url, Map<String, String> headers,Map<String,String> params){
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse httpResponse=null;
        HttpPost httpPost=null;
        HttpClientResult httpClientResult=null;
        //String result="";
        try {
            //1.创建httpClient对象
            //httpClient = HttpClients.createDefault();
            httpClient =createHttpsClient(filePath,passWord);
            // 创建访问的地址
            URIBuilder uriBuilder = new URIBuilder(url);
            if (params != null) {
                Set<Map.Entry<String, String>> entrySet = params.entrySet();
                for (Map.Entry<String, String> entry : entrySet) {
                    uriBuilder.setParameter(entry.getKey(), entry.getValue());
                }
            }
            //2.创建httpPost对象
            httpPost = new HttpPost(uriBuilder.build());
            //httpPost = new HttpPost(url);
            //2.1对象设置请求头
            //httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
            //httpPost.setHeader("Authorization",token);
            packageHeader(headers, httpPost);
            //2.2封装请求参数
            //packageParam(params, httpPost);

            //2.3对象设置请求和传输超时时间
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(SOCKET_TIME_OUT).setConnectTimeout(CONNECT_TIME_OUT).build();
            httpPost.setConfig(requestConfig);
            //3.使用httpClient发起请求并响应获取response
            /*
            httpResponse = httpClient.execute(httpPost);
            HttpEntity entity = httpResponse.getEntity();
            //4.解析响应,获取数据
            //判断状态码是否是200
            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result = EntityUtils.toString(entity,ENCODING);
                //System.out.println(result);
            }
            //System.out.println(httpResponse.getStatusLine());
            //System.out.println(httpResponse.getStatusLine().getStatusCode());
            //System.out.println(httpResponse.getProtocolVersion());
            //System.out.println(HttpStatus.SC_OK);
            */
            httpClientResult=getHttpClientResult(httpResponse,httpClient,httpPost);
        }
        catch (Exception e){
            log.error(e.toString());
        }
        finally {
            //5.释放资源
            release(httpResponse, httpClient);

        }
        return httpClientResult;
    }

调用:

        String appId="123456789";
        Map<String, String> headers=new HashMap<>();
        headers.put("Content-Type","application/json;charset=UTF-8");
        headers.put("Authorization","Bearer "+token);

    @Test
    public void c06_pluginDeployment() {
        for (ApiDataBean data : listExcel) {
            if (data.getApiName().trim().equals("pluginDeployment")) {
                HttpsClientUtils httpUtils = new HttpsClientUtils();
                String host=BaseRequest.getHost();//这里例如:https://IP
                String reqUrl = host + data.getUrl();   //https://IP+接口url
                System.out.println(reqUrl);
                HttpClientResult httpResult= null;
                try {
                    Map<String, String> params=new HashMap<>();
                    params.put("appId",appId);
                    System.out.println(appId);
                    httpResult = httpUtils.doPost(reqUrl, headers,params);
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error(String.valueOf(e));
                }

                String resultContent = httpResult.getContent();
                //int code = JsonPath.read(resultContent, "$.code");
                log.info(resultContent);
            }
        }
    }

参考:

[Java 接口自动化框架]httpclient4.5.3(CloseableHttpClient) https的工具类HttpsClientUtils

猜你喜欢

转载自blog.csdn.net/fen_fen/article/details/130771616