zoukankan      html  css  js  c++  java
  • URL

    URL:URL(Uniform Resource Locator)是统一资源定位符的简称,它表示Internet上某一资源的地址。通过URL我们可以访问Internet上的各种网络资源,比如最常见WWW,FTP站点。浏览器通过解析给定的URL可以在网络上查找相应的文件或其他资源。

     1 import java.net.MalformedURLException;
     2 import java.net.URL;
     3 
     4 /*
     5  * URL常用方法
     6  */
     7 public class Test02 {
     8     public static void main(String[] args) {
     9         try {
    10             //创建一个URL实例
    11             URL imooc=new URL("http://www.baidu.com");
    12             //?后面表示参数,#后面表示锚点
    13             URL url=new URL(imooc, "/index.html?username=tom#test");
    14             System.out.println("协议:"+url.getProtocol());
    15             System.out.println("主机:"+url.getHost());
    16             //如果未指定端口号,则使用默认的端口号,此时getPort()方法返回值为-1
    17             System.out.println("端口:"+url.getPort());
    18             System.out.println("文件路径:"+url.getPath());
    19             System.out.println("文件名:"+url.getFile());
    20             System.out.println("相对路径:"+url.getRef());
    21             System.out.println("查询字符串:"+url.getQuery());
    22         } catch (MalformedURLException e) {
    23             e.printStackTrace();
    24         }
    25     }
    26 }

    获取网站页面源码

     1 package network;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.IOException;
     5 import java.io.InputStreamReader;
     6 import java.net.MalformedURLException;
     7 import java.net.URL;
     8 
     9 public class Urlconnection3 {
    10     public static void main(String[] args) throws IOException {
    11         URL url=new URL("http://www.sohu.com");
    12         
    13         BufferedReader br=new BufferedReader(new InputStreamReader(url.openStream()));
    14         
    15         String line=null;
    16         
    17         while(null!=(line=br.readLine())){
    18             System.out.println(line);
    19         }
    20         
    21         br.close();
    22     }
    23 }
  • 相关阅读:
    diffstat命令
    v-if与v-show的区别
    常数时间插入、删除和获取随机元素
    diff命令
    C++ bitset的简单使用
    树的直径 | 简答的两道模板题
    Codeforces Round #544 (Div. 3)简单题解
    VIM 入门手册, (VS Code)
    PTA 天梯赛 L3-003 社交集群(并查集)
    L3-002 特殊堆栈 (双数组模拟栈)
  • 原文地址:https://www.cnblogs.com/daneres/p/4668902.html
Copyright © 2011-2022 走看看