Http golang程序确定用户是否使用代理
我希望我的golang http客户端仅在用户提供代理值时使用代理Http golang程序确定用户是否使用代理,http,go,proxy,Http,Go,Proxy,我希望我的golang http客户端仅在用户提供代理值时使用代理 // Make HTTP GET/POST request proxyUrl, err := url.Parse(proxy) tr := &http.Transport{ DisableKeepAlives: true, Proxy: http.ProxyURL(proxyUrl), } 上述代码始终尝试通过代理进行连接,即使代理变量为空 谢谢你的建议。现在我可以让它工
// Make HTTP GET/POST request
proxyUrl, err := url.Parse(proxy)
tr := &http.Transport{
DisableKeepAlives: true,
Proxy: http.ProxyURL(proxyUrl),
}
上述代码始终尝试通过代理进行连接,即使代理变量为空 谢谢你的建议。现在我可以让它工作了。下面是修改后的代码
tr := &http.Transport{}
tr.DisableKeepAlives = true
if len(proxy) != 0 { // Set the proxy only if the proxy param is specified
proxyUrl, err := url.Parse(proxy)
if err == nil {
tr.Proxy = http.ProxyURL(proxyUrl)
}
}
您也可以查看作为参考。