我试图在golang中的请求中使用user:pass proxy,当我运行时,我得到了"需要代理身份验证">
这是我的代码:
var ip string = "123.45.67.890:54400:user:pass"
func main() {
proxy := "http://" + ip
proxyURL, err := url.Parse(proxy)
if err != nil {
log.Fatal(err)
}
urlStr := site
url, err := url.Parse(urlStr)
if err != nil {
log.Fatal(err)
}
transport := &http.Transport{
Proxy: http.ProxyURL(proxyURL),
}
jar, _ := cookiejar.New(nil)
client := &http.Client{
Jar: jar,
Transport: transport,
}
req, _ := http.NewRequest("GET", url.String(), nil)
if err != nil {
log.Fatal(err)
}
auth := "johndoe2x:johndoe2x"
basicAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
req.Header.Add("Proxy-Authorization", basicAuth)
req.Header.Add("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
req.Header.Add("accept-language", "en-US,en;q=0.9")
req.Header.Add("upgrade-insecure-requests", "1")
req.Header.Add("user-agent", userAgent)
res, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
log.Println("statusCode:", res.StatusCode)
data, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
log.Println(string(data))
}
同样,当我运行这个时,我得到Time_here Get "https://site_here/": Proxy Authentication Required
我已经被困在这个问题上几个小时了,我不确定如何在Go中做到这一点,我可以在JS中用var为用户和通行证做这件事,但这件事在Golang 中给我带来了一些麻烦
提前感谢!
您正在将代理身份验证标头添加到将发送到目标服务器的请求中。您要做的是将此标头添加到transport
。
用替换req.Header.Add("Proxy-Authorization", basicAuth)
transport.ProxyConnectHeader = http.Header{}
transport.ProxyConnectHeader.Add("Proxy-Authorization", basicAuth)