利用doPost请求调用第三方平台接口
程序员文章站
2024-02-08 10:56:34
...
一、前端传输参数
参数格式如下:
{
"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"; // 自定义错误信息
}
上一篇: 局部变量表Slot复用