收藏
回答

微信jsapi发起支付,回调解析body是空的?

微信jsapi发起支付,回调解析body是空的?

private String getRequestBody(HttpServletRequest request) {
    try {
        InputStream inStream = request.getInputStream();
        ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
            outSteam.write(buffer, 0, len);
        }
        outSteam.close();
        inStream.close();
        String result = new String(outSteam.toByteArray(), "utf-8");// 获取微信调用我们notify_url的返回信息
        return result;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

/**
 * 读取请求数据流
 * body空值
 */
private String getRequestBody1(HttpServletRequest request) {
    StringBuffer sb = new StringBuffer();
    try (ServletInputStream inputStream = request.getInputStream();
         BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
    ) {
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }
    return sb.toString();
}

/**
 * 返回空
 * @param request
 * @return
 */
private String getRequestBody2(HttpServletRequest request) {
    ByteArrayOutputStream body = new ByteArrayOutputStream();
    try {
        ServletInputStream inputStream = request.getInputStream();
        byte[] buffer = new byte[1024];
        for (int length; (length = inputStream.read(buffer)) != -1; ) {
            body.write(buffer, 0, length);
        }
    } catch (IOException ex) {
        logger.error("支付回调,读取数据流异常", ex);
    }
    logger.info("支付回调,通知消息体:"+ body);
    return body.toString();
}


String body = null;
 try {
     body = IOUtils.toString(request.getInputStream());
 } catch (IOException e) {
     throw new RuntimeException(e);
 }

这是微信返回时,解析body的4种方法,返回body都是空的,后台报错

out_trade_no:

98d1328ee5f04a039fa59005311f9e64

24d4f66459fb4fb08daa763f7f31a8f3

1322dc6e96784df0a273c983d1561e35

48ce7137e59b4de9bc32c43c2676b297

05c3e8b9bbec4e3190dcd625bfd9f3f9

d55a45f8137a45538b8150f3e746d800

66804d1f8ab84d1da15a558ebf6b7f4e

c46849e2fa0e45aba98848e1c316bcec

回答关注问题邀请回答
收藏

1 个回答

登录 后发表内容