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