hutool.core.util.url¶
Module Contents¶
Classes¶
URL工具类,对应 Java cn.hutool.core.util.URLUtil |
API¶
- class hutool.core.util.url.URLUtil¶
URL工具类,对应 Java cn.hutool.core.util.URLUtil
- static normalize(url_str: str) str¶
标准化URL
补齐协议头(缺少时添加 http://)
将反斜杠替换为正斜杠
合并路径中的连续斜杠(保留协议后的双斜杠)
- 参数:
url_str – 原始URL字符串
- 返回:
标准化后的URL字符串
- static encode(url_str: str, charset: str = 'utf-8') str¶
URL编码
对整个URL字符串进行编码。
- 参数:
url_str – 待编码的URL字符串
charset – 字符编码,默认utf-8
- 返回:
编码后的URL字符串
- static decode(url_str: str, charset: str = 'utf-8') str¶
URL解码
对URL编码的字符串进行解码。
- 参数:
url_str – 待解码的URL字符串
charset – 字符编码,默认utf-8
- 返回:
解码后的URL字符串
- static encode_params(params_str: str, charset: str = 'utf-8') str¶
编码URL参数部分
对URL查询参数字符串中的值部分进行编码。
- 参数:
params_str – 参数字符串,如 “key1=value1&key2=value2”
charset – 字符编码,默认utf-8
- 返回:
编码后的参数字符串
- static get_path(url_str: str) str¶
获取URL的路径部分
- 参数:
url_str – URL字符串
- 返回:
URL的路径部分,解析失败返回空字符串
- static get_host(url_str: str) str¶
获取URL的主机部分
- 参数:
url_str – URL字符串
- 返回:
URL的主机名,解析失败返回空字符串
- static get_port(url_str: str) int¶
获取URL的端口号,无端口返回-1
- 参数:
url_str – URL字符串
- 返回:
端口号,无端口或解析失败返回-1
- static get_query(url_str: str) str¶
获取URL的查询参数部分
- 参数:
url_str – URL字符串
- 返回:
URL的查询参数字符串(不含?),解析失败返回空字符串
- static build_url(base: str, params: Dict[str, str]) str¶
构建带参数的URL
在基础URL上附加参数,如果原URL已有参数则追加。
- 参数:
base – 基础URL
params – 参数字典
- 返回:
带参数的完整URL
- static url_with_form(url: str, form: Dict[str, str]) str¶
在URL后附加表单参数
与build_url类似,将表单参数附加到URL后。
- 参数:
url – 原始URL
form – 表单参数字典
- 返回:
附加参数后的URL
- static to_params(param_map: Dict[str, str], charset: str = 'utf-8') str¶
参数Map转URL参数字符串
- 参数:
param_map – 参数字典
charset – 字符编码,默认utf-8
- 返回:
URL参数字符串,如 “key1=value1&key2=value2”
- static decode_param_map(params_str: str, charset: str = 'utf-8') Dict[str, str]¶
URL参数字符串转Map(每个key只保留最后一个值)
- 参数:
params_str – 参数字符串,如 “key1=value1&key2=value2”
charset – 字符编码,默认utf-8
- 返回:
参数字典,重复key时保留最后一个值
- static decode_params(params_str: str, charset: str = 'utf-8') Dict[str, List[str]]¶
URL参数字符串转Map(每个key对应值列表)
- 参数:
params_str – 参数字符串,如 “key=value1&key=value2”
charset – 字符编码,默认utf-8
- 返回:
参数字典,每个key对应一个值列表
- static is_https(url_str: str) bool¶
是否为HTTPS
- 参数:
url_str – URL字符串
- 返回:
是HTTPS返回True,否则返回False
- static is_http(url_str: str) bool¶
是否为HTTP
- 参数:
url_str – URL字符串
- 返回:
是HTTP返回True,否则返回False
- static build_query(params: Dict[str, str], is_encode: bool = True) str¶
将参数字典转换为查询字符串。
- 参数:
params – 参数字典
is_encode – 是否 URL 编码
- 返回:
查询字符串
- static encode_blank(url: str) str¶
编码 URL 中的空格(替换为 %20)。
- 参数:
url – URL 字符串
- 返回:
编码后的 URL
- static complete_url(base_url: str, relative_path: str) str¶
补全相对 URL。
- 参数:
base_url – 基础 URL
relative_path – 相对路径
- 返回:
完整 URL
- static get_params(url: str) Dict[str, str]¶
解析 URL 参数为字典。
- 参数:
url – URL 字符串
- 返回:
参数字典
- static get_data_uri_base64(mime_type: str, data: str) str¶
构建 base64 编码的 Data URI。
- 参数:
mime_type – MIME 类型,如
"image/png"data – base64 编码后的数据
- 返回:
Data URI 字符串,如
data:image/png;base64,iVBOR...
- static get_data_uri(mime_type: Optional[str] = None, encoding: Optional[str] = None, data: str = '', charset: Optional[str] = None) str¶
构建 Data URI。
Data URI 格式:
data:[<mime>][;charset=<charset>][;<encoding>],<data>- 参数:
mime_type – MIME 类型,如
"image/png",可为 Noneencoding – 编码方式,如
"base64",可为 Nonedata – 数据内容
charset – 字符集,如
"utf-8",可为 None
- 返回:
Data URI 字符串