httpclient post请求

示例

/**

     * 调用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;

    }

 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值