绕过Golang中的http_proxy

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

Bypassing http_proxy in Golang

问题

我已经设置了http_proxy环境变量,但是在另一个调用中,我想绕过代理,直接连接到目标服务器。

在Go语言中有没有办法做到这一点?

谢谢。

英文:

I have env variable set for http_proxy, but with another call, I like to bypass proxy and use direct connection to destination server instead.

Is there any way I can do that in Go lang?

Thanks.

答案1

得分: 3

如@Volker所提到的,你可以选择以下方式之一:

  • 使用自己的RoundTripper替代DefaultTransport
  • 修改DefaultTransport.Proxy,使其对于所需的请求返回nil
  • 如果你想要忽略代理的调用是针对特定主机的,并且你总是希望忽略对该主机的调用的代理,将该主机添加到NO_PROXY环境变量中
英文:

As @Volker mentioned, you can either:

  • Use your own RoundTripper instead of the DefaultTransport
  • Modify DefaultTransport.Proxy to return nil for the request in question
  • If the call you want to ignore proxies with is to a specific host, and you always want to ignore proxy for calls to that host, add the host to the NO_PROXY environment variable

答案2

得分: 3

这是我做的:

var defaultTransport http.RoundTripper = &http.Transport{
	Proxy: nil,
	DialContext: (&net.Dialer{
		Timeout:   10 * time.Second,
		KeepAlive: 30 * time.Second,
		DualStack: true,
	}).DialContext,
	MaxIdleConns:          30,
	IdleConnTimeout:       90 * time.Second,
	TLSHandshakeTimeout:   15 * time.Second,
	ExpectContinueTimeout: 1 * time.Second,
}

client := &http.Client{Transport: defaultTransport}

这段代码创建了一个默认的HTTP传输器,并使用它来创建一个HTTP客户端。传输器的设置包括超时时间、最大空闲连接数等。

英文:

this is what I did:

var defaultTransport http.RoundTripper = &http.Transport{
	Proxy: nil,
	DialContext: (&net.Dialer{
		Timeout:   10 * time.Second,
		KeepAlive: 30 * time.Second,
		DualStack: true,
	}).DialContext,
	MaxIdleConns:          30,
	IdleConnTimeout:       90 * time.Second,
	TLSHandshakeTimeout:   15 * time.Second,
	ExpectContinueTimeout: 1 * time.Second,
}

client := &http.Client{Transport: defaultTransport}

答案3

得分: 3

以下是翻译好的内容:

我在使用企业代理时遇到了这个问题,甚至禁用了它使用的代理URL。
这是我的解决方案:

// 重置http RoundTripper中的代理
var defaultTransport http.RoundTripper = &http.Transport{Proxy: nil}
client := &http.Client{Transport: defaultTransport}

英文:

I had that problem with a corporate proxy, even disabled the proxy url it was using it
this was my solution

// reset proxy in http RoundTripper
var defaultTransport http.RoundTripper = &http.Transport{Proxy: nil}
client := &http.Client{Transport: defaultTransport}

huangapple
  • 本文由 发表于 2017年6月1日 01:27:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/44291191.html
匿名

发表评论

匿名网友

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

确定