[英]Setting up proxy for HTTP client
I'm trying to setup the HTTP client so that it uses a proxy, however I cannot quite understand how to do it.我正在尝试设置 HTTP 客户端,以便它使用代理,但我不太明白该怎么做。 The documentation has multiple reference to "proxy" but none of the functions seem to allow to define the proxy.
该文档多次引用“代理”,但似乎没有任何功能允许定义代理。 What I need is something like this:
我需要的是这样的:
client := &http.Client{}
client.SetProxy("someip:someport") // pseudo code
resp, err := client.Get("http://example.com") // do request through proxy
Any idea how to do this in Go?知道如何在 Go 中执行此操作吗?
lukad is correct, you could set the HTTP_PROXY
environment variable, if you do this Go will use it by default. lukad 是正确的,您可以设置
HTTP_PROXY
环境变量,如果您这样做,Go 将默认使用它。
Bash:重击:
export HTTP_PROXY="http://proxyIp:proxyPort"
Go:去:
os.Setenv("HTTP_PROXY", "http://proxyIp:proxyPort")
You could also construct your own http.Client that MUST use a proxy regardless of the environment's configuration:您还可以构建自己的 http.Client,无论环境的配置如何,它都必须使用代理:
proxyUrl, err := url.Parse("http://proxyIp:proxyPort")
myClient := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)}}
This is useful if you can not depend on the environment's configuration, or do not want to modify it.如果您不能依赖环境的配置,或者不想修改它,这将很有用。
You could also modify the default transport used by the " net/http
" package.您还可以修改“
net/http
”包使用的默认传输。 This would affect your entire program (including the default HTTP client).这会影响您的整个程序(包括默认的 HTTP 客户端)。
proxyUrl, err := url.Parse("http://proxyIp:proxyPort")
http.DefaultTransport = &http.Transport{Proxy: http.ProxyURL(proxyUrl)}
Go will use the the proxy defined in the environment variable HTTP_PROXY
if it's set.如果设置了,Go 将使用环境变量
HTTP_PROXY
定义的代理。 Otherwise it will use no proxy.否则它将不使用代理。
You could do it like this:你可以这样做:
os.Setenv("HTTP_PROXY", "http://someip:someport")
resp, err := http.Get("http://example.com")
if err != nil {
panic(err)
}
May you could also try this:你也可以试试这个:
url_i := url.URL{}
url_proxy, _ := url_i.Parse(proxy_addr)
transport := http.Transport{}
transport.Proxy = http.ProxyURL(url_proxy)// set proxy
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} //set ssl
client := &http.Client{}
client.Transport = transport
resp, err := client.Get("http://example.com") // do request through proxy
If you run something like this:如果你运行这样的东西:
HTTP_PROXY=89.x.y.z path_to_program
Then the HTTP_PROXY setting is set for that command only, which is useful if you don't want to set it for the whole shell session.然后只为该命令设置 HTTP_PROXY 设置,如果您不想为整个 shell 会话设置它,这很有用。 Note: there's no ;
注意:没有; between the setting and the path;
在设置和路径之间; if you put a semicolon, it would set (but not export) HTTP_PROXY for that shell
如果你放一个分号,它会为那个 shell 设置(但不导出)HTTP_PROXY
For an alternative way, you can also use GoRequest which has a feature that you can set proxy easily for any single request.对于另一种方式,您还可以使用GoRequest ,它具有可以轻松为任何单个请求设置代理的功能。
request := gorequest.New()
resp, body, errs:= request.Proxy("http://proxy:999").Get("http://example.com").End()
resp2, body2, errs2 := request.Proxy("http://proxy2:999").Get("http://example2.com").End()
Or you can set for the whole at once.或者您可以一次设置为整体。
request := gorequest.New().Proxy("http://proxy:999")
resp, body, errs:= request.Get("http://example.com").End()
resp2, body2, errs2 := request.Get("http://example2.com").End()
The Go built-in proxy use is briefly documented in the DefaultTransport
:在
DefaultTransport
简要记录了 Go 内置代理的使用:
// DefaultTransport .... It uses HTTP proxies
// as directed by the $HTTP_PROXY and $NO_PROXY (or $http_proxy and
// $no_proxy) environment variables.
var DefaultTransport RoundTripper = &Transport{
Proxy: ProxyFromEnvironment,
This points to the usefulness of creating custom Transports from DefaultTransport
vs. from scratch to take advantage of the built-in ProxyFromEnvironment
function这表明从
DefaultTransport
与从头开始创建自定义传输以利用内置的ProxyFromEnvironment
函数的ProxyFromEnvironment
Adding to accepted answer.添加到接受的答案。 If your proxy requires an username and password along with IP, the
如果您的代理需要用户名和密码以及 IP,则
export HTTP_PROXY="http://proxyIp:proxyPort"
becomes成为
export HTTP_PROXY="http://PROXY_LOGIN:PROXY_PASS@proxyIp:proxyPort"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.