import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; /** * 获取当前URL对象的相关属性信息,并将结果打印到控制台 * @author 笑傲江湖 */ public class URL_Class { public static void main(String[] args) throws MalformedURLException { URL Aurl = new URL("http://java.sun.com/docs/books/"); URL tuto = new URL(Aurl,"tutorial.intro.html#DOWNLOADING"); //获取协议名 System.out.println("protocol:" + tuto.getProtocol()); System.out.println("host:" + tuto.getHost()); System.out.println("port:" + tuto.getPort()); System.out.println("ref:" + tuto.getRef()); System.out.println("query:" + tuto.getQuery()); System.out.println("path:" + tuto.getPath()); } } /** * 执行结果是指定网站资源对应文件的HTML源码 * @author 笑傲江湖 */ class URLReader { public static void main(String[] args) throws IOException { URL baidu = new URL("http://www.baidu.com"); BufferedReader buffer; buffer = new BufferedReader(new InputStreamReader(baidu.openStream())); String inputLine; while((inputLine = buffer.readLine()) != null ){ System.out.println(inputLine); } buffer.close(); } }
Java (网络通信)中对URL的相关操作
最新推荐文章于 2023-12-20 08:49:04 发布