public final class UrlBuilder extends Object implements Builder<String>
[scheme:]scheme-specific-part[#fragment] [scheme:][//authority][path][?query][#fragment] [scheme:][//host:port][path][?query][#fragment]
Constructor and Description |
---|
UrlBuilder()
构造
|
UrlBuilder(String scheme,
String host,
int port,
UrlPath path,
UrlQuery query,
String fragment,
Charset charset)
构造
|
Modifier and Type | Method and Description |
---|---|
UrlBuilder |
addPath(CharSequence path)
增加路径,在现有路径基础上追加路径
|
UrlBuilder |
addPathSegment(CharSequence segment)
增加路径节点,路径节点中的"/"会被转义为"%2F"
|
UrlBuilder |
addQuery(String key,
Object value)
添加查询项,支持重复键
|
UrlBuilder |
appendPath(CharSequence path)
Deprecated.
方法重复,请使用
addPath(CharSequence) |
String |
build()
创建URL字符串
|
static UrlBuilder |
create()
Deprecated.
请使用
of() |
String |
getAuthority()
获得authority部分
|
Charset |
getCharset()
获取编码,用于URLEncode和URLDecode
|
String |
getFragment()
获取标识符,#后边的部分
|
String |
getFragmentEncoded()
获取标识符,#后边的部分
|
String |
getHost()
获取 主机,例如127.0.0.1
|
UrlPath |
getPath()
获取路径,例如/aa/bb/cc
|
String |
getPathStr()
获得路径,例如/aa/bb/cc
|
int |
getPort()
获取端口,默认-1
|
int |
getPortWithDefault()
获取端口,如果未自定义返回协议默认端口
|
UrlQuery |
getQuery()
获取查询语句,例如a=1&b=2
可能为 null |
String |
getQueryStr()
获取查询语句,例如a=1&b=2
|
String |
getScheme()
获取协议,例如http
|
String |
getSchemeWithDefault()
获取协议,例如http,如果用户未定义协议,使用默认的http协议
|
static UrlBuilder |
of()
创建空的UrlBuilder
|
static UrlBuilder |
of(String url)
使用URL字符串构建UrlBuilder,默认使用UTF-8编码
|
static UrlBuilder |
of(String url,
Charset charset)
使用URL字符串构建UrlBuilder
|
static UrlBuilder |
of(String scheme,
String host,
int port,
String path,
String query,
String fragment,
Charset charset)
构建UrlBuilder
|
static UrlBuilder |
of(String scheme,
String host,
int port,
UrlPath path,
UrlQuery query,
String fragment,
Charset charset)
构建UrlBuilder
|
static UrlBuilder |
of(URI uri,
Charset charset)
使用URI构建UrlBuilder
|
static UrlBuilder |
of(URL url,
Charset charset)
使用URL构建UrlBuilder
|
static UrlBuilder |
ofHttp(String httpUrl)
使用URL字符串构建UrlBuilder,当传入的URL没有协议时,按照http协议对待,编码默认使用UTF-8
|
static UrlBuilder |
ofHttp(String httpUrl,
Charset charset)
使用URL字符串构建UrlBuilder,当传入的URL没有协议时,按照http协议对待。
|
static UrlBuilder |
ofHttpWithoutEncode(String httpUrl)
使用URL字符串构建UrlBuilder,当传入的URL没有协议时,按照http协议对待
此方法不对URL编码 |
UrlBuilder |
setCharset(Charset charset)
设置编码,用于URLEncode和URLDecode
|
UrlBuilder |
setFragment(String fragment)
设置标识符,例如#后边的部分
|
UrlBuilder |
setHost(String host)
设置主机,例如127.0.0.1
|
UrlBuilder |
setPath(UrlPath path)
设置路径,例如/aa/bb/cc,将覆盖之前所有的path相关设置
|
UrlBuilder |
setPort(int port)
设置端口,默认-1
|
UrlBuilder |
setQuery(UrlQuery query)
设置查询语句,例如a=1&b=2,将覆盖之前所有的query相关设置
|
UrlBuilder |
setScheme(String scheme)
设置协议,例如http
|
UrlBuilder |
setWithEndTag(boolean withEngTag)
是否path的末尾加 /
|
String |
toString() |
URI |
toURI()
转换为URI
|
URL |
toURL()
转换为
URL 对象 |
URL |
toURL(URLStreamHandler handler)
转换为
URL 对象 |
public UrlBuilder()
public UrlBuilder(String scheme, String host, int port, UrlPath path, UrlQuery query, String fragment, Charset charset)
scheme
- 协议,默认httphost
- 主机,例如127.0.0.1port
- 端口,-1表示默认端口path
- 路径,例如/aa/bb/ccquery
- 查询,例如a=1&b=2fragment
- 标识符例如#后边的部分charset
- 编码,用于URLEncode和URLDecode,null
表示不编码public static UrlBuilder of(URI uri, Charset charset)
uri
- URIcharset
- 编码,用于URLEncode和URLDecodepublic static UrlBuilder ofHttpWithoutEncode(String httpUrl)
httpUrl
- URL字符串public static UrlBuilder ofHttp(String httpUrl)
httpUrl
- URL字符串public static UrlBuilder ofHttp(String httpUrl, Charset charset)
httpUrl
- URL字符串charset
- 编码,用于URLEncode和URLDecodepublic static UrlBuilder of(String url)
url
- URL字符串public static UrlBuilder of(String url, Charset charset)
url
- URL字符串charset
- 编码,用于URLEncode和URLDecodepublic static UrlBuilder of(URL url, Charset charset)
url
- URLcharset
- 编码,用于URLEncode和URLDecodepublic static UrlBuilder of(String scheme, String host, int port, String path, String query, String fragment, Charset charset)
scheme
- 协议,默认httphost
- 主机,例如127.0.0.1port
- 端口,-1表示默认端口path
- 路径,例如/aa/bb/ccquery
- 查询,例如a=1&b=2fragment
- 标识符例如#后边的部分charset
- 编码,用于URLEncode和URLDecodepublic static UrlBuilder of(String scheme, String host, int port, UrlPath path, UrlQuery query, String fragment, Charset charset)
scheme
- 协议,默认httphost
- 主机,例如127.0.0.1port
- 端口,-1表示默认端口path
- 路径,例如/aa/bb/ccquery
- 查询,例如a=1&b=2fragment
- 标识符例如#后边的部分charset
- 编码,用于URLEncode和URLDecode@Deprecated public static UrlBuilder create()
of()
public static UrlBuilder of()
public String getScheme()
public String getSchemeWithDefault()
public UrlBuilder setScheme(String scheme)
scheme
- 协议,例如httppublic String getHost()
public UrlBuilder setHost(String host)
host
- 主机,例如127.0.0.1public int getPort()
public int getPortWithDefault()
public UrlBuilder setPort(int port)
port
- 端口,默认-1public String getAuthority()
public UrlPath getPath()
public String getPathStr()
public UrlBuilder setWithEndTag(boolean withEngTag)
withEngTag
- 是否path的末尾加 /public UrlBuilder setPath(UrlPath path)
path
- 路径,例如/aa/bb/ccpublic UrlBuilder addPath(CharSequence path)
path
- 路径,例如aaa/bbb/cccpublic UrlBuilder addPathSegment(CharSequence segment)
segment
- 路径节点@Deprecated public UrlBuilder appendPath(CharSequence path)
addPath(CharSequence)
path
- path节点public UrlQuery getQuery()
null
null
public String getQueryStr()
public UrlBuilder setQuery(UrlQuery query)
query
- 查询语句,例如a=1&b=2public UrlBuilder addQuery(String key, Object value)
key
- 键value
- 值public String getFragment()
public String getFragmentEncoded()
public UrlBuilder setFragment(String fragment)
fragment
- 标识符,例如#后边的部分public Charset getCharset()
public UrlBuilder setCharset(Charset charset)
charset
- 编码public String build()
public URL toURL(URLStreamHandler handler)
URL
对象handler
- URLStreamHandler
,null表示默认URL
public URI toURI()
Copyright © 2024. All rights reserved.