Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Http golang程序确定用户是否使用代理_Http_Go_Proxy - Fatal编程技术网

Http golang程序确定用户是否使用代理

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), } 上述代码始终尝试通过代理进行连接,即使代理变量为空 谢谢你的建议。现在我可以让它工

我希望我的golang http客户端仅在用户提供代理值时使用代理

// 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)
    }
 }
您也可以查看作为参考。