zoukankan      html  css  js  c++  java
  • 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<String, String> cinemas() {
            HashMap<String, String> params = new HashMap<String, String>();
            params.put("method", "getCinemas");
            params.put("uid", StartTest.USERNAME);
            return params;
        }

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

    public static List<NameValuePair> getFormparams (HashMap<String, String> params) {
            List<NameValuePair> formparams = new ArrayList<NameValuePair>();
            for (Entry<String, String> 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<NameValuePair> 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<String, String> 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<datajsonArray.size(); 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<String, String> 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数据获取目标值已完成

  • 相关阅读:
    解决IE新开窗口无法显示内容的问题
    日历控件
    关于索引,我们可以知道的更多 全表扫描和索引扫描
    C#构造函数的小说明
    动态创建的组件的ID设定
    正则表达式使用详解
    “运行”命令全集
    数组型参数
    接口实现的继承机制
    读取XML文件
  • 原文地址:https://www.cnblogs.com/mayibanjiah/p/4210863.html
Copyright © 2011-2022 走看看