简述:
URL,即
Uniform / Universal Resource Locator,中文名字叫统一资源定位符。它如同在网络上的门牌,是因特网上标准的资源的地址。
在因特网的历史上,统一资源定位符的发明是一个非常基础的步骤。统一资源定位符的语法是一般的,可扩展的,它使用ASCII代码的一部分来表示因特网的地址。统一资源定位符的开始,一般会标志着一个计算机网络所使用的网络协议。
统一资源定位符的标准格式如下:
协议类型://服务器地址(必要时需加上端口号)/路径/文件名
超文本传输协议(HTTP)的统一资源定位符将从因特网获取信息的五个基本元素包括在一个简单的地址中:
- 传送协议。
- 服务器。
- 端口号。(以数字方式表示,若为HTTP的默认值“:80”可省略)
- 路径。(以“/”字符区别路径中的每一个目录名称)
- 查询。(GET模式的窗体参数,以“?”字符为起点,每个参数以“&”隔开,再以“=”分开参数名称与数据,通常以UTF8的URL编码,避开字符冲突的问题)
统一资源定位符不但被用作网页地址,数据库终端也使用统一资源定位符服务器连接其服务器。实际上任何终端-服务器程序都可以使用统一资源定位符来连接。
以下是一个数据库的统一资源定位符:
jdbc:datadirect:oracle://myserver:1521;sid=testdb
应用:
java中有个类叫URL,在java.net包中,这个类用来处理与url相关的内容。
1.创建url
打开文档,查看该类的构造方法:
创建url大体有两种方式,一种是传入绝对地址,一种是传入相对地址。
比如:URL myURL = new URL(“
http://example.com/”);
这里面传入的就是绝对的url,通过这个url,我们可以定位该资源,因为这个url中指定了协议名和服务器地址。
当然也可以把协议和主机名,端口号,文件名分开.像这样:new URL("http", "example.com", "/pages/page1.html");
另外一种构造方式是传入相对地址。
这两个url同在一个网站中,这时我们可以用相对的地址定位它们:
URL myURL = new URL(
http://example.com/pages/);
URL page1URL = new URL(myURL, "page1.html");
URL page2URL = new URL(myURL, "page2.html");
另外,还可以通过uri转换的方式构造url:
URI uri = new URI("http", "example.com", "/hello world/", "");
URL url = uri.toURL();
2.MalformedURLException
当构造函数的参数指向空或者未知的协议名,这个构造函数都会抛出MalformedURLException异常。
一般情况下,我们会把构造函数包含在try-catch中:
try {
URL myURL = new URL(. . .)
} catch (MalformedURLException e) {
. . .
//
exception handler code here
. . .
}
3.解析url
URL提供了一系列get方法用来获取url的属性:
4.读取url所指向的资源
我们可以通过url的openStream方法获取inputStream流。
例如,我们想获取百度主页的内容,可以这样做:
package basicNetDemo;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class URLDemo
{
/**
* @param args
*/
public static void main(String[] args)
{
func_1();
}
public static void func_1()
{
try
{
URL url = new URL("http://www.baidu.com/");
BufferedReader bufr =
new BufferedReader(new InputStreamReader(url.openStream()));
PrintWriter pw = new PrintWriter(System.out,true);
String line = null;
while((line = bufr.readLine()) != null)
{
pw.println(line);
pw.flush();
}
}
catch (MalformedURLException e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch (IOException e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
5.连接url
当你成功的建立了一个URL类的对象后,你可以通过该类的openConnection方法获取URLConnection的对象。
URLConnection类代表应用程序和 URL 之间的通信链接,此类的实例可用于读取和写入此 URL 引用的资源。
通常创建一个到url的连接需要以下几个步骤:
- 通过在 URL 上调用
openConnection
方法创建连接对象。 - 处理设置参数和一般请求属性。
- 使用
connect
方法建立到远程对象的实际连接。 - 远程对象变为可用。远程对象的头字段和内容变为可访问。
代码如下:
try {
URL myURL = new URL("http://example.com/");
URLConnection myURLConnection = myURL.openConnection();
myURLConnection.connect();
} catch (MalformedURLException e) { // new URL() failed
. . .
} catch (IOException e) { // openConnection() failed
. . .
}
这里需要注意的是,我们不一定每一次都显式地调用URLConnection类的connect方法取得连接,
比如getInputStream和getOutputStream等方法会自动调用connect方法!
6.通过URLConnection类读取或者写入内容
1.读取:
import java.net.*;
import java.io.*;
public class
URLConnectionReader
{
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://www.oracle.com/");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()
));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
2.写入
package basicNetDemo;
import java.io.*;
import java.net.*;
public class URLDemo {
public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.err.println("Usage: java Reverse " +
"http://<location of your servlet/script>" +
" string_to_reverse");
System.exit(1);
}
String stringToReverse = URLEncoder.encode(args[1], "UTF-8");
URL url = new URL(args[0]);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(
connection.getOutputStream());
out.write("string=" + stringToReverse);
out.close();
BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
String decodedString;
while ((decodedString = in.readLine()) != null) {
System.out.println(decodedString);
}
in.close();
}
}
需要注意的是setDoOutput方法,如果打算使用 URL 连接进行输出,则将 DoOutput 标志设置为 true;如果不打算使用,则设置为 false。默认值为 false。 当设置为false时,getOutputStream方法会抛出 UnknownServiceException异常。