JAVA网络编程之——URL类

来源:互联网 发布:设置网络共享打印机 编辑:程序博客网 时间:2024/05/22 13:44
JAVA网络编程之——URL类

    1. URL类

    URL类中包含了很多方法用于访问URL的各个部分,具体方法及描述如下:

序号方法描述1public String getPath()
返回URL路径部分。2public String getQuery()
返回URL查询部分。3public String getAuthority()
获取此 URL 的授权部分。4public int getPort()
返回URL端口部分5public int getDefaultPort()
返回协议的默认端口号。6public String getProtocol()
返回URL的协议7public String getHost()
返回URL的主机8public String getFile()
返回URL文件名部分9public String getRef()
获取此 URL 的锚点(也称为"引用")。10public URLConnection openConnection() throws IOException
打开一个URL连接,并运行客户端访问资源。


    2.  URLConnections 类方法
    openConnection() 返回一个 java.net.URLConnection。
    我连接的是HTTP协议的URL,所以openConnection() 方法返回 HttpURLConnection 对象。
    URLConnection 方法列表如下:
序号方法描述1Object getContent() 
检索URL链接内容2Object getContent(Class[] classes) 
检索URL链接内容3String getContentEncoding() 
返回头部 content-encoding 字段值。4int getContentLength() 
返回头部 content-length字段值5String getContentType() 
返回头部 content-type 字段值6int getLastModified() 
返回头部 last-modified 字段值。7long getExpiration() 
返回头部 expires 字段值。8long getIfModifiedSince() 
返回对象的 ifModifiedSince 字段值。9public void setDoInput(boolean input)
URL 连接可用于输入和/或输出。如果打算使用 URL 连接进行输入,则将 DoInput 标志设置为 true;如果不打算使用,则设置为 false。默认值为 true。10public void setDoOutput(boolean output)
URL 连接可用于输入和/或输出。如果打算使用 URL 连接进行输出,则将 DoOutput 标志设置为 true;如果不打算使用,则设置为 false。默认值为 false。11public InputStream getInputStream() throws IOException
返回URL的输入流,用于读取资源12public OutputStream getOutputStream() throws IOException
返回URL的输出流, 用于写入资源。13public URL getURL()
返回 URLConnection 对象连接的URL
    测试代码如下:    
public class URLConnectionTest{public static void main(String[] args){try{URL url = new URL("http://localhost:8080/http/test.html");//获得URLConnection对像URLConnection conn = url.openConnection();//判断是否是HTTP协议的URL,如果是,获得网络连接对像HttpURLConnection httpConn;if(conn instanceof HttpURLConnection){httpConn = (HttpURLConnection)conn;//获得URLConnection的InputStreamBufferedReader br = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));/*获得URLConntion连接的头信息*/Map<String,List<String>> headerMap = httpConn.getHeaderFields();for(String key: headerMap.keySet()){//输出头字段System.out.println(key+"--->"+headerMap.get(key));}System.out.println("HeaderFields"+httpConn.getHeaderFields());System.out.println("Content-Length:"+httpConn.getContentLength());System.out.println("Content-Type:"+httpConn.getContentType());System.out.println("Content-Encoding:"+httpConn.getContentEncoding());System.out.println("----------------------------------------");//输出InputStreamString line=null;while((line=br.readLine()) != null){//输出内容为原始的HTML标记内容System.out.println(line);}}}catch (MalformedURLException e){e.printStackTrace();}catch (IOException e){e.printStackTrace();}}}


    实际上,也可以绕过HttpURLConnection类,直接通过URL类获得inputStream或者直接通过URLConnection获得inputStream和outputStream。
    测试如下:  
    1. 使用URL类的openStream功能直接获得一个用于从该连接读入的 InputStream
URL url = new URL("http://localhost:8080/http/test.html");BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));String line=null;while((line=br.readLine()) != null){//输出内容为原始的HTML标记内容System.out.println(line);}

    2. 使用URLConnection类获得该连接的inputStream和outputStream,同时还可以获得具体的协议和相关头信息。    
URL url = new URL("http://localhost:8080/http/test.html");//获得URLConnection对像URLConnection conn = url.openConnection();//获得URLConnection的InputStreamBufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));/*获得URLConntion连接的头信息*/Map<String,List<String>> headerMap = conn.getHeaderFields();for(String key: headerMap.keySet()){//输出头字段System.out.println(key+":"+headerMap.get(key));}System.out.println();System.out.println("Content-Length:"+conn.getContentLength());System.out.println("Content-Type:"+conn.getContentType());System.out.println("Content-Encoding:"+conn.getContentEncoding());System.out.println("Date:"+conn.getDate());System.out.println("Last-Modified:"+conn.getLastModified());//输出InputStreamString line=null;while((line=br.readLine()) != null){//输出内容为原始的HTML标记内容System.out.println(line);}

   

0 0
原创粉丝点击