java HttpServletRequest的getQueryString,getInputStream,getParameterMap的区别

HttpServletRequest可以通过getQueryString和getInputStream和getParameterMap来获取参数。

三者有什么区别了:

我们通过一个简单的demo来看下:

public static final String method = "POST";
    
    public static void main(String[] args) {
        try {
            BufferedReader reader = null;
            OutputStream out = null;
            String url = "http://woqufadai.com/hdd/channel/payment-return/YEEPAY?k=v";
            String params = "key=value";
            URL pageUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) pageUrl.openConnection();
            
            conn.setRequestProperty("Content-type", "text/html;charset=utf-8");
            //conn.addRequestProperty("Content-type", "text/html;charset=utf-8");
            ByteArrayOutputStream bout = null;
            bout = new ByteArrayOutputStream();
            bout.write(params.getBytes());
            byte[] b = bout.toByteArray();
            conn.setRequestMethod(method);
            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0 abc");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            out = conn.getOutputStream();
            out.write(b);
            out.flush();
            
            // Open connection to URL for reading.
            reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));    
            // Read page into buffer.
            String line;
            StringBuffer pageBuffer = new StringBuffer();
            while ((line = reader.readLine()) != null) {
                pageBuffer.append(line);
            }
            System.out.println(pageBuffer.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

在服务端调试结果如下:

requestMothedContent-typerequest方法是否可以获取参数
get getQueryStringtrue
get getInputStreamfalse
get getParameterMaptrue
postapplication/x-www-form-urlencodedgetQueryStringfalse
postapplication/x-www-form-urlencodedgetInputStreamtrue
postapplication/x-www-form-urlencodedgetParameterMaptrue
posttext/htmlgetQueryStringfalse
posttext/htmlgetInputStreamfalse
posttext/htmlgetParameterMaptrue

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值