zoukankan      html  css  js  c++  java
  • java获取http请求的Header和Body

    在http请求中,有Header和Body之分,读取header使用request.getHeader("...");

    读取Body使用request.getReader(),但getReader获取的是BufferedReader,需要把它转换成字符串,下面是转换的方法。

    public class TestController {
    
        @RequestMapping("/a")
        protected void doPost(HttpServletRequest request,
                HttpServletResponse response, BufferedReader br)
                throws ServletException, IOException {
    //Header部分 System.out.print(request.getHeaderNames()); Enumeration
    <?> enum1 = request.getHeaderNames(); while (enum1.hasMoreElements()) { String key = (String) enum1.nextElement(); String value = request.getHeader(key); System.out.println(key + " " + value); } //body部分 String inputLine; String str = ""; try { while ((inputLine = br.readLine()) != null) { str += inputLine; } br.close(); } catch (IOException e) { System.out.println("IOException: " + e); } System.out.println("str:" + str); }
  • 相关阅读:
    linux配置虚拟主机
    mysql允许远程登录
    php优化
    php socket 函数
    1-- prometheus安装、图形化界面
    Ansible Roles
    Ansible 的 Playbook
    Ansible 变量
    Ansible 模块
    Ansible入门;Ansible ad-hoc; ansible-vault加密工具 ;ansible-console
  • 原文地址:https://www.cnblogs.com/azhqiang/p/5413803.html
Copyright © 2011-2022 走看看