【问题标题】:golang program determine if user uses proxygolang 程序判断用户是否使用代理
【发布时间】:2015-09-12 21:20:43
【问题描述】:

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

// Make HTTP GET/POST request
proxyUrl, err := url.Parse(proxy)
tr := &http.Transport{
      DisableKeepAlives: true,
      Proxy:             http.ProxyURL(proxyUrl),
}

即使代理变量为空,上面的代码也总是尝试通过代理进行连接。

【问题讨论】:

标签: http go proxy


【解决方案1】:

感谢您的建议。现在我可以让它工作了。下面是修改后的代码。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 2013-05-12
    • 2013-03-11
    • 1970-01-01
    相关资源
    最近更新 更多