小编dri*_*imk的帖子

当内容类型为 application/x-www-form-urlencoded 时,Java 读取 POST 数据

我有一个正在测试的 API。API 接收 POST 请求并像这样读取它

      StringBuffer jb = new StringBuffer();
      String line = null;
      try {
        BufferedReader reader = request.getReader();
        while ((line = reader.readLine()) != null)
            jb.append(line);

        System.out.println("jb: "+jb);
        System.out.println("request.getHeader('content-type'): "+request.getHeader("content-type"));

      } catch (Exception e) { /*report an error*/ }
Run Code Online (Sandbox Code Playgroud)

当我在“application/json;charset=utf-8”中发送 POST 请求时,一切正常

httpPost.setHeader("content-type", "application/json;charset=utf-8");
Run Code Online (Sandbox Code Playgroud)

它打印这个:

jb: {"client_domain":"=....); //proper Json data
request.getHeader('content-type'): application/json;charset=utf-8
Run Code Online (Sandbox Code Playgroud)

我可以正确读取数据。

但是我的问题是当我以相同的方式发送数据但我设置了内容类型“application/x-www-form-urlencoded;charset=utf-8”

httpPost.setHeader("content-type", "application/x-www-form-urlencoded;charset=utf-8");
Run Code Online (Sandbox Code Playgroud)

测试相同,只是内容类型不同,但似乎我不再收到任何数据:

jb: 
request.getHeader('content-type'): application/x-www-form-urlencoded;charset=utf-8
Run Code Online (Sandbox Code Playgroud)

任何的想法?

/// 更新

这是弹簧控制器

@RequestMapping(value = {"user/add"}, method = RequestMethod.POST, produces="application/json; charset=utf-8")
@ResponseBody
public ResponseEntity<String> getNewUserApi(HttpServletRequest request,
        HttpServletResponse response) …
Run Code Online (Sandbox Code Playgroud)

java post content-type spring-mvc mime-types

7
推荐指数
1
解决办法
2万
查看次数

标签 统计

content-type ×1

java ×1

mime-types ×1

post ×1

spring-mvc ×1