Setting up proxy for HTTP client

后端 未结 5 2070
抹茶落季
抹茶落季 2020-12-12 18:15

I\'m trying to setup the HTTP client so that it uses a proxy, however I cannot quite understand how to do it. The documentation has multiple reference to \"proxy\" but none

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-12 18:37

    lukad is correct, you could set the HTTP_PROXY environment variable, if you do this Go will use it by default.

    Bash:

    export HTTP_PROXY="http://proxyIp:proxyPort"
    

    Go:

    os.Setenv("HTTP_PROXY", "http://proxyIp:proxyPort")
    

    You could also construct your own http.Client that MUST use a proxy regardless of the environment's configuration:

    proxyUrl, err := url.Parse("http://proxyIp:proxyPort")
    myClient := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)}}
    

    This is useful if you can not depend on the environment's configuration, or do not want to modify it.

    You could also modify the default transport used by the "net/http" package. This would affect your entire program (including the default HTTP client).

    proxyUrl, err := url.Parse("http://proxyIp:proxyPort")
    http.DefaultTransport = &http.Transport{Proxy: http.ProxyURL(proxyUrl)}
    

提交回复
热议问题