Table of Contents
ToggleJava URL Methods
統一資源定位碼 ( Uniform Resource Locator ) 是網路上資源的引用或位址,透過網路進行通訊的 Java 程式碼可以使用 java.net.URL 類別來表示資源的位址, URL Java Methods 本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- net
| +- url
| +- URLMethodsTest.java
單元測試
URL Methods Java 提供建立、取得等操作 URL 。
constructor
URL Methods Java 建立一個 URL ,提供相關資訊。
@Test
public void constructor() {
try {
String value = "https://username:password@www.ruoxue.org/java-learn/java-net?amount=101#top";
URL url = new URL(value);
System.out.println(url);
System.out.println("Protocol: " + url.getProtocol());
System.out.println("Authority: " + url.getAuthority());
System.out.println("UserInfo: " + url.getUserInfo());
System.out.println("Host: " + url.getHost());
System.out.println("Port: " + url.getPort());
System.out.println("DefaultPort: " + url.getDefaultPort());
System.out.println("File: " + url.getFile());
System.out.println("Path: " + url.getPath());
System.out.println("Query: " + url.getQuery());
System.out.println("Ref: " + url.getRef());
URL url2 = new URL("https", "www.ruoxue.org", 443, "/java-learn/java-net?amount=101#top");
System.out.println(url2);
assertThat(url.getProtocol()).isEqualTo(url2.getProtocol());
assertThat(url.getHost()).isEqualTo(url2.getHost());
assertThat(url.getDefaultPort()).isEqualTo(url2.getDefaultPort());
assertThat(url.getFile()).isEqualTo(url2.getFile());
assertThat(url.getPath()).isEqualTo(url2.getPath());
assertThat(url.getQuery()).isEqualTo(url2.getQuery());
assertThat(url.getRef()).isEqualTo(url2.getRef());
} catch (MalformedURLException ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
}
https://username:password@www.ruoxue.org/java-learn/java-net?amount=101#top
Protocol: https
Authority: username:password@www.ruoxue.org
UserInfo: username:password
Host: www.ruoxue.org
Port: -1
DefaultPort: 443
File: /java-learn/java-net?amount=101
Path: /java-learn/java-net
Query: amount=101
Ref: top
https://www.ruoxue.org:443/java-learn/java-net?amount=101#top
constructorThrowException
URL Methods Java 建立一個 URL ,傳入無法辨識的字串,會拋出例外。
@Test
public void constructorThrowException() {
assertThatCode(() -> {
String value = "/java-learn/java-net";
URL url = new URL(value);
System.out.println(url);
}).isInstanceOf(MalformedURLException.class);
}
06:01:04-05:00
06:01:04-05:00
openConnection
URL Methods Java 建立一個 URL ,建立連線,取得連線資訊。
@Test
public void openConnection() {
try {
String value = "https://www.ruoxue.org";
URL url = new URL(value);
System.out.println(url);
URLConnection conn = url.openConnection();
System.out.println(conn);
System.out.println(conn.getContentType());
System.out.println(conn.getConnectTimeout());
assertThat(conn.getContentType()).isEqualTo("text/html; charset=utf-8");
assertThat(conn.getConnectTimeout()).isEqualTo(0);
} catch (Exception ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
}
https://www.ruoxue.org
sun.net.www.protocol.https.DelegateHttpsURLConnection:https://www.ruoxue.org
text/html; charset=utf-8
0
openStream
URL Methods Java 建立一個 URL ,開啟串流,取得回應內容。
@Test
public void openStream() {
try {
URL url = new URL("https://www.ruoxue.org");
System.out.println(url);
try (InputStream in = url.openStream()) {
String result = IOUtils.toString(in, StandardCharsets.UTF_8.toString());
System.out.println(result.length());
assertThat(result).hasSizeGreaterThan(0);
}
} catch (Exception ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
}
https://www.ruoxue.org
20400
URLMethodsTest.java
URL Methods in Java 新增單元測試,驗證 URL Functions in Java 是否符合預期。
package org.ruoxue.java_147.net.url;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
public class URLMethodsTest {
@Test
public void constructor() {
try {
String value = "https://username:password@www.ruoxue.org/java-learn/java-net?amount=101#top";
URL url = new URL(value);
System.out.println(url);
System.out.println("Protocol: " + url.getProtocol());
System.out.println("Authority: " + url.getAuthority());
System.out.println("UserInfo: " + url.getUserInfo());
System.out.println("Host: " + url.getHost());
System.out.println("Port: " + url.getPort());
System.out.println("DefaultPort: " + url.getDefaultPort());
System.out.println("File: " + url.getFile());
System.out.println("Path: " + url.getPath());
System.out.println("Query: " + url.getQuery());
System.out.println("Ref: " + url.getRef());
URL url2 = new URL("https", "www.ruoxue.org", 443, "/java-learn/java-net?amount=101#top");
System.out.println(url2);
assertThat(url.getProtocol()).isEqualTo(url2.getProtocol());
assertThat(url.getHost()).isEqualTo(url2.getHost());
assertThat(url.getDefaultPort()).isEqualTo(url2.getDefaultPort());
assertThat(url.getFile()).isEqualTo(url2.getFile());
assertThat(url.getPath()).isEqualTo(url2.getPath());
assertThat(url.getQuery()).isEqualTo(url2.getQuery());
assertThat(url.getRef()).isEqualTo(url2.getRef());
} catch (MalformedURLException ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
}
@Test
public void constructorThrowException() {
assertThatCode(() -> {
String value = "/java-learn/java-net";
URL url = new URL(value);
System.out.println(url);
}).isInstanceOf(MalformedURLException.class);
}
@Test
public void openConnection() {
try {
String value = "https://www.ruoxue.org";
URL url = new URL(value);
System.out.println(url);
URLConnection conn = url.openConnection();
System.out.println(conn);
System.out.println(conn.getContentType());
System.out.println(conn.getConnectTimeout());
assertThat(conn.getContentType()).isEqualTo("text/html; charset=utf-8");
assertThat(conn.getConnectTimeout()).isEqualTo(0);
} catch (Exception ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
}
@Test
public void openStream() {
try {
URL url = new URL("https://www.ruoxue.org");
System.out.println(url);
try (InputStream in = url.openStream()) {
String result = IOUtils.toString(in, StandardCharsets.UTF_8.toString());
System.out.println(result.length());
assertThat(result).hasSizeGreaterThan(0);
}
} catch (Exception ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
}
}
心得分享
URL Functions in Java 形式統一 ( Uniform ) 對於各種不同的資源,都能有相同的表現,因此可以使用相同的語義進行解釋和理解,在不影響現存的資源的情況下,出現新的資源,而資源 ( Resource ) 任何事情都可以成為資源,可以被標示,及定位碼 ( Locator ) 資源的引用或位址,熟悉 URL Methods in Java 這些方法的操作,如:建構子、 openConnection 、 openStream 等方法,提供範例參考。