HttpServletRequest中读取HTTP请求的body
Posted liyiren
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HttpServletRequest中读取HTTP请求的body相关的知识,希望对你有一定的参考价值。
背景
Spring MVC 等框架用久了之后,发现对于POST请求里面如果不使用@RequestBody 好像很难获得POST的body。
HTTP请求中的是字符串数据
字符串读取
void charReader(HttpServletRequest request) { BufferedReader br = request.getReader(); String str, wholeStr = ""; while((str = br.readLine()) != null){ wholeStr += str; } System.out.println(wholeStr); }
二进制读取
void binaryReader(HttpServletRequest request) { int len = request.getContentLength(); ServletInputStream iii = request.getInputStream(); byte[] buffer = new byte[len]; iii.read(buffer, 0, len); }
void binaryReader(HttpServletRequest request) { intlen = request.getContentLength(); ServletInputStream iii = request.getInputStream(); byte[] buffer = new byte[len]; iii.read(buffer, 0, len); }
以上是关于HttpServletRequest中读取HTTP请求的body的主要内容,如果未能解决你的问题,请参考以下文章
Servlet获取请求数据HttpServletRequest
Servlet获取请求数据HttpServletRequest
Servlet获取请求数据HttpServletRequest