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); } } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。