接口测试:动态获取TOKEN、POST方式传输参数

上级给的学习任务:动态的获取Token,并用Token与给定的参数以Post的方式来测试接口。

在网上也找了部分资料,可能会出现部分代码重合哈哈哈哈哈哈ORZ。

主程序类:

public static String sendRequest(String urlParam,String params) {
        URLConnection con = null;
        //从字符流到字节流的桥接
        OutputStreamWriter out = null;
        BufferedReader buffer = null;
        StringBuffer resultBuffer = new StringBuffer();

        try{
            //获取token
            String result = getToken("此处输入获取Token的网站");
            JSONObject json = new JSONObject(result);
            //类型转换
            String token = (String) json.get("accessToken");
            System.out.println(token);

            URL url =new URL(urlParam);
            con = url.openConnection();
            //请求头
            con.setRequestProperty("elsAccount","输入给定的参数,没有给定可以不写");
            con.setRequestProperty("accessToken", token);
            con.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            //允许写出
            con.setDoOutput(true);
            //允许读入
            con.setDoInput(true);
            //不使用缓存
            con.setUseCaches(false);
            //获取URLConnection对象对应的输出流并开始发送数据
            out = new OutputStreamWriter(con.getOutputStream(), "UTF-8");
            //添加参数
            out.write(params);
            out.flush();

            buffer = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
            String line;

            while ((line = buffer.readLine()) != null) {
                //将字符串追加到此字符序列
                resultBuffer.append(line);
            }
            //返回此序列中数据的字符串表示形式
            return resultBuffer.toString();
            //con.connect();
        }catch(Exception e) {
            e.printStackTrace();
        }finally {    // 使用finally块来关闭输出流、输入流
            try {
                if (out != null) {
                    out.close();
                }
                if (buffer != null) {
                    buffer.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        return "";
    }

获取Token的函数:getToken:

public static String getToken(String urlParam) {

        URLConnection con = null;

        BufferedReader buffer = null;
        StringBuffer resultBuffer = null;

        try {
            URL url = new URL(urlParam);
            con = url.openConnection();

            //设置请求需要返回的数据类型和字符集类型
            con.setRequestProperty("Content-Type", "application/json;charset=GBK");
            //允许写出
            con.setDoOutput(true);
            //允许读入
            con.setDoInput(true);
            //不使用缓存
            con.setUseCaches(false);
            //得到响应流
            InputStream inputStream = con.getInputStream();
            //将响应流转换成字符串
            resultBuffer = new StringBuffer();
            String line;
            buffer = new BufferedReader(new InputStreamReader(inputStream, "GBK"));
            while ((line = buffer.readLine()) != null) {
                resultBuffer.append(line);
            }
            return resultBuffer.toString();

        }catch(Exception e) {
            e.printStackTrace();
        }

        return "";
    }

主函数。此处有两种参数:

1.普通的JSONObject。

2带有数组的JSONObject。

public static void main(String[] args) throws JSONException {
        String url ="此处输入所需要的测试链接";
        //普通的JsonObject,此处按照自己的参数来用JSON格式输入
        String params = "{\"account\":\"XXX\",\"code\":\"YYY\",\"type\":\"json\",\"data\":{\"header\":{\"elsAccount\":\"123\",\"pwd\":\"123123\",\"subAccount\":\"App_open_test\"},\"body\":{\"data\":{\"accessNumber\":\"1234567\",\"auditStatus\":\"3\"},\"interfaceCode\":\"statusFor\"}}}";
        //带JsonArray的JsonObject,此处输入的是带数组的JSON数据
        String bo = "{\"account\": \"XXX\",\"code\": \"YYY\",\"type\": \"json\",\"data\":{\"header\": {\"elsAccount\": \"123\",\"pwd\": \"123123\",\"subAccount\": \"App_open_test\"},\"body\":{\"data\": {\"headList\":[{\"taxAmount\": \"111\",\"deductInstruction\": \"测试接口\",\"supplierCode\":\"101\",\"deductReason\":\"2\"}]},\"interfaceCode\": \"11111\"}}}";

        
        String result = sendRequest(url, params);
        System.out.println(result);

    }

如果不想用JSON格式输入参数也能按照下面的方法来入参。

public static void main(String[] args) throws JSONException {
        String url ="输入想要测试的链接";
        //jsonArray是为了输入带数组类型的JSON,没有的话可以不用写。此处我假设有。
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject2 = new JSONObject();

        //入参
        jsonObject2.put("taxAmount","11111");
        jsonObject2.put("deductInstruction","测试接口");
        //封装成JSONArray格式
        jsonArray.put(jsonObject2);

        jsonObject.put("data",jsonArray);
        jsonObject.put("account","123");
        jsonObject.put("code","createDeduct");
        jsonObject.put("type","json");

        String JO = jsonObject.toString();

        String result = sendRequest(url, JO);
        System.out.println(result);

    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值