const (
EnableHttpProxy = true
HttpProxyUrl = "http://127.0.0.1:8118"
)
func SetHttpProxy() (httpclient http.Client) {
ProxyURL, _ := url.Parse(HttpProxyUrl)
httpclient = http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(ProxyURL),
},
}
return httpclient
}
func main() {
var client http.Client
if EnableHttpProxy {
client = SetHttpProxy()
}
//生成要访问的url
url := "https://api.my.com"
//提交请求
reqest, err := http.NewRequest("GET", url, nil)
//增加header选项
reqest.Header.Add("sec-ch-ua", "Not A;Brand\";v=\"99\", \"Chromium\";v=\"98\", \"Google Chrome\";v=\"98\"")
reqest.Header.Add("sec-ch-ua-mobile", "?0")
reqest.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36")
if err != nil {
panic(err)
}
//处理返回结果
response, _ := client.Do(reqest)
defer response.Body.Close()