java.lang.Object
org.miaixz.bus.http.UnoUrl
HTTP 或 HTTPS 的统一资源定位器(URL)
提供对 URL 的构建、解析和编码处理,支持 HTTP 和 HTTPS 协议。 避免使用检查型异常,解析无效 URL 时返回 null 或抛出 IllegalArgumentException。 实例是不可变的,支持组件的编码和解码操作。
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String表单编码字符集static final String片段编码字符集static final StringURI 片段编码字符集static final String密码编码字符集static final String路径段编码字符集static final StringURI 路径段编码字符集static final String查询组件编码字符集static final StringURI 查询组件编码字符集static final String查询组件重新编码字符集static final String查询参数编码字符集static final String用户名编码字符集 -
Method Summary
Modifier and TypeMethodDescriptionstatic Stringcanonicalize(String input, int pos, int limit, String encodeSet, boolean alreadyEncoded, boolean strict, boolean plusIsSpace, boolean asciiOnly, Charset charset) 规范化字符串static Stringcanonicalize(String input, String encodeSet, boolean alreadyEncoded, boolean strict, boolean plusIsSpace, boolean asciiOnly) 规范化字符串(默认 UTF-8)static Stringcanonicalize(String input, String encodeSet, boolean alreadyEncoded, boolean strict, boolean plusIsSpace, boolean asciiOnly, Charset charset) 规范化字符串(默认 UTF-8)static voidcanonicalize(org.miaixz.bus.core.io.buffer.Buffer out, String input, int pos, int limit, String encodeSet, boolean alreadyEncoded, boolean strict, boolean plusIsSpace, boolean asciiOnly, Charset charset) 规范化字符串到缓冲区static intdefaultPort(String scheme) 获取默认端口号获取编码后的片段http://host/nullhttp://host/#""http://host/#abc"abc"http://host/#abc|def"abc|def"获取编码后的密码http://host/""http://username@host/""http://username:password@host/"password"http://a%20b:c%20d@host/"c%20d"获取编码后的路径http://host//http://host/a/b/c"/a/b/c"http://host/a/b%20c/d"/a/b%20c/d"获取编码后的路径段列表http://host/[""]http://host/a/b/c["a", "b", "c"]http://host/a/b%20c/d["a", "b%20c", "d"]获取编码后的查询字符串http://host/nullhttp://host/?""http://host/?a=apple&k=key+lime"a=apple&k=key+lime"http://host/?a=apple&a=apricot"a=apple&a=apricot"http://host/?a=apple&b"a=apple&b"获取编码后的用户名boolean比较两个 UnoUrl 对象是否相等fragment()获取解码后的片段http://host/nullhttp://host/#""http://host/#abc"abc"http://host/#abc|def"abc|def"static UnoUrl构建 UnoUrl 实例static UnoUrl从 URI 对象构建 UnoUrl 实例static UnoUrl从 URL 对象构建 UnoUrl 实例inthashCode()计算 URL 的哈希码host()获取主机名 A regular host name, likeandroid.com.booleanisHttps()检查是否为 HTTPS 协议创建新的 Builder 实例newBuilder(String link) 创建相对链接的 Builder 实例static UnoUrl解析 URL 字符串为 UnoUrl 实例password()获取解码后的密码http://host/""http://username@host/""http://username:password@host/"password"http://a%20b:c%20d@host/"c d"获取解码后的路径段列表http://host/[""]http://host/a/b/c"["a", "b", "c"]http://host/a/b%20c/d"["a", "b c", "d"]intpathSize()获取路径段数量http://host/1http://host/a/b/c3http://host/a/b/c/4static StringpercentDecode(String encoded, boolean plusIsSpace) 解码百分比编码字符串static StringpercentDecode(String encoded, int pos, int limit, boolean plusIsSpace) 解码百分比编码字符串(指定范围)static voidpercentDecode(org.miaixz.bus.core.io.buffer.Buffer out, String encoded, int pos, int limit, boolean plusIsSpace) 解码百分比编码字符串到缓冲区static booleanpercentEncoded(String encoded, int pos, int limit) 检查字符串是否为百分比编码intport()获取端口号http://host/80http://host:8000/8000https://host/443query()获取解码后的查询字符串http://host/nullhttp://host/?""http://host/?a=apple&k=key+lime"a=apple&k=key lime"http://host/?a=apple&a=apricot"a=apple&a=apricot"http://host/?a=apple&b"a=apple&b"queryParameter(String name) 获取指定名称的第一个查询参数值http://host/nullhttp://host/?nullhttp://host/?a=apple&k=key+lime"apple"http://host/?a=apple&a=apricot"apple"http://host/?a=apple&b"apple"queryParameterName(int index) 获取指定索引的查询参数名称http://host/exceptionexceptionhttp://host/?""exceptionhttp://host/?a=apple&k=key+lime"a""k"http://host/?a=apple&a=apricot"a""a"http://host/?a=apple&b"a""b"获取查询参数名称集合http://host/[]http://host/?[""]http://host/?a=apple&k=key+lime["a", "k"]http://host/?a=apple&a=apricot["a"]http://host/?a=apple&b["a", "b"]queryParameterValue(int index) 获取指定索引的查询参数值http://host/exceptionexceptionhttp://host/?nullexceptionhttp://host/?a=apple&k=key+lime"apple""key lime"http://host/?a=apple&a=apricot"apple""apricot"http://host/?a=apple&b"apple"nullqueryParameterValues(String name) 获取指定名称的所有查询参数值http://host/[][]http://host/?[][]http://host/?a=apple&k=key+lime["apple"][]http://host/?a=apple&a=apricot["apple", "apricot"][]http://host/?a=apple&b["apple"][null]int获取查询参数数量http://host/0http://host/?1http://host/?a=apple&k=key+lime2http://host/?a=apple&a=apricot2http://host/?a=apple&b2redact()返回隐藏敏感信息的 URL解析相对链接scheme()获取协议toString()返回 URL 的字符串表示uri()转换为 java.net.URI 对象url()转换为 java.net.URL 对象username()获取解码后的用户名http://host/""http://username@host/"username"http://username:password@host/"username"http://a%20b:c%20d@host/"a b"
-
Field Details
-
USERNAME_ENCODE_SET
用户名编码字符集- See Also:
-
PASSWORD_ENCODE_SET
密码编码字符集- See Also:
-
PATH_SEGMENT_ENCODE_SET
路径段编码字符集- See Also:
-
PATH_SEGMENT_ENCODE_SET_URI
URI 路径段编码字符集- See Also:
-
QUERY_ENCODE_SET
查询参数编码字符集- See Also:
-
QUERY_COMPONENT_REENCODE_SET
查询组件重新编码字符集- See Also:
-
QUERY_COMPONENT_ENCODE_SET
查询组件编码字符集- See Also:
-
QUERY_COMPONENT_ENCODE_SET_URI
URI 查询组件编码字符集- See Also:
-
FORM_ENCODE_SET
表单编码字符集- See Also:
-
FRAGMENT_ENCODE_SET
片段编码字符集- See Also:
-
FRAGMENT_ENCODE_SET_URI
URI 片段编码字符集- See Also:
-
-
Method Details
-
defaultPort
获取默认端口号返回 80(http)、443(https)或 -1(其他协议)。
- Parameters:
scheme- 协议名称- Returns:
- 默认端口号
-
parse
解析 URL 字符串为 UnoUrl 实例如果 URL 格式有效,返回 UnoUrl 实例;否则返回 null。
- Parameters:
url- URL 字符串- Returns:
- UnoUrl 实例或 null
-
get
构建 UnoUrl 实例如果 URL 格式无效,抛出 IllegalArgumentException。
- Parameters:
url- URL 字符串- Returns:
- UnoUrl 实例
- Throws:
IllegalArgumentException- 如果 URL 格式无效
-
get
从 URL 对象构建 UnoUrl 实例仅支持 http 和 https 协议,非有效协议返回 null。
- Parameters:
url- URL 对象- Returns:
- UnoUrl 实例或 null
-
get
从 URI 对象构建 UnoUrl 实例- Parameters:
uri- URI 对象- Returns:
- UnoUrl 实例或 null
-
percentDecode
解码百分比编码字符串- Parameters:
encoded- 编码字符串plusIsSpace- 是否将加号解码为空格- Returns:
- 解码后的字符串
-
percentDecode
解码百分比编码字符串(指定范围)- Parameters:
encoded- 编码字符串pos- 起始位置limit- 结束位置plusIsSpace- 是否将加号解码为空格- Returns:
- 解码后的字符串
-
percentDecode
public static void percentDecode(org.miaixz.bus.core.io.buffer.Buffer out, String encoded, int pos, int limit, boolean plusIsSpace) 解码百分比编码字符串到缓冲区- Parameters:
out- 输出缓冲区encoded- 编码字符串pos- 起始位置limit- 结束位置plusIsSpace- 是否将加号解码为空格
-
percentEncoded
检查字符串是否为百分比编码- Parameters:
encoded- 编码字符串pos- 起始位置limit- 结束位置- Returns:
- true 如果是有效的百分比编码
-
canonicalize
public static String canonicalize(String input, int pos, int limit, String encodeSet, boolean alreadyEncoded, boolean strict, boolean plusIsSpace, boolean asciiOnly, Charset charset) 规范化字符串跳过控制字符,编码指定字符集中的字符,处理百分比编码和加号。
- Parameters:
input- 输入字符串pos- 起始位置limit- 结束位置encodeSet- 编码字符集alreadyEncoded- 是否已编码strict- 是否严格编码plusIsSpace- 是否将加号编码为空格asciiOnly- 是否仅限 ASCIIcharset- 字符集(null 为 UTF-8)- Returns:
- 规范化字符串
-
canonicalize
public static void canonicalize(org.miaixz.bus.core.io.buffer.Buffer out, String input, int pos, int limit, String encodeSet, boolean alreadyEncoded, boolean strict, boolean plusIsSpace, boolean asciiOnly, Charset charset) 规范化字符串到缓冲区- Parameters:
out- 输出缓冲区input- 输入字符串pos- 起始位置limit- 结束位置encodeSet- 编码字符集alreadyEncoded- 是否已编码strict- 是否严格编码plusIsSpace- 是否将加号编码为空格asciiOnly- 是否仅限 ASCIIcharset- 字符集(null 为 UTF-8)
-
canonicalize
public static String canonicalize(String input, String encodeSet, boolean alreadyEncoded, boolean strict, boolean plusIsSpace, boolean asciiOnly, Charset charset) 规范化字符串(默认 UTF-8)- Parameters:
input- 输入字符串encodeSet- 编码字符集alreadyEncoded- 是否已编码strict- 是否严格编码plusIsSpace- 是否将加号编码为空格asciiOnly- 是否仅限 ASCIIcharset- 字符集(null 为 UTF-8)- Returns:
- 规范化字符串
-
canonicalize
public static String canonicalize(String input, String encodeSet, boolean alreadyEncoded, boolean strict, boolean plusIsSpace, boolean asciiOnly) 规范化字符串(默认 UTF-8)- Parameters:
input- 输入字符串encodeSet- 编码字符集alreadyEncoded- 是否已编码strict- 是否严格编码plusIsSpace- 是否将加号编码为空格asciiOnly- 是否仅限 ASCII- Returns:
- 规范化字符串
-
url
转换为 java.net.URL 对象- Returns:
- URL 对象
- Throws:
RuntimeException- 如果 URL 格式无效
-
uri
转换为 java.net.URI 对象注意:URI 比 UnoUrl 更严格,可能对某些字符进行转义或移除(如片段中的空白)。 建议避免直接使用 URI,以免服务器解释差异。
- Returns:
- URI 对象
- Throws:
RuntimeException- 如果 URI 格式无效
-
scheme
获取协议- Returns:
- 协议(http 或 https)
-
isHttps
public boolean isHttps()检查是否为 HTTPS 协议- Returns:
- true 如果协议为 HTTPS
-
encodedUsername
获取编码后的用户名- Returns:
- 编码后的用户名(空字符串如果未设置)
-
username
获取解码后的用户名http://host/""http://username@host/"username"http://username:password@host/"username"http://a%20b:c%20d@host/"a b"
- Returns:
- 用户信息
-
encodedPassword
获取编码后的密码http://host/""http://username@host/""http://username:password@host/"password"http://a%20b:c%20d@host/"c%20d"
- Returns:
- 返回密码
-
password
获取解码后的密码http://host/""http://username@host/""http://username:password@host/"password"http://a%20b:c%20d@host/"c d"
- Returns:
- 返回已解码的密码
-
host
获取主机名- A regular host name, like
android.com. - An IPv4 address, like
127.0.0.1. - An IPv6 address, like
::1. - An encoded IDN, like
xn--n3h.net.
http://android.com/"android.com"http://127.0.0.1/"127.0.0.1"http://[::1]/"::1"http://xn--n3h.net/"xn--n3h.net"
- Returns:
- 主机host
- A regular host name, like
-
port
public int port()获取端口号http://host/80http://host:8000/8000https://host/443
- Returns:
- 端口
-
pathSize
public int pathSize()获取路径段数量http://host/1http://host/a/b/c3http://host/a/b/c/4
- Returns:
- the size
-
encodedPath
获取编码后的路径http://host//http://host/a/b/c"/a/b/c"http://host/a/b%20c/d"/a/b%20c/d"
- Returns:
- URL的完整路径
-
encodedPathSegments
获取编码后的路径段列表http://host/[""]http://host/a/b/c["a", "b", "c"]http://host/a/b%20c/d["a", "b%20c", "d"]
- Returns:
- 路径段列表
-
pathSegments
获取解码后的路径段列表http://host/[""]http://host/a/b/c"["a", "b", "c"]http://host/a/b%20c/d"["a", "b c", "d"]
- Returns:
- the string
-
encodedQuery
获取编码后的查询字符串http://host/nullhttp://host/?""http://host/?a=apple&k=key+lime"a=apple&k=key+lime"http://host/?a=apple&a=apricot"a=apple&a=apricot"http://host/?a=apple&b"a=apple&b"
- Returns:
- the string
-
query
获取解码后的查询字符串http://host/nullhttp://host/?""http://host/?a=apple&k=key+lime"a=apple&k=key lime"http://host/?a=apple&a=apricot"a=apple&a=apricot"http://host/?a=apple&b"a=apple&b"
- Returns:
- the string
-
querySize
public int querySize()获取查询参数数量http://host/0http://host/?1http://host/?a=apple&k=key+lime2http://host/?a=apple&a=apricot2http://host/?a=apple&b2
- Returns:
- the int
-
queryParameter
获取指定名称的第一个查询参数值http://host/nullhttp://host/?nullhttp://host/?a=apple&k=key+lime"apple"http://host/?a=apple&a=apricot"apple"http://host/?a=apple&b"apple"
- Parameters:
name- 名称- Returns:
- the string
-
queryParameterNames
获取查询参数名称集合http://host/[]http://host/?[""]http://host/?a=apple&k=key+lime["a", "k"]http://host/?a=apple&a=apricot["a"]http://host/?a=apple&b["a", "b"]
- Returns:
- the set
-
queryParameterValues
获取指定名称的所有查询参数值http://host/[][]http://host/?[][]http://host/?a=apple&k=key+lime["apple"][]http://host/?a=apple&a=apricot["apple", "apricot"][]http://host/?a=apple&b["apple"][null]
- Parameters:
name- 名称- Returns:
- the list
-
queryParameterName
获取指定索引的查询参数名称http://host/exceptionexceptionhttp://host/?""exceptionhttp://host/?a=apple&k=key+lime"a""k"http://host/?a=apple&a=apricot"a""a"http://host/?a=apple&b"a""b"
- Parameters:
index- 索引- Returns:
- 参数名称
- Throws:
IndexOutOfBoundsException- 如果索引无效
-
queryParameterValue
获取指定索引的查询参数值http://host/exceptionexceptionhttp://host/?nullexceptionhttp://host/?a=apple&k=key+lime"apple""key lime"http://host/?a=apple&a=apricot"apple""apricot"http://host/?a=apple&b"apple"null
- Parameters:
index- 索引- Returns:
- 参数值
- Throws:
IndexOutOfBoundsException- 如果索引无效
-
encodedFragment
获取编码后的片段http://host/nullhttp://host/#""http://host/#abc"abc"http://host/#abc|def"abc|def"
- Returns:
- 编码后的片段(可能为 null)
-
fragment
获取解码后的片段http://host/nullhttp://host/#""http://host/#abc"abc"http://host/#abc|def"abc|def"
- Returns:
- 解码后的片段(可能为 null)
-
redact
返回隐藏敏感信息的 URL- Returns:
- 隐藏用户名和密码的 URL 字符串
-
resolve
解析相对链接- Parameters:
link- 相对链接- Returns:
- 解析后的 UnoUrl 实例(无效时为 null)
-
newBuilder
创建新的 Builder 实例- Returns:
- Builder 实例
-
newBuilder
创建相对链接的 Builder 实例- Parameters:
link- 相对链接- Returns:
- Builder 实例(无效时为 null)
-
equals
比较两个 UnoUrl 对象是否相等 -
hashCode
public int hashCode()计算 URL 的哈希码 -
toString
返回 URL 的字符串表示
-