原文网址:Java--网络相关类--URL类、URLConnection类--使用/实例_IT利刃出鞘的博客-CSDN博客
简介说明
本文介绍Java里网络相关的类。包括:java.net.URL,java.net.URLConnection。
URL介绍
URL 可以分为如下几个部分。
protocol://host:port/path?query#fragment
URL实例
https://knife.blog.csdn.net/article/details/124509399?name=Tony&age=25#abc
- 协议(protocol):https(所有种类:HTTP、HTTPS、FTP 和 File)
- 主机(host:port):knife.blog.csdn.net
- 端口为(port): 80 ,以上URL实例并未指定端口,因为 HTTP 协议默认的端口号为 80。
- 文件路径(path):/article/details/124509399
- 请求参数(query):name=Tony&age=25
- 定位位置(fragment):abc,定位到网页中 id 属性为 abc 的 HTML 元素位置 。
| 方法 | 作用 |
| public URL(String protocol, String host, int port, String file) throws MalformedURLException | 通过给定的参数(协议、主机名、端口号、文件名)创建URL。 |
| public URL(String protocol, String host, String file) throws MalformedURLException | 使用指定的协议、主机名、文件名创建URL,端口使用协议的默认端口。 |
| public URL(String url) throws MalformedURLException | 通过给定的URL字符串创建URL |
| public URL(URL context, String url) throws MalformedURLException | 使用基地址和相对URL创建 |
| 方法 | 作用 |
| public String getProtocol() | 返回URL的协议 |
| public String getHost() | 返回URL的主机。 |
| public int getPort() | 返回URL端口部分。 |
| public int getDefaultPort() | 返回协议的默认端口号。 |
| public String getPath() | 返回URL路径部分。 |
| public String getQuery() | 返回URL查询参数部分。 |
| public String getAuthority() | 获取此 URL 的授权部分。 |
| public String getRef() | 获取此 URL 的锚点(也称为"引用")。 |
| public String getFile() | 返回URL文件名部分 |
| 方法 | 作用 |
| public URLConnection openConnection() throws IOException | 打开一个URL连接,并运行客户端访问资源。 |
package com.example.a;
import java.net.MalformedURLException;
import java.net.URL;
public class Demo {
public static void main(String[] args) {
URL url = null;
try {
url = new URL("https://knife.blog.csdn.net/article/details/124509399?name=Tony&age=25#abc");
} catch (MalformedURLException e) {
e.printStackTrace();
}
System.out.println("URL为 :" + url.toString());
System.out.println("协议为:" + url.getProtocol());
System.out.println("验证信息:" + url.getAuthority());
System.out.println("文件名及请求参数:" + url.getFile());
System.out.println("主机名:" + url.getHost());
System.out.println("路径:" + url.getPath());
System.out.println("端口:" + url.getPort());
System.out.println("默认端口:" + url.getDefaultPort());
System.out.println("请求参数:" + url.getQuery());
System.out.println("定位位置:" + url.getRef());
}
}
执行结果
URL为 :https://knife.blog.csdn.net/article/details/124509399?name=Tony&age=25#abc 协议为:https 验证信息:knife.blog.csdn.net 文件名及请求参数:/article/details/124509399?name=Tony&age=25 主机名:knife.blog.csdn.net 路径:/article/details/124509399 端口:-1 默认端口:443 请求参数:name=Tony&age=25 定位位置:abc示例2:通过基类和相对url创建
package com.example.a;
import java.net.MalformedURLException;
import java.net.URL;
public class Demo {
public static void main(String[] args) {
URL originUrl = null;
URL url = null;
try {
originUrl = new URL("https://knife.blog.csdn.net/article/details/124509399?name=Tony&age=25#abc");
url = new URL(originUrl, "1122?number=10#aaa");
} catch (MalformedURLException e) {
e.printStackTrace();
}
System.out.println("URL为 :" + url.toString());
System.out.println("协议为:" + url.getProtocol());
System.out.println("验证信息:" + url.getAuthority());
System.out.println("文件名及请求参数:" + url.getFile());
System.out.println("主机名:" + url.getHost());
System.out.println("路径:" + url.getPath());
System.out.println("端口:" + url.getPort());
System.out.println("默认端口:" + url.getDefaultPort());
System.out.println("请求参数:" + url.getQuery());
System.out.println("定位位置:" + url.getRef());
}
}
执行结果
URL为 :https://knife.blog.csdn.net/article/details/1122?number=10#aaa 协议为:https 验证信息:knife.blog.csdn.net 文件名及请求参数:/article/details/1122?number=10 主机名:knife.blog.csdn.net 路径:/article/details/1122 端口:-1 默认端口:443 请求参数:number=10 定位位置:aaaURLConnection类 说明
URL的openConnection() 返回一个 java.net.URLConnection。
- 如果你连接HTTP协议的URL, openConnection() 方法返回 HttpURLConnection 对象。
- 如果你连接的URL为一个 JAR 文件, openConnection() 方法将返回 JarURLConnection 对象。
- 等等...
| 方法 | 描述 | |
| 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 | |
| public void setDoOutput(boolean output) | URL连接可用于输入和/或输出。如果打算使用URL连接进行输出,则将DoOutput标志设置为tue | |
| public InputStream getInputStream()throws IOException | 返回URL的输入流,用于读取资源 | |
| public OutputStream getOutputStream()throws IOException | 返回URL的输出流,用于写入资源。 | |
| public URL getURL() | 返回URLConnection对象连接的URL | |
package com.example.a;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class Demo {
public static void main(String[] args) {
URL url = null;
try {
url = new URL("https://knife.blog.csdn.net/article/details/124509399?name=Tony&age=25#abc");
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();
}
}
}
执行结果
会打印出相应url的html内容其他网址
Java URL 处理 | 菜鸟教程



