`

访问url--传值-->调用request.getInputStream()获取参数

    博客分类:
  • Java
 
阅读更多

访问url--传值-->调用request.getInputStream()获取参数

 

Java访问url--传值

 

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

public class Test {
    public static void main(String[] args) {

        try {
            URL url = new URL("http://192.168.1.99:8005/shipnav/mobilepilotop?type=3");

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            /**
             * 然后把连接设为输出模式。URLConnection通常作为输入来使用,比如下载一个Web页。
             * 通过把URLConnection设为输出,你可以把数据向你个Web页传送。下面是如何做:
             */
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestProperty("content-type", "text/html");
            connection.setRequestMethod("POST");
            connection.setConnectTimeout(50000);
            connection.setReadTimeout(50000);
            connection.connect();
            /**
             * 最后,为了得到OutputStream,简单起见,把它约束在Writer并且放入POST信息中,例如: ...
             */
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
            out.write("message=402881e3409a949201409aa8215e002c,563004840,2013-10-08 16:43:00");  //以流的形式传值
            out.flush();

            String sCurrentLine;
            String sTotalString;
            sCurrentLine = "";
            sTotalString = "";
            InputStream l_urlStream;
            l_urlStream = connection.getInputStream();
            // 传说中的三层包装阿!
            BufferedReader l_reader = new BufferedReader(new InputStreamReader(l_urlStream));
            while ((sCurrentLine = l_reader.readLine()) != null) {
                sTotalString += sCurrentLine + "/r/n";
            }
            System.out.println(sTotalString);  //获取返回的流

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

 获取请求传过来的参数(流)

  ServletInputStream inStream = req.getInputStream();
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[8192]; // buff用于存放循环读取的临时数据
        int rc = 0;
        while ((rc = inStream.read(buff, 0, 1024)) > 0) {
            swapStream.write(buff, 0, rc);
        }
        byte[] in_b = swapStream.toByteArray(); // in_b为转换之后的结果?
        String msg = new String(in_b);
        System.out.println("----------------------------------" + msg); //msg="message=402881e3409a949201409aa8215e002c,563004840,2013-10-08 16:43:00"
 
分享到:
评论

相关推荐

    浅谈图片上传利用request.getInputStream()获取文件流时遇到的问题

    下面小编就为大家带来一篇浅谈图片上传利用request.getInputStream()获取文件流时遇到的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    浅谈request.getinputstream只能读取一次的问题

    下面小编就为大家带来一篇浅谈request.getinputstream只能读取一次的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    postman调试接口工具

    在我们平时开发中,特别是需要与接口打交道时,无论是写接口还是用接口,拿到接口后肯定都得提前测试一下,这样的话就非常需要有一个比较给力的Http请求模拟工具,现在流行的这种工具也挺多的,像火狐浏览器插件-...

    JSF文件上传

    <managed-bean-scope>request</managed-bean-scope> </managed-bean> </faces-config> MyBean如下: package com.dhc; import java.io.BufferedInputStream; import java.io.FileOutputStream; import java.io....

    个人全自动1521传马工具日抓千鸡

    BufferedInputStream bis =new BufferedInputStream(p.getInputStream(), bufSize); int len; byte buffer[] = new byte[bufSize]; // Echo back what the program spit out while ((len = bis.read(buffer , 0, ...

    servlet电子书籍 pdf格式

    3、通过<init-param>标签来配置初始化参数.................................7 2.4 Get请求和Post请求 ................................................................7 2.5 ServletRequest ....................

    aspose-cells-8.5.2.jar带license.xml

    BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream())); License license = new License(); license.setLicense(br); } catch (Exception var3) { var3.printStackTrace...

    servlet2.4doc

    The doFilter method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain. doGet...

    Java调用Linux命令

    由于调用Runtime.exec方法所创建的子进程没有自己的终端或控制台,因此该子进程的标准IO(如stdin,stdou,stderr)都通过Process.getOutputStream(),Process.getInputStream(), Process.getErrorStream()方法重定向给它...

    JS组件Bootstrap Table使用方法详解

    new InputStreamReader(request.getInputStream(),"UTF-8")); StringBuilder sBuilder = new StringBuilder(""); String temp = ""; while((temp = bufr.readLine()) != null){ sBuilder.append(temp); }...

    ognl源码包值得用一用

    List<FileItem> list = upload.parseRequest(request); for (FileItem item : list) { if (item.isFormField()) { String name = item.getFieldName(); String value = item.getString("GBK")...

    CSS实现程序关键字加亮效果

    java.io.InputStream in=con.getInputStream(); int c; while((c=in.read())!=-1){ str+=((char)c); } str=new String(str.getBytes("ISO8859-1")); return str.substring(str.index...

    jsp探针 ver0.1

    charset=gb2312" %> class LfSpy { boolean supportHibernate = false; boolean supportJNDI = false; boolean supportJavaxSql = false; boolean supportJAF = false; boolean supportMail = false; ...

    java解析给定url

    * 现在要求用Java编写一个程序,该程序访问上面的URL,并从页面中, * 提取出上图所示位置的页面信息(仅图中所要求的内容),将其内容按如下格式,输出到控制台 * GrapWeatherInfo * @author lihsh * @version ...

    AndroidHttpURLConnection发送GET请求

    调用URL对象的openConnection( )来获取HttpURLConnection对象实例: HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 设置HTTP请求使用的方法:conn.setRequestMethod("GET"); 设置连接超时,...

    java 编写文件上传类简单易用

    InputStream in = request.getInputStream(); int total = 0; int once = 0; while ((total ) && (once >=0)) { once = in.read(buffer,total,len); total += once; } //3 OutputStream out=new ...

    实现视频在线播放.zip

    public void getVideo(HttpServletRequest request,HttpServletResponse response,@RequestParam("path")String videoPath) throws IOException { System.out.println("来请求了!路径:"+videoPath); //1.创建...

    commons-net-2.0.jar TelnetClient包.rar

    TelnetClient包,Telnet...然后,可以使用getInputStream() 和 getOutputStream() 方法获取用于通过Telnet连接发送和接收数据的InputStream和OutputStream 。使用完流后,必须调用 disconnect 而不是简单地关闭流。

    jspsmartupload上传下载,已修改过源代码!

    97. * 解决取得request的参数的中文编码问题 98. */ 99. String value = new String(m_binArray, m_startData, (m_endData - m_startData) + 1, "utf-8"); 100. 101. 102. m_formRequest.putParameter...

    ServletRequest过滤程序

    提供了ServletRequest过滤程序,重新构造对象内容,并有效规避request.getParameter()、request.getInputStream()冲突的问题,同时提供了对跨站脚本攻击XSS和SQL注入的过滤程序。

Global site tag (gtag.js) - Google Analytics