httpclient + TestNG 接口自动测试 第二章

请求地址由参数加参数签名形式生成,例如:

http://ip/server?method=getPlans&planDate=2014-08-25&cinemaId=101&uid=remote&enc=d0fe8420c641dd87d4165c09fe1d0c70&time_stamp=1408960607250

1.构建url

  1)首先构建参数对

public static HashMap cinemas() {
        HashMap params = new HashMap();
        params.put("method", "getCinemas");
        params.put("uid", StartTest.USERNAME);
        return params;
    }

    2)生成包含参数签名的参数对

public static List getFormparams (HashMap params) {
        List formparams = new ArrayList();
        for (Entry e : params.entrySet()) {
            formparams.add(new BasicNameValuePair(e.getKey(), e.getValue()));
        }
        formparams.add(new BasicNameValuePair("time_stamp", String.valueOf(new Date().getTime())));
        formparams.add(new BasicNameValuePair("enc", GetENC.getEnc(formparams, StartTest.PASSWORD)));
        return formparams;
    }

  3)生成请求URL

    public static URI getUrl(List formparams, String HOST, String PATH) {
        URI uri = null;
        try {
            uri = new URIBuilder().setScheme("http").setHost(HOST)
                    .setPath(PATH).setParameters(formparams).build();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return uri;
    }

2.构建完URL后发送请求

public static HttpEntity getEntity(HashMap params, String HOST, String PATH) {
        HttpEntity entity = null;
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            // 创建httpget请求.  
            HttpGet httpget = new HttpGet(getUrl(getFormparams(params), HOST, PATH));
            // 执行get请求 
            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
                // 将响应放入entity载体
                entity = response.getEntity();
                 if (entity != null) {
                        entity = new BufferedHttpEntity(entity);
                        if(StartTest.log) {
                            ResultsLog.writefile(EntityUtils.toString(entity, "UTF-8"), StartTest.path);
                        }
                    }
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 断开连接
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return entity;
    }

3.解析目标JSON数据,返回目标数组

public static String[] targetArray(String sourcejson, String targetfield) {
    
    String targetArray[] = null;
    JSONObject js = JSONObject.fromObject(sourcejson);
    if (sourcejson.contains("data")) {
        JSONArray datajsonArray = js.getJSONArray("data");
        targetArray = new String[datajsonArray.size()];
        for(int i=0; i){
            targetArray[i] = (JSONObject.fromObject(datajsonArray.getString(i))).getString(targetfield);
        }
    }  else {
        targetArray[0] = js.getString("errCode") + "----" + js.getString("errMsg");
    }
    return targetArray;
}

4.执行请求URL并解析返回JSON数据,获取目标值构成数组

    public static String[]  targetValueArray(HashMap params, String targetfield) {
        String targetarray[] = null;
        HttpEntity entity = GetPost.getEntity(params, StartTest.ROUND_HOST,
                StartTest.ROUND_PATH); 
        try {
            targetarray = JSONParsing.targetArray(
                    EntityUtils.toString(entity, "UTF-8"), targetfield);
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return targetarray;
    }

 

至此从构建URL-->发送get请求-->解析JSON数据获取目标值已完成

转载于:https://www.cnblogs.com/mayibanjiah/p/4210863.html

你可能感兴趣的:(httpclient + TestNG 接口自动测试 第二章)