HttpClientUtil发送post请求获取不到参数,servlet如何获取请求参数

149 阅读1分钟

HttpClientUtil有两个常用的方法

String result = httpClientUtil.sendPostUrl(url, map, "UTF-8");
String result = httpClientUtil.sendPost(url, map, "UTF-8");

两个方法传递参数的方式不同,所以获取参数的方式也不同 sendPostUrl获取参数:

String method = request.getParameter("method");

sendPost获取参数:

Map<String, Object> params = new HashMap<>();
        // 获取内容格式
        String contentType = request.getContentType();
        if ("application/json; charset=UTF-8".equalsIgnoreCase(contentType)) {
            // 使用 commons-io中 IOUtils 类快速获取输入流内容
            String paramJson = IOUtils.toString(request.getInputStream(), "UTF-8");
            Map parseObject = JSON.parseObject(paramJson, Map.class);
            params.putAll(parseObject);
        }

补充:两个方法的参数传递方式详解