网络编程:URL与URLConnection

1.URI与URL

    URI(Uniform Resource Identifier)统一资源标识符,是用来标识某一互联网资源名称的字符串。
    URL(Uniform Resource Locator)统一资源定位符,表示互联网上标准资源的地址。
    “https://baidu.com/item/url”
    像上面这样的,可以说它是URL,也可以说它是URI。
    “ <IMG src="../icons/logo.gif" alt="logo">
    上面这个就是平时我们写网页的时候引入图片的时候用的方法,它就是URI,但并不是URL,它这样写在网页内也可以帮我们定位到logo.gif,但单独拿出来后却并不行,它是依赖于进入到该网页的路径,在此基础上寻找logo.gif的。这种情况有点像相对路径而前一种情况就是绝对路径。
    由此可见URI的范围比URL的大,URL是URI的子集。URL只能是绝对路径,而URI可以是绝对路径也可以是相对路径。
        

2.URL与URLConnection


    在 java.net 包下有这么两个类:URL与URLConnection 。
    URL 就是URL的抽象(好像是废话),它的实例表示一个资源定位地址的对象。比如:协议(http)、主机名(127.0.0.1)、端口号(80)、头内容等。一眼能看见的如“ https://baidu.com/item/url”这样的信息,还看不见的提交的内容或查询的内容等。它是连接的基础(地址都没,你想去哪?)。
    URLConnection 是连接的抽象,包含整个连接过程:连接前的设置、连接、连接后返回的信息。
    连接的步骤:
  1. 调用URL上的openConnection方法得到 URLConnection对象;
  2.  URLConnection对象上设置一些参数和请求属性;
  3.  调用 URLConnection 的connect 方法建立与远程对象的连接;
  4.  连接成功后,远程对象就可以访问了。比如连接的是一个html文件,那么我就可以将它的文本信息获取下来在本地展示。

    对于URLConnection 对象 使用以下方法修改设置参数:
setAllowUserInteraction
setDoInput
setDoOutput
setIfModifiedSince
setUseCaches

    对于URLConnection 对象使用以下方法修改一般请求属性:
setRequestProperty

    demon 代码
package com.hlm.http;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class URLandURLconection {
      public static void main(String[] args ) throws IOException {
           URL url = new URL( "https://blog.csdn.net/mottohlm/article/details/80588927" );
           
            print ( "Authority:--->" + url .getAuthority()); //获取此 URL 的授权部分
            print ( "File:--->" + url .getFile()); //获取此 URL 的文件名,此 URL 的文件名,如果没有文件名,则返回一个空字符串
            print ( "Host:--->" + url .getHost()); //获取此 URL 的主机名
            print ( "Path:--->" + url .getPath()); //获取此 URL 的路径部分
            print ( "Protocol:--->" + url .getProtocol()); //获取此 URL 的协议名称
            print ( "Query:--->" + url .getQuery()); //获取此 URL 的查询部分
            print ( "Ref:--->" + url .getRef()); //获取此 URL 的锚点(也称为“引用”),如果没有锚点,则返回 null
            print ( "UserInfo:--->" + url .getUserInfo()); //获取此 URL 的 userInfo 部分
           print ( "Content:--->" + url .getContent().toString()); //获取此 URL 的内容
           print ( "DefaultPort:--->" + url .getDefaultPort()+ "" ); //获取与此 URL 关联协议的默认端口号,如果 URL 方案或 URL 的 URLStreamHandler 未定义默认的端口号,则返回 -1
            print ( "Port:--->" + url .getPort()+ "" ); //获取此 URL 的端口号,如果未设置端口号,则返回 -1
           
           
            print ( "《!----我是华丽的分割线-----!》" );
           URLConnection un = url .openConnection();
            un .connect();
           
           print ( "ContentEncoding:--->" + un .getContentEncoding()); //返回 content-encoding 头字段的值
           print ( "ContentLength:--->" + un .getContentLength()); //返回 content-length 头字段的值
            print ( "ContentType:--->" + un .getContentType()); //返回 content-type 头字段的值
            print ( "Date:--->" + un .getDate()); //返回 date 头字段的值
            print ( "Expiration:--->" + un .getExpiration()); //返回 expires 头字段的值
            print ( "LastModified:--->" + un .getLastModified()); //返回 last-modified 头字段的值
            print ( "DoInput:--->" + un .getDoInput()); //返回此 URLConnection 的 doInput 标志的值
            print ( "DoOutput:--->" + un .getDoOutput()); //返回此 URLConnection 的 doOutput 标志的值
            print ( "URL:--->" + un .getURL()); //返回此 URLConnection 的 URL 字段的值
            print ( "Permission:--->" + un .getPermission()); //返回一个权限对象,其代表建立此对象表示的连接所需的权限
           
            //将从网络中获取的数据放到本地一个叫test.html的文件中
           InputStream is = un .getInputStream();
           BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream( "C:\\Users\\Administrator\\Desktop\\URLtest.html" ));
            int n ;
            while (( n = is .read())!=-1){ 
             out .write( n ); 
             //System.out.print((char)n); 
         }
     }
     
      public static void print(String str ){
           System. out .println( str );
     }
}
    打印结果:
Authority:--->blog.csdn.net
File:--->/mottohlm/article/details/80588927
Host:--->blog.csdn.net
Path:--->/mottohlm/article/details/80588927
Protocol:--->https
Query:--->null
Ref:--->null
UserInfo:--->null
DefaultPort:--->443
Port:--->-1
《!----我是华丽的分割线-----!》
ContentEncoding:--->null
ContentLength:--->-1
ContentType:--->text/html; charset=UTF-8
Date:--->1528298757000
Expiration:--->0
LastModified:--->0
DoInput:--->true
DoOutput:--->false
Permission:--->("java.net.SocketPermission" "blog.csdn.net:80" "connect,resolve")
    保存到本地的html 文件为:

20180606


猜你喜欢

转载自blog.csdn.net/mottohlm/article/details/80602701