`
dragonhunter
  • 浏览: 32591 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

java HttpServletRequest的getQueryString,getInputStream,getParameterMap的区别

    博客分类:
  • java
阅读更多

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();
        }
    }

在服务端调试结果如下:

requestMothed Content-type request方法 是否可以获取参数
get   getQueryString true
get   getInputStream false
get   getParameterMap true
post application/x-www-form-urlencoded getQueryString false
post application/x-www-form-urlencoded getInputStream true
post application/x-www-form-urlencoded getParameterMap true
post text/html getQueryString false
post text/html getInputStream false
post text/html getParameterMap true

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics