HttpServletRequest获取请求参数

private static String getRequestParameter(HttpServletRequest request, HttpServletResponse response) {

    if (null == request) {
        return null;
    }

    String method = request.getMethod();
    String param = null;
    if (method.equalsIgnoreCase("GET")) {
        /**
         *获取?后面的字符串
         *http://127.0.0.1:8080/test?username=zhangsan&age=100
         *-->username=zhangsan&age=100
         *http://127.0.0.1:8080/test?{"username":"zhangsan"}
         *-->{"username":"zhangsan"}是json字符串
         *有了josn串就可以映射成对象了
         */
        param = request.getQueryString();
        if (Base64.isBase64(param)) {
            param = new String(Base64.decodeBase64(param), StandardCharsets.UTF_8);
        }
        System.out.println("param:" + param);
    } else {
        param = getBodyData(request);
        if (Base64.isBase64(param)) {
            param = new String(Base64.decodeBase64(param), StandardCharsets.UTF_8);
        }

    }
    return param;
}

//获取请求体中的字符串(POST)
private static String getBodyData(HttpServletRequest request) {
    StringBuffer data = new StringBuffer();
    String line = null;
    BufferedReader reader = null;
    try {
        reader = request.getReader();
        while (null != (line = reader.readLine()))
            data.append(line);
    } catch (IOException e) {
    } finally {
    }
    return data.toString();
}
上一篇:asp.net中Get请求和Post请求


下一篇:浏览器的get请求和post请求的区别