JAVA网络编程之——URL类

JAVA网络编程之——URL类

    1. URL类

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

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



    2.  URLConnections 类方法
    openConnection() 返回一个 java.net.URLConnection。
    我连接的是HTTP协议的URL,所以openConnection() 方法返回 HttpURLConnection 对象。
    URLConnection 方法列表如下:
序号 方法描述
1 Object getContent() 
检索URL链接内容
2 Object getContent(Class[] classes) 
检索URL链接内容
3 String getContentEncoding() 
返回头部 content-encoding 字段值。
4 int getContentLength() 
返回头部 content-length字段值
5 String getContentType() 
返回头部 content-type 字段值
6 int getLastModified() 
返回头部 last-modified 字段值。
7 long getExpiration() 
返回头部 expires 字段值。
8 long getIfModifiedSince() 
返回对象的 ifModifiedSince 字段值。
9 public void setDoInput(boolean input)
URL 连接可用于输入和/或输出。如果打算使用 URL 连接进行输入,则将 DoInput 标志设置为 true;如果不打算使用,则设置为 false。默认值为 true。
10 public void setDoOutput(boolean output)
URL 连接可用于输入和/或输出。如果打算使用 URL 连接进行输出,则将 DoOutput 标志设置为 true;如果不打算使用,则设置为 false。默认值为 false。
11 public InputStream getInputStream() throws IOException
返回URL的输入流,用于读取资源
12 public OutputStream getOutputStream() throws IOException
返回URL的输出流, 用于写入资源。
13 public 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的InputStream
				BufferedReader 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("----------------------------------------");
				//输出InputStream
				String 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的InputStream
			BufferedReader 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());

			//输出InputStream
			String line=null;
			while((line=br.readLine()) != null){
				//输出内容为原始的HTML标记内容
				System.out.println(line);
			}

   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值