网络编程URL类的使用

URL类的使用

URL(Uinform Resource Locator)类代表统一资源定位器,统一资源定位器

是指互联网“资源”的名称。资源可以使简单的文件或目录,也可以是对更为

复杂的对象的引用,例如对数据库或搜索引擎的查询。通常URL可以由协议

名、主机、端口和资源组成。URL的格式

为“protocol://host:port/resourceName”。例如,URL地

址“http://www.163.index.htm”。

URL类提供了多个构造方法用于创建URL对象,常用的有两个。
public URL(String str)
public URL(URL context,String str)

例:URL urlbaidu = new URL("http://www.baidu.com");

String getFile(): 获取此URL的资源名
String getHost(): 获取此URL的主机名
String getPath(): 获取此URL的路径部分
String getProtocol(): 获取此URL的协议名称
String getQuery(): 获取此URL的查询字符串部分
int getPort(): 获取此URL的端口号
URLConnection openConnection(): 返回一个URLConnection对象,它表示到

URL所引用的远程对象的连接。
InputStream openStream(): 打开与此URL的连接,并返回一个用于读取该URL

资源的InputStream。

URL类的简单用法实例:

import java.net.*;
import java.io.*;
public class TestNet2{
 public static void main(String[] args)throws Exception{
  URL aURL=new URL

("http://java.sun.com:80/docs/books/tutorial"+"/index.html?

name=networking#DOWNLOADING");
  System.out.println("protocol="+aURL.getPtotocol());
  System.out.println("authority="+aURL.getAuthority

());
  System.out.println("host="+aURL.getHost());
  System.out.println("port="+aURL.getPort());
  System.out.println("path="+aURL.getPath());
  System.out.println("query="+aURL.getQuery());
  System.out.println("filename="+aURL.getFile());
  System.out.println("ref="+aURL.getRef());
 }
}

通过URl来截取网页的信息实例:

package com.hbsi.tcp;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

public class URLDemo {

 public static void main(String[] args) throws Exception {
  URL url = new URL("http://www.baidu.com");
  InputStream in = url.openStream();
  BufferedReader brin = new BufferedReader(new

InputStreamReader(in));
  String line = null;
  FileOutputStream fos = new FileOutputStream("e:\

\mydownloadhtml.html");
  while ((line = brin.readLine()) != null) {
   fos.write(line.getBytes());
   System.out.println(line);
  }
  brin.close();
  fos.close();
 }
}

URLDecoder类和URLEncoder类

URLDecoder类和URLEncoder类用于完成普通字符串和application/x-www-

form-urlencoded MIME字符串(“所谓乱码”)之间的相互转换。

URLDecoder类包含一个encode(String s,String enc)静态方法,它可以将看

上去是乱码的特殊字符串转换成普通字符串。

URLEncoder类包含一个encode(String s,String enc)静态方法,它可以将普

通字符串转换成application/x-www-form-urlencoded MIME字符串。

import java.net.*;
import java.io.*;
public class TestNet4{
 public static void main(String[] args){
  try{
  //将application/x-www-form-urlencoded字符串解码
  String str1=URLDecoder.decode("java%E5%BC%E5%8F

%91","UTF-8");
  System.out.print(str1);
  //将普通字符串转换成application/x-www-form-

urlencoded字符串
  String str2=URLEncoder.encode("Java程序开发","UTF-

8");
  System.out.print(str2); 
  }catch(){
     System.out.println(e.getMessage()); 
  }
 }
}

程序运行结果如下:

Java开发
Java%E7%A8%8B%E5BA%8F%E%BC%80%E%%8F%91

 

 

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值