URL
URL(Uniform Resource Locator)中文名为统一资源定位符,有时也被俗称为网页地址
URL可以分为如下几个部分。
protocol://host:port/path?query#ref
protocols(协议)可以是 HTTP, HTTPS, FTP, 和File。port 为端口号。path为文件路径及文件名。
创建URL对象
public URL(String protocol, String host, int port, String file) throws MalformedURLException.
通过给定的参数(协议、主机名、端口号、文件名)创建URL。
eg:URL lib = new URL (“http” , www.cqwu.edu.cn, 80 , “/test/test.asp”);
public URL(String protocol, String host, String file) throws MalformedURLException
使用指定的协议、主机名、文件名创建URL,端口使用协议的默认端口。
eg new URL (“http” , www.cqwu.edu.cn, “/test/test.asp”);
public URL(String url) throws MalformedURLException
通过给定的URL字符串创建URL
eg:URL urll=new URL(http://www.cqwu.edu.cn);
public URL(URL context, String url) throws MalformedURLException
使用基地址和相对URL创建
eg URL urll=new URL(http://www.cqwu.edu.cn);
URL lib=new URL(urll , “library / library.asp”);
获取URL对象的属性
public String getPath();//返回URL路径部分。
public String getQuery();//返回URL查询部分。
public String getAuthority();//获取此 URL 的授权部分。
public int getPort();//返回URL端口部分
public int getDefaultPort();//返回协议的默认端口号。
public String getProtocol();//返回URL的协议
public String getHost();//返回URL的主机
public String getFile();//返回URL文件名部分
public String getRef();//获取此 URL 的锚点(也称为”引用”)。
String toString();//返回完整的URL字符串。
public class URLDemo {
public static void main(String[] args) {
try {
URL url = new URL("http://www.w3cschool.cc/index.html?language=cn#j2se");
System.out.println("URL is " + url.toString());
System.out.println("protocol is "
+ url.getProtocol());
System.out.println("authority is "
+ url.getAuthority());
System.out.println("file name is " + url.getFile());
System.out.println("host is " + url.getHost());
System.out.println("path is " + url.getPath());
System.out.println("port is " + url.getPort());
System.out.println("default port is "
+ url.getDefaultPort());
System.out.println("query is " + url.getQuery());
System.out.println("ref is " + url.getRef());
} catch (IOException e) {
e.printStackTrace();
}
}
}
访问Internet
有两种方法可以用来访问Internet,一是利用URL类的openStream()方法;二是使用openConnection()方法创建一个URLConnection类的对象。
URLConnection openConnection()
返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接。
InputStream openStream()
打开到此 URL 的连接并返回一个用于从该连接读入的 InputStream。以从这一连接中读取数据
使用openStream访问Internet
public class ReadURL {
public static void main(String args[]) throws Exception {
try {
URL url = new URL("http://www.baidu.com");
InputStreamReader isr = new InputStreamReader(url.openStream());
BufferedReader br = new BufferedReader(isr);
String str;
while ((str = br.readLine()) != null) {
System.out.println(str);
}
br.close();
isr.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
openStream()方法只能读取网络资源,若要既能读取又能发送数据,则要用到URL类的openConnection()方法来创建一个 URLConnection类的对象,此对象在本地机和URL指定的远程节点建立一条HTTP协议的数据通道,可进行双向数据传输。
URLConnection类
如果你连接HTTP协议的URL, openConnection() 方法返回 HttpURLConnection 对象。
如果你连接的URL为一个 JAR 文件, openConnection() 方法将返回 JarURLConnection 对象。
URLConnection方法
Object getContent();//检索URL链接内容
Object getContent(Class[] classes);//检索URL链接内容
String getContentEncoding();//返回头部 content-encoding 字段值。
int getContentLength();//返回头部 content-length字段值
String getContentType();//返回头部 content-type 字段值
int getLastModified();//返回头部 last-modified 字段值。
long getExpiration();//返回头部 expires 字段值。
long getIfModifiedSince();//返回对象的 ifModifiedSince 字段值。
public void setDoInput(boolean input);//URL 连接可用于输入和/或输出。如果打算使用 URL 连接进行输入,则将 DoInput 标志设置为 true;如果不打算使用,则设置为 false。默认值为 true。
public void setDoOutput(boolean output);//URL 连接可用于输入和/或输出。如果打算使用 URL 连接进行输出,则将 DoOutput 标志设置为 true;如果不打算使用,则设置为 false。默认值为 false。
public InputStream getInputStream() throws IOException;//返回URL的输入流,用于读取资源
public OutputStream getOutputStream() throws IOException;//返回URL的输出流, 用于写入资源。
public URL getURL();//返回 URLConnection 对象连接的URL
实例
以下实例中URL采用了HTTP 协议。 openConnection 返回HttpURLConnection对象。
public class URLConnDemo
{
public static void main(String [] args)
{
try
{
URL url = new URL("http://www.w3cschool.cc");
URLConnection urlConnection = url.openConnection();
HttpURLConnection connection = null;
if(urlConnection instanceof HttpURLConnection)
{
connection = (HttpURLConnection) urlConnection;
}
else
{
System.out.println("Please enter an HTTP 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();
}
}
}