1 Star 1 Fork 1

xiaoyutab/xgotool

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
new.go 1.71 KB
Copy Edit Raw Blame History
xiaoyutab authored 2025-05-20 16:31 +08:00 . 1
package https
import (
"context"
"net/url"
"regexp"
"strings"
"time"
)
// 声明URL请求结构信息
//
// uri 要请求的网址信息
// opts 可选参数,针对此网址进行的配置项追加
func New(uri string, opts ...OptionFunc) *CURL {
// 默认配置项加载
opt := Option{
timeOut: _default.timeOut,
cacheTime: _default.cacheTime,
jar: getJar(),
context: context.TODO(),
}
// 配置项设置
for _, optFunc := range opts {
optFunc(&opt)
}
// 检测URL是否有多余的斜杠,如果有则进行处理
if strings.Contains(uri, "//") {
inf, err := regexp.Compile(`\/+`)
if err == nil {
// 去除多余的斜杠
uri = inf.ReplaceAllString(uri, "/")
// 补全协议头
uri = strings.ReplaceAll(uri, ":/", "://")
}
}
curls := CURL{
URI: uri, // 请求URL
option: &opt, // 配置项
CreateTime: time.Now(), // 创建时间
HeaderQuest: map[string]string{}, // 直接创建请求头map,避免每次都if判断
ParamQuest: map[string]string{}, // 直接创建请求参数map,避免每次都if判断
}
// 设置网络请求时的 referer header头信息【网页来源,一般用于防盗链】
if u, err := url.Parse(uri); err == nil {
curls.HeaderKV("origin", u.Scheme+"://"+u.Host)
curls.HeaderKV("referer", u.Scheme+"://"+u.Host)
}
// 声明配置项的值
return &curls
}
// 设置Option选项[此参数需要在Get/Post/PostJson之前进行调用,若在之后调用的话会失去作用]
//
// opts 可选参数,针对此网址进行的配置项追加
func (c *CURL) WithOption(opts ...OptionFunc) *CURL {
// 配置项设置
for _, optFunc := range opts {
optFunc(c.option)
}
return c
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/xiaoyutab/xgotool.git
git@gitee.com:xiaoyutab/xgotool.git
xiaoyutab
xgotool
xgotool
v0.3.66

Search