示例
/**
* 调用XXX接口
* @param request
* @param response
* @return
* @throws IOException
*/
@ResponseBody
@RequestMapping("XXX.do")
public JSONObject XXX(HttpServletRequest request, HttpServletResponse response) throws IOException{
logger.info("------------------XXX ------------------");
String username = request.getParameter("name");
String password = request.getParameter("password");
String model = request.getParameter("model");
logger.info("callurl:"+GetScriptProperties.getValue("Url"));
HttpClient client = new
DefaultHttpClient();HttpPost httpPost = new HttpPost(GetScriptProperties.getValue("Url"));
String result = null;
String mkey = GetScriptProperties.getValue("key");
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
String timestamp =df.format(new Date());
String checkcode = username + timestamp + mkey;
checkcode = Md5DigestUtil.md5Bit256(checkcode);
JSONObject obj = new JSONObject();
obj.put("name", username);
obj.put("password", password);
obj.put("timestamp", timestamp);
obj.put("checkcode", checkcode);
obj.put("mode", model);
logger.info("inputJson:"+obj.toString());
// 解决中文乱码问题
StringEntity stringEntity = new StringEntity(obj.toString(), "UTF-8");
stringEntity.setContentEncoding("UTF-8");
httpPost.setEntity(stringEntity);
//执行post请求
HttpResponse httpResponse;
try {
httpResponse = client.execute(httpPost);
if(response != null){
HttpEntity resEntity = httpResponse.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,"utf-8");
}
}
} catch (Exception e) {
logger.info("Exception:"+e.getMessage());
JSONObject jsonResult = JSONObject.fromObject("{resultCode: '1', resultDesc: '执行失败:"+e.getMessage()+"'}");
return jsonResult;
}
logger.info("resultInfo:"+result);
JSONObject jsonResult = JSONObject.fromObject(result);
boolean flag = false;
if(jsonResult.get("resultCode").toString().equals("0")){
flag = true;
}
String userOperation = jsonResult.get("resultDesc").toString();
xxxLogService.saveLog(ValidateEntityUtil.validateDoScriptSyslog(request, Thread.currentThread().getStackTrace()[1],
flag, userOperation));
return jsonResult;
}