英文:
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 theDefaultTransport
- 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}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论