通過rul讀取流資料此條適用於 webservice-restful 執行介面

瓜瓜東西發表於2014-10-21
package com.newegg.frontservice.test;

import java.net.URLConnection;
import java.net.HttpURLConnection;
import java.net.JarURLConnection;
import java.net.URL;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
 
public class URLConnectionTest {
    public static void main(String []args){
       try{
           /*
            *   方法一
            *     
           URL url = new URL("http://www.sina.com.cn");
           URLConnection urlcon = url.openConnection();
           InputStream is = urlcon.getInputStream();
            */
          
           /*
            * 方法二
            *
            * URL url = new URL("http://www.yhfund.com.cn");
           HttpURLConnection urlcon = (HttpURLConnection)url.openConnection();
           InputStream is = urlcon.getInputStream();
            */
          
          
          
           /*
            * 方法三
            * URL url = new URL("http://www.yhfund.com.cn");
           InputStream is = url.openStream();
            */
           long begintime = System.currentTimeMillis();
          
           URL url = new URL("aaa");
           HttpURLConnection urlcon = (HttpURLConnection)url.openConnection();
           urlcon.connect();         //獲取連線
           InputStream is = urlcon.getInputStream();
           BufferedReader buffer = new BufferedReader(new InputStreamReader(is));
           StringBuffer bs = new StringBuffer();
           String l = null;
           while((l=buffer.readLine())!=null){
               bs.append(l).append("/n");
           }
           System.out.println(bs.toString());
          
           //System.out.println(" content-encode:"+urlcon.getContentEncoding());
           //System.out.println(" content-length:"+urlcon.getContentLength());
           //System.out.println(" content-type:"+urlcon.getContentType());
           //System.out.println(" date:"+urlcon.getDate());
      
          
           System.out.println("總共執行時間為:"+(System.currentTimeMillis()-begintime)+"毫秒");
        }catch(IOException e){
           System.out.println(e);
       }
    }
 
}

相關文章