网络编程1-URL相关的处理

网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据。程序员所作的事情就是把数据发送到指定的位置,或者接收到指定的数据,这个就是狭义的网络编程范畴。在发送和接收数据时,大部分的程序设计语言都设计了专门的API实现这些功能,程序员只需要调用即可。
URL(统一资源定位符),俗称为网页地址。表示为互联网上的资源,如网页或者FTP地址。
Java是如处理URL的。URL可以分为如下几个部分。
protocol(协议)可以是 HTTP、HTTPS、FTP 和 File,
HTTP 协议的 URL 实例如下:
http://www.runoob.com/index.html?language=cn#j2se
URL 解析:
协议为(protocol):http
主机为(host:port):www.runoob.com
端口号为(port): 80 ,以上URL实例并未指定端口,因为 HTTP 协议默认的端口号为 80。
文件路径为(path):/index.html
请求参数(query):language=cn
定位位置(fragment):j2se,定位到网页中 id 属性为 j2se 的 HTML 元素位置 。

一.URL类

1.创建URL

(1) public URL (String spec);
     通过一个表示URL地址的字符串可以构造一个URL对象
     URL urlBase=new URL(“http://www. 263.net/”)
(2) public URL(URL context, String spec);
     通过基URL和相对URL构造一个URL对象。
     URL net263=new URL (“http://www.263.net/“);
     URL index263=new URL(net263, “index.html”)
(3) public URL(String protocol, String host, String file);
     new URL(“http”, “www.gamelan.com”, “/pages/Gamelan.net. html”);
(4) public URL(String protocol, String host, int port, String file);
     URL gamelan=new URL(“http”, “www.gamelan.com”, 80, “Pages/Gamelan.network.html”);
  注意:类URL的构造方法都声明抛弃非运行时例外(MalformedURLException),因此生成URL对象时,我们必须要对这一例外进行处理,通常是用try-catch语句进行捕获。格式如下:

try{
     URL myURL= new URL(…)
  }catch (MalformedURLException e){
  …  }

2. 解析一个URL

一个URL对象生成后,其属性是不能被改变的,但是我们可以通过类URL所提供的方法来获取这些属性:
   public String getProtocol() 获取该URL的协议名。
   public String getHost() 获取该URL的主机名。
   public int getPort() 获取该URL的端口号,如果没有设置端口,返回-1。
   public String getFile() 获取该URL的文件名。
   public String getRef() 获取该URL在文件中的相对位置。
   public String getQuery() 获取该URL的查询信息。
   public String getPath() 获取该URL的路径
   public String getAuthority() 获取该URL的权限信息
   public String getUserInfo() 获得使用者的信息
    public String getRef()获得该URL的锚

3.从URL读取网络资源

当我们得到一个URL对象后,就可以通过它读取指定的WWW资源。这时我们将使用URL的方法openStream(),其定义为:
InputStream openStream();
  方法openSteam()与指定的URL建立连接并返回InputStream类的对象以从这一连接中读取数据。

URL url = new URL("http://www.baidu.com");
//使用openStream得到一输入流并由此构造一个BufferedReader对象
BufferedReader br = new BufferedReader(new InputStreamReader( url.openStream()));
String line = null;
while(null != (line = br.readLine()))
{
System.out.println(line);
}
br.close();

二.URLConnections 类方法

openConnection() 返回一个 java.net.URLConnection。
例如:
如果你连接HTTP协议的URL, openConnection() 方法返回 HttpURLConnection 对象。
如果你连接的URL为一个 JAR 文件, openConnection() 方法将返回 JarURLConnection 对象。
等等…
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
实例
以下实例中URL采用了HTTP 协议。 openConnection 返回HttpURLConnection对象。
URLConnDemo.java

import java.net.*;
import java.io.*;
public class URLConnDemo
{
   public static void main(String [] args)
   {
      try
      {
         URL url = new URL("http://www.runoob.com");
         URLConnection urlConnection = url.openConnection();
         HttpURLConnection connection = null;
         if(urlConnection instanceof HttpURLConnection)
         {
            connection = (HttpURLConnection) urlConnection;
         }
         else
         {
            System.out.println("请输入 URL 地址");
            return;
         }
         BufferedReader in = new BufferedReader(
         new InputStreamReader(connection.getInputStream()));
         String urlString = "";
         String current;
         while((current = in.readLine()) != null)
         {
            urlString += current;
         }
         System.out.println(urlString);
      }catch(IOException e)
      {
         e.printStackTrace();
      }
   }
}

以上实例编译运行结果如下:

$ javac URLConnDemo.java 
$ java URLConnDemo
.....这里会输出菜鸟教程首页(http://www.runoob.com)的 HTML 内容.....

参考:http://www.runoob.com/java/java-url-processing.html
http://blog.csdn.net/csh624366188/article/details/7331716

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值