Using proxies with net/http in GO

huangapple go评论112阅读模式
英文:

Using proxies with net/http in GO

问题

我正在尝试使用net/http包来使用代理。我的代理是一个带有用户名、密码、代理地址和端口的轮换代理。我尝试使用os.setEnv()将其设置为环境变量,还将其添加到我的Windows 10环境变量中,但似乎Windows不支持用户密码验证的代理。我也尝试了http传输方法,但无法使其工作。

请问有人能指导我使用代理,特别是带有用户密码验证的代理的教程或文档吗?

注意:我在所有地方都使用了这种格式:username:password@proxyaddress:port

英文:

I'm trying to use proxies with the net/http package. My proxy is a rotating proxy with a Username, password, Proxy address and a port.
I tried setting it as an environment variable using os.setEnv() as well as adding it in my windows 10 env variables but turns out maybe windows does not support user-pass authenticated proxies.
I tried the http transport method too but could not get it to work

func SetProxy()	*http.Client {
	cfg := GetConfig()
	if cfg.UseProxy {
		proxyUrl, err := url.Parse("http://" + cfg.Proxy)
		if err != nil {
			panic(err)
		}
		myClient := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)}}
		return myClient
	}
	return &http.Client{}
}

Please if someone could point me to a tutorial or some documentation to use proxies specifically proxies with user-pass auth!

note: I used this format everywhere: username:password@proxyaddress:port

答案1

得分: 0

作为调试尝试,你可以打印client.Transport.Proxy函数;如果它不是nil,就调用它并打印它返回的内容。如果Proxy不是nil并且返回的不是nil*URL,那么就使用了代理。

英文:

As a debug attempt, you can print the client.Transport.Proxy function; if it's not nil, call it and print what it returns. If Proxy is not nil and returns not nil *URL, then proxy IS used.

huangapple
  • 本文由 发表于 2021年11月18日 21:44:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/70020954.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定