为HTTP客户端设置代理

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

Setting up proxy for HTTP client

问题

我正在尝试设置HTTP客户端以使用代理,但是我无法完全理解如何做到这一点。文档中多次提到了“代理”,但是似乎没有任何函数允许定义代理。我需要的是像这样的东西:

client := &http.Client{}
client.SetProxy("someip:someport") // 伪代码
resp, err := client.Get("http://example.com") // 通过代理发送请求

有任何关于如何在Go中实现这个的想法吗?

英文:

I'm trying to setup the HTTP client so that it uses a proxy, however I cannot quite understand how to do it. 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?

答案1

得分: 153

lukad是正确的,你可以设置HTTP_PROXY环境变量,如果你这样做,Go将默认使用它。

Bash:

export HTTP_PROXY="http://proxyIp:proxyPort"

Go:

os.Setenv("HTTP_PROXY", "http://proxyIp:proxyPort")

你也可以构建自己的http.Client,无论环境的配置如何,它都必须使用代理:

proxyUrl, err := url.Parse("http://proxyIp:proxyPort")
myClient := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)}}

如果你不能依赖环境的配置,或者不想修改它,这很有用。

你也可以修改“net/http”包使用的默认传输方式。这将影响你的整个程序(包括默认的HTTP客户端)。

proxyUrl, err := url.Parse("http://proxyIp:proxyPort")
http.DefaultTransport = &http.Transport{Proxy: http.ProxyURL(proxyUrl)}
英文:

lukad is correct, you could set the HTTP_PROXY environment variable, if you do this Go will use it by default.

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:

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. This would affect your entire program (including the default HTTP client).

proxyUrl, err := url.Parse("http://proxyIp:proxyPort")
http.DefaultTransport = &http.Transport{Proxy: http.ProxyURL(proxyUrl)}

答案2

得分: 16

Go将使用环境变量HTTP_PROXY中定义的代理(如果已设置)。否则,它将不使用代理。

您可以像这样设置代理:

os.Setenv("HTTP_PROXY", "http://someip:someport")
resp, err := http.Get("http://example.com")
if err != nil {
	panic(err)
}
英文:

Go will use the the proxy defined in the environment variable HTTP_PROXY if it's set. 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)
}

答案3

得分: 7

你也可以尝试这样做:

url_i := url.URL{}
url_proxy, _ := url_i.Parse(proxy_addr)

transport := http.Transport{}    
transport.Proxy = http.ProxyURL(url_proxy)// 设置代理 
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} // 设置SSL

client := &http.Client{}
client.Transport = transport
resp, err := client.Get("http://example.com") // 通过代理发送请求
英文:

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

答案4

得分: 2

Go内置的代理使用在DefaultTransport中有简要的文档说明:

// 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,

这指出了使用DefaultTransport创建自定义传输方式与从头开始创建的有用之处,以便利用内置的ProxyFromEnvironment函数。

英文:

The Go built-in proxy use is briefly documented in the DefaultTransport:

// 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

答案5

得分: 1

如果你运行像这样的命令:

HTTP_PROXY=89.x.y.z path_to_program

那么HTTP_PROXY设置仅对该命令有效,这在你不想为整个shell会话设置它时非常有用。
注意:在设置和路径之间没有分号;如果你放一个分号,它会为该shell设置(但不导出)HTTP_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.
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

答案6

得分: 0

对于另一种方法,您还可以使用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()

或者您可以一次设置整个代理。

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()
英文:

For an alternative way, you can also use GoRequest which has a feature that you can set proxy easily for any single request.

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()

huangapple
  • 本文由 发表于 2013年2月2日 19:40:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/14661511.html
匿名

发表评论

匿名网友

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

确定