JAVA中三种URL连接方法


package zzm.java.net;
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("http://www.yhfund.com.cn");
           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);
       }
    }

}

更多知识及其API查看:
http://www.runoob.com/java/java-url-processing.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值