利用doPost请求调用第三方平台接口

一、前端传输参数
参数格式如下:

{
            "bk_app_code": "service-desk",
            "bk_app_secret": "2b502835-65f1",
            "bk_username": "BPM",
            "flow_no": "papersheet",
            "action_type": "usb_enable",
            "callback_url": null,
            "action_data": {
                "ad_account": "20021339",
                "hostname": "DW7CNECZ89",
                "is_laptop": "false",
                "usb_group": "03_Print",
                "usb_group_name": "打印",
                "url": "papersheet",
                "start_date": "2021-02-07"  
                           }
        }

利用ajax调用后台接口

     $.ajax({
            url: '${pageContext.request.contextPath}/IT00022Controller/offLineOpen.action',
            type: 'post',
            async : false,
            data: {
                url: "http://test.aact.com:80/api/c/self-service-api/auto_action_test/",
                data: JSON.stringify(data)
            },
            dataType : 'json',
            success : function(result) {
                $.messager.progress('close');
                if(result.result == "true"){
                    alert("开通成功!!!");
                  	resetData();
                }else{
                    alert("开通失败!!!");
                }
            }
        });

二、后端代码

@RequestMapping("/offLineOpen")
    @ResponseBody
    public Map<String,Object> offLineOpen(String url,String data){
        JSONObject jsonObject = JSON.parseObject(data);
        String param = " {\n" +
                "            \"bk_app_code\": \"service-desk\",\n" +
                "            \"bk_app_secret\": \"2b502835-65f1\",\n" +
                "            \"bk_username\": \"BPM\",\n" +
                "            \"flow_no\": \"papersheet\",\n" +
                "            \"action_type\": \"usb_enable\",\n" +
                "            \"callback_url\": null,\n" +
                "            \"action_data\": {\n" +
                "                \"ad_account\": \""+jsonObject.get("ad_account")+"\",\n" +
                "                \"hostname\": \""+jsonObject.get("hostname")+"\",\n" +
                "                \"is_laptop\": \""+jsonObject.get("is_laptop")+"\",\n" +
                "                \"usb_group\": \""+jsonObject.get("usb_group")+"\",\n" +
                "                \"usb_group_name\": \""+jsonObject.get("usb_group_name")+"\",\n" +
                "                \"url\": \"papersheet\",\n" +
                "                \"end_date\": \""+jsonObject.get("end_date")+"\"" +
                "           }\n" +
                "        }";
        String result = it22Service.doPost(url,param);
        JSONObject resultMap = JSON.parseObject(result);
        if(resultMap.getString("result").equals("false")) {
        	jsonObject.put("result",resultMap.getString("result")+";"+resultMap.getString("message"));
        }else {
        	jsonObject.put("result",resultMap.getString("result"));
        }
        iT00022Service.insertOffLineDate(jsonObject);
        return jsonObject;
    }
public String doPost(String strURL, String params) {
        BufferedReader reader = null;
        try {
            URL url = new URL(strURL);// 创建连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestMethod("POST"); // 设置请求方式
            // connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
            connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
            connection.connect();
            //一定要用BufferedReader 来接收响应, 使用字节来接收响应的方法是接收不到内容的
            OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码
            out.append(params);
            out.flush();
            out.close();
            // 读取响应
            reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String line;
            String res = "";
            while ((line = reader.readLine()) != null) {
                res += line;
            }
            reader.close();
            return res;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "error"; // 自定义错误信息
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值