获取无参数名的post数据请求示例

xiaoxiao2021-02-28  86

php版本

file_get_contents('php://input')

java servlet 版本

继承自HttpServlet的子类的方法中的server方法(或者doGet或者doPost方法都可以)

protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=UTF-8"); resp.setCharacterEncoding("utf-8");

               ServletInputStream ss = req.getInputStream(); String re=convertStreamToString(ss); System.out.println("无参数名的请求数据:"+re);

   }

public static String convertStreamToString(InputStream is) {                BufferedReader reader = new BufferedReader(new InputStreamReader(is));                StringBuilder sb = new StringBuilder();                      String line = null;               try {                   while ((line = reader.readLine()) != null) {                        sb.append(line + "\n");                    }                } catch (IOException e) {                    e.printStackTrace();                } finally {                   try {                        is.close();                    } catch (IOException e) {                        e.printStackTrace();                    }                }                     return sb.toString();            }

 

其他版本请自行百度

转载请注明原文地址: https://www.6miu.com/read-52413.html

最新回复(0)