hutool.httpx_client.http_client¶
Module Contents¶
Classes¶
HTTP工具类,提供常用的HTTP操作方法 |
API¶
- class hutool.httpx_client.http_client.HttpUtil¶
HTTP工具类,提供常用的HTTP操作方法
- static is_https(url: str) bool¶
判断URL是否为HTTPS协议
- 参数:
url – 待判断的URL字符串
- 返回:
如果URL以 https:// 开头返回 True,否则返回 False
- static is_http(url: str) bool¶
判断URL是否为HTTP协议
- 参数:
url – 待判断的URL字符串
- 返回:
如果URL以 http:// 开头返回 True,否则返回 False
- static get(url: str, params: Optional[dict] = None, timeout: int = 30000, headers: Optional[dict] = None) str¶
发送GET请求并返回响应体字符串
- 参数:
url – 请求URL
params – 查询参数
timeout – 超时时间(毫秒)
headers – 请求头
- 返回:
响应体字符串
- static post(url: str, data=None, json_data=None, timeout: int = 30000, headers: Optional[dict] = None) str¶
发送POST请求并返回响应体字符串
- 参数:
url – 请求URL
data – 表单数据
json_data – JSON数据
timeout – 超时时间(毫秒)
headers – 请求头
- 返回:
响应体字符串
- static create_get(url: str) hutool.httpx_client.http_request.HttpRequest¶
创建GET请求对象
- 参数:
url – 请求URL
- 返回:
HttpRequest对象
- static create_post(url: str) hutool.httpx_client.http_request.HttpRequest¶
创建POST请求对象
- 参数:
url – 请求URL
- 返回:
HttpRequest对象
- static download_string(url: str, charset: str = 'utf-8') str¶
下载URL内容为字符串
- 参数:
url – 下载URL
charset – 字符集,默认utf-8
- 返回:
下载的字符串内容
- static download_file(url: str, dest: str) int¶
下载URL内容到文件
- 参数:
url – 下载URL
dest – 目标文件路径
- 返回:
下载的字节数
- static download_bytes(url: str) bytes¶
下载URL内容为字节数组
- 参数:
url – 下载URL
- 返回:
下载的字节数组
- static to_params(param_map: dict, charset: str = 'utf-8') str¶
将参数Map转换为URL查询字符串
- 参数:
param_map – 参数字典
charset – 字符集,默认utf-8
- 返回:
URL编码后的查询字符串,如 “key1=value1&key2=value2”
- static decode_param_map(params_str: str) Dict[str, str]¶
将URL查询字符串解码为单值参数字典
如果某个键对应多个值,只取第一个值。
- 参数:
params_str – URL查询字符串,如 “key1=value1&key2=value2”
- 返回:
参数字典,值为单个字符串
- static decode_params(params_str: str) Dict[str, List[str]]¶
将URL查询字符串解码为多值参数字典
- 参数:
params_str – URL查询字符串,如 “key=value1&key=value2”
- 返回:
参数字典,值为字符串列表
- static url_with_form(url: str, form: dict) str¶
将表单参数附加到URL上
- 参数:
url – 基础URL
form – 表单参数字典
- 返回:
附加参数后的完整URL
- static get_charset(content_type: str) str¶
从Content-Type中提取字符集
- 参数:
content_type – Content-Type头值,如 “text/html; charset=utf-8”
- 返回:
字符集名称,如 “utf-8”;如果未指定则返回 “utf-8”
- static encode_url(url_str: str) str¶
URL编码
- 参数:
url_str – 待编码的URL字符串
- 返回:
编码后的URL字符串
- static decode_url(url_str: str) str¶
URL解码
- 参数:
url_str – 待解码的URL字符串
- 返回:
解码后的URL字符串
- static download(url: str, dest=None, timeout: int = 60000) Union[int, bytes]¶
统一下载接口
如果dest为字符串路径,下载到文件并返回字节数。 如果dest为None,返回bytes。
- 参数:
url – 下载URL
dest – 目标文件路径或None
timeout – 超时时间(毫秒)
- 返回:
字节数或bytes
- static encode_params(params: dict, charset: str = 'utf-8') str¶
编码参数为URL查询字符串(to_params别名)
- 参数:
params – 参数字典
charset – 字符集
- 返回:
URL编码后的查询字符串
- static get_mime_type(content_type: str) str¶
从Content-Type获取MIME类型
- 参数:
content_type – Content-Type头值
- 返回:
MIME类型,如 ‘text/html’
- static build_basic_auth(username: str, password: str) str¶
构建Basic认证头
- 参数:
username – 用户名
password – 密码
- 返回:
Basic认证头值(如 ‘Basic dXNlcjpwYXNz’)
- static normalize_params(params: dict) dict¶
规范化参数(过滤None值)
- 参数:
params – 参数字典
- 返回:
过滤None值后的字典