现在的位置: 首页 > 综合 > 正文

Java网络编程URL与URLConnection

2018年01月31日 ⁄ 综合 ⁄ 共 762字 ⁄ 字号 评论关闭

ip(Internet Protocol) : 网络之间互连的协议

ip地址=网络地址+主机地址

网络号:用于识别主机所在的网络

主机号:用于识别该网络中的主机

URL:统一资源占位符,可以直接使用此类找到互联网上的资源.

public class URLDemo{
	public static void main(String args[]) throws Exception {	// 所有异常抛出
		URL url = new URL("http","www.mldnjava.cn",80,"/curriculum.htm") ;
		InputStream input = url.openStream() ;	// 打开输入流
		Scanner scan = new Scanner(input) ;		// 实例化Scanner类
		scan.useDelimiter("\n") ;	// 设置读取分隔符
		while(scan.hasNext()){
			System.out.println(scan.next()) ;
		}
	}
};

URLConnection:是封装访问远程网络资源一般方法的类,通过它可以建立与远程服务器的连接,检查远程资源的一些属性.

public class URLConnectionDemo{
	public static void main(String args[]) throws Exception {	// 所有异常抛出
		URL url = new URL("http://www.mldnjava.cn") ;
		URLConnection urlCon = url.openConnection() ;	// 建立连接
		System.out.println("内容大小:" + urlCon.getContentLength()) ;
		System.out.println("内容类型:" + urlCon.getContentType()) ;
	}
};

抱歉!评论已关闭.