Class UnoUrl

java.lang.Object
org.miaixz.bus.http.UnoUrl

public class UnoUrl extends Object
统一资源定位器(URL),其模式为httphttps。使用这个类来组合和分解Internet地址 这个类有一个现代的API。它避免了惩罚性的检查异常:get()对无效的输入抛出IllegalArgumentException, 或者parse()如果输入是无效的URL,则返回null。您甚至可以明确每个组件是否已经编码
Since:
Java 17+
Author:
Kimi Liu
  • Field Details

  • Method Details

    • defaultPort

      public static int defaultPort(String scheme)
      Returns 80 if scheme.equals("http"), 443 if scheme.equals("https") and -1 otherwise.
    • parse

      public static UnoUrl parse(String url)
      Returns a new HttpUrl representing url if it is a well-formed HTTP or HTTPS URL, or null if it isn't.
    • get

      public static UnoUrl get(String url)
      Returns a new HttpUrl representing url.
      Throws:
      IllegalArgumentException - If url is not a well-formed HTTP or HTTPS URL.
    • get

      public static UnoUrl get(URL url)
      Returns an UnoUrl for url if its protocol is http or https, or null if it has any other protocol.
    • get

      public static UnoUrl get(URI uri)
    • percentDecode

      public static String percentDecode(String encoded, boolean plusIsSpace)
    • canonicalize

      public static String canonicalize(String input, String encodeSet, boolean alreadyEncoded, boolean strict, boolean plusIsSpace, boolean asciiOnly, Charset charset)
    • url

      public URL url()
      Returns this URL as a java.net.URL.
    • uri

      public URI uri()
      Returns this URL as a java.net.URI. Because URI is more strict than this class, the returned URI may be semantically different from this URL:
      • Characters forbidden by URI like [ and | will be escaped.
      • Invalid percent-encoded sequences like %xx will be encoded like %25xx.
      • Whitespace and control characters in the fragment will be stripped.

      These differences may have a significant consequence when the URI is interpreted by a webserver. For this reason the URI class and this method should be avoided.

    • scheme

      public String scheme()
      Returns either "http" or "https".
    • isHttps

      public boolean isHttps()
    • encodedUsername

      public String encodedUsername()
      Returns the username, or an empty string if none is set.
    • username

      public String username()
      返回已解码的用户名,如果不存在,则返回空字符串.
      • http://host/""
      • http://username@host/"username"
      • http://username:password@host/"username"
      • http://a%20b:c%20d@host/"a b"
      Returns:
      用户信息
    • encodedPassword

      public String encodedPassword()
      返回密码,如果没有设置则返回空字符串.
      • http://host/""
      • http://username@host/""
      • http://username:password@host/"password"
      • http://a%20b:c%20d@host/"c%20d"
      Returns:
      返回密码
    • password

      public String password()
      返回已解码的密码,如果不存在,则返回空字符串.
      • http://host/""
      • http://username@host/""
      • http://username:password@host/"password"
      • http://a%20b:c%20d@host/"c d"
      Returns:
      返回已解码的密码
    • host

      public String 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
    • port

      public int port()
      • http://host/80
      • http://host:8000/8000
      • https://host/443
      Returns:
      端口
    • pathSize

      public int pathSize()
      Returns the number of segments in this URL's path. This is also the number of slashes in the URL's path, like 3 in http://host/a/b/c. This is always at least 1.
      • http://host/1
      • http://host/a/b/c3
      • http://host/a/b/c/4
      Returns:
      the size
    • encodedPath

      public String encodedPath()
      该URL编码后用于HTTP资源解析。返回的路径将以/开始
      • http://host//
      • http://host/a/b/c"/a/b/c"
      • http://host/a/b%20c/d"/a/b%20c/d"
      Returns:
      URL的完整路径
    • encodedPathSegments

      public List<String> encodedPathSegments()
      返回一个已编码的路径段列表 ["a", "b", "c"] for the URL http://host/a/b/c. 这个列表从不为空,尽管它可能包含一个空字符串.
      • http://host/[""]
      • http://host/a/b/c["a", "b", "c"]
      • http://host/a/b%20c/d["a", "b%20c", "d"]
      Returns:
      路径段列表
    • pathSegments

      public List<String> pathSegments()
      Returns a list of path segments like ["a", "b", "c"] for the URL http://host/a/b/c. This list is never empty though it may contain a single empty string.
      • http://host/[""]
      • http://host/a/b/c"["a", "b", "c"]
      • http://host/a/b%20c/d"["a", "b c", "d"]
      Returns:
      the string
    • encodedQuery

      public String encodedQuery()
      Returns the query of this URL, encoded for use in HTTP resource resolution. The returned string may be null (for URLs with no query), empty (for URLs with an empty query) or non-empty (all other URLs).
      • http://host/null
      • http://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

      public String query()
      Returns this URL's query, like "abc" for http://host/?abc. Most callers should prefer queryParameterName(int) and queryParameterValue(int) because these methods offer direct access to individual query parameters.
      • http://host/null
      • http://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()
      Returns the number of query parameters in this URL, like 2 for http://host/?a=apple&b=banana. If this URL has no query this returns 0. Otherwise it returns one more than the number of "&" separators in the query.
      • http://host/0
      • http://host/?1
      • http://host/?a=apple&k=key+lime2
      • http://host/?a=apple&a=apricot2
      • http://host/?a=apple&b2
      Returns:
      the int
    • queryParameter

      public String queryParameter(String name)
      Returns the first query parameter named name decoded using UTF-8, or null if there is no such query parameter.
      • http://host/null
      • http://host/?null
      • http://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

      public Set<String> queryParameterNames()
      Returns the distinct query parameter names in this URL, like ["a", "b"] for http://host/?a=apple&b=banana. If this URL has no query this returns the empty set.
      • 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

      public List<String> queryParameterValues(String name)
      Returns all values for the query parameter name ordered by their appearance in this URL. For example this returns ["banana"] for queryParameterValue("b") on http://host/?a=apple&b=banana.
      • 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

      public String queryParameterName(int index)
      Returns the name of the query parameter at index. For example this returns "a" for queryParameterName(0) on http://host/?a=apple&b=banana. This throws if index is not less than the query size.
      • http://host/exceptionexception
      • http://host/?""exception
      • http://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:
      the string
    • queryParameterValue

      public String queryParameterValue(int index)
      Returns the value of the query parameter at index. For example this returns "apple" for queryParameterName(0) on http://host/?a=apple&b=banana. This throws if index is not less than the query size.
      • http://host/exceptionexception
      • http://host/?nullexception
      • http://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:
      the string
    • encodedFragment

      public String encodedFragment()
      返回这个URL的片段 "abc" for http://host/#abc. 如果URL没有片段,则返回null
      • http://host/null
      • http://host/#""
      • http://host/#abc"abc"
      • http://host/#abc|def"abc|def"
      Returns:
      the string
    • fragment

      public String fragment()
      返回这个URL的片段 "abc" for http://host/#abc. 如果URL没有片段,则返回null
      • http://host/null
      • http://host/#""
      • http://host/#abc"abc"
      • http://host/#abc|def"abc|def"
      Returns:
      the string
    • redact

      public String redact()
    • resolve

      public UnoUrl resolve(String link)
    • newBuilder

      public UnoUrl.Builder newBuilder()
    • newBuilder

      public UnoUrl.Builder newBuilder(String link)
      Returns a builder for the URL that would be retrieved by following link from this URL, or null if the resulting URL is not well-formed.
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object