postman中Params和Body设置参数的问题

 

 http请求由3部分组成:
 1、首行(method URL HTTP版本)
 2、请求头(header)
 3、请求体(body)
 
 params是参数放在首行部分,跟在URL后面的方式传参。(http://www.baidu.com?name=liubei&age=48)
 postman里也能清楚的看到,如果在params里添加参数,他会自动跟在url后面。受url长度限制()、url编码
 
 对于使用SpringBoot框架 或者中微子代理基础框架来接收参数,
 params中的key-value参数、body中form-data参数,都可以用同样的方式接收,对于后端来说没有区别

package com.zlj.servlet;
import net.sf.json.JSONObject;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
@WebServlet("/login")
public class UserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (ServletInputStream)req.getInputStream(), "utf-8"));
            StringBuffer sb = new StringBuffer("");
            String temp;
            while ((temp = br.readLine()) != null) {
                sb.append(temp);
            }
            br.close();
            //获取到的json字符串
            String acceptjson = sb.toString();
            JSONObject json = JSONObject.fromObject(acceptjson);
            System.out.println(json);
            System.out.println("---------------这个是数据输出");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
servelt处理post请求body参数

ps:GET和POST请求限制

 

posted @ 2023-02-09 15:40  徒步阳光855  阅读(1309)  评论(0编辑  收藏  举报