package com.franky.net.http;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
/**
* URL的使用实例
* @author Administrator
*
*/
public class URLDemo {
public static void main(String[] args) throws IOException{
//远程资源地址
URL url = new URL("http://www.sina.com/");
//获取远程资源地址的各种属性
// int port = url.getPort();
// System.out.println(port);
// System.out.println(url.getHost());
// System.out.println(url.getProtocol());
//打开远程资源连接对象
URLConnection conn = url.openConnection();
//获取资源输入流
InputStream is = conn.getInputStream();
int len = 0;
byte[] b = new byte[1024];
//读取资源
while((len = is.read(b))!=-1){
System.out.println(new String(b,0,len));
}
}
}
URL的使用实例
最新推荐文章于 2024-04-17 16:41:57 发布