Documentation
¶
Overview ¶
Package netutils 提供网络相关的工具函数
Index ¶
- func ExtractHostPort(urlStr string) (string, int, error)
- func IsPortOpen(host string, port int, timeout time.Duration) (bool, error)
- func IsURLReachable(urlStr string, timeout time.Duration) (bool, int, error)
- func IsValidDomain(domain string) bool
- func IsValidIP(ipStr string) bool
- func IsValidIPv4(ipStr string) bool
- func IsValidIPv6(ipStr string) bool
- type HTTPClient
- func (c *HTTPClient) Get(url string, headers map[string]string) (*HTTPResponse, error)
- func (c *HTTPClient) GetJSON(url string, headers map[string]string, result interface{}) error
- func (c *HTTPClient) Post(url string, headers map[string]string, body []byte) (*HTTPResponse, error)
- func (c *HTTPClient) PostJSON(url string, headers map[string]string, data interface{}) (*HTTPResponse, error)
- type HTTPResponse
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExtractHostPort ¶
ExtractHostPort 从URL中提取主机名和端口号 参数:
- urlStr: URL字符串
返回:
- string: 主机名
- int: 端口号
- error: 如果解析失败则返回错误信息
func IsPortOpen ¶
IsPortOpen 检查指定IP和端口是否开放 参数:
- host: 主机名或IP地址
- port: 端口号
- timeout: 连接超时时间
返回:
- bool: 端口是否开放
- error: 如果发生错误则返回错误信息
func IsURLReachable ¶
IsURLReachable 检查URL是否可访问 参数:
- urlStr: 要检查的URL
- timeout: 请求超时时间
返回:
- bool: URL是否可访问
- int: HTTP状态码 (如果请求成功)
- error: 如果发生错误则返回错误信息
Types ¶
type HTTPClient ¶
type HTTPClient struct {
// contains filtered or unexported fields
}
HTTPClient 是一个简化的HTTP客户端
func NewHTTPClient ¶
func NewHTTPClient(timeout time.Duration) *HTTPClient
NewHTTPClient 创建一个新的HTTP客户端 参数:
- timeout: 请求超时时间
返回:
- *HTTPClient: 新创建的HTTP客户端
func (*HTTPClient) Get ¶
func (c *HTTPClient) Get(url string, headers map[string]string) (*HTTPResponse, error)
Get 发送GET请求 参数:
- url: 请求URL
- headers: 请求头
返回:
- *HTTPResponse: HTTP响应
- error: 如果发生错误则返回错误信息
func (*HTTPClient) GetJSON ¶
func (c *HTTPClient) GetJSON(url string, headers map[string]string, result interface{}) error
GetJSON 发送GET请求并将响应解析为JSON 参数:
- url: 请求URL
- headers: 请求头
- result: 用于存储解析结果的对象指针
返回:
- error: 如果发生错误则返回错误信息
func (*HTTPClient) Post ¶
func (c *HTTPClient) Post(url string, headers map[string]string, body []byte) (*HTTPResponse, error)
Post 发送POST请求 参数:
- url: 请求URL
- headers: 请求头
- body: 请求体
返回:
- *HTTPResponse: HTTP响应
- error: 如果发生错误则返回错误信息
func (*HTTPClient) PostJSON ¶
func (c *HTTPClient) PostJSON(url string, headers map[string]string, data interface{}) (*HTTPResponse, error)
PostJSON 发送JSON格式的POST请求 参数:
- url: 请求URL
- headers: 请求头
- data: 要发送的数据对象,将被转换为JSON
返回:
- *HTTPResponse: HTTP响应
- error: 如果发生错误则返回错误信息
Click to show internal directories.
Click to hide internal directories.