• Golang http 请求如何设置代理


    ENV
    golang 1.17

    使用代理

    需要在创建 http client 的时候设置,使 http 库能够捕获环境变量

    示例

    func newClient(cert tls.Certificate) (*http.Client, error) {
    	config := &tls.Config{
    		Certificates: []tls.Certificate{cert},
    	}
    	config.BuildNameToCertificate()
    	transport := &http.Transport{
    		Proxy:           http.ProxyFromEnvironment,
    		TLSClientConfig: config,
    		IdleConnTimeout: 90 * time.Second,
    	}
    
    	if err := http2.ConfigureTransport(transport); err != nil {
    		return nil, err
    	}
    
    	return &http.Client{
    		Transport: transport,
    		Timeout:   20 * time.Second,
    	}, nil
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    不使用代理

    1. NO_PROXY 环境变量
    // A nil URL and nil error are returned if no proxy is defined in the
    // environment, or a proxy should not be used for the given request,
    // as defined by NO_PROXY.
    
    • 1
    • 2
    • 3
    1. localhost 127.0.0.1 默认也不使用代理
    // As a special case, if req.URL.Host is "localhost" (with or without
    // a port number), then a nil URL and nil error will be returned.
    
    • 1
    • 2
    1. 使用代码禁用代理环境变量
      创建 client 时,可以使用自定义 transport
    transport := http.DefaultTransport
    transport.(*http.Transport).Proxy = nil
    client := &http.Client{
        Transport: transport,
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    golang源码出处 go1.17:src/net/http/transport.go;l=40

    // DefaultTransport is the default implementation of Transport and is
    // used by DefaultClient. It establishes network connections as needed
    // and caches them for reuse by subsequent calls. It uses HTTP proxies
    // as directed by the $HTTP_PROXY and $NO_PROXY (or $http_proxy and
    // $no_proxy) environment variables.
    
    ...
    
    // ProxyFromEnvironment returns the URL of the proxy to use for a
    // given request, as indicated by the environment variables
    // HTTP_PROXY, HTTPS_PROXY and NO_PROXY (or the lowercase versions
    // thereof). HTTPS_PROXY takes precedence over HTTP_PROXY for https
    // requests.
    //
    // The environment values may be either a complete URL or a
    // "host[:port]", in which case the "http" scheme is assumed.
    // The schemes "http", "https", and "socks5" are supported.
    // An error is returned if the value is a different form.
    //
    // A nil URL and nil error are returned if no proxy is defined in the
    // environment, or a proxy should not be used for the given request,
    // as defined by NO_PROXY.
    //
    // As a special case, if req.URL.Host is "localhost" (with or without
    // a port number), then a nil URL and nil error will be returned.
    func ProxyFromEnvironment(req *Request) (*url.URL, error) {
    	return envProxyFunc()(req.URL)
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
  • 相关阅读:
    JAVA Swing 与 GUI Form
    【gazebo要素9】 SDF制作Model文件
    oracle在where条件中关于索引字段的使用注意事项
    java毕业生设计星光在线光影系统计算机源码+系统+mysql+调试部署+lw
    浮点型数据转为字符串
    Mysql 类型转换
    动态组件中的keep-alive的作用
    基于typeorm的nestjs项目使用@zdhsoft/tmg将数据库生成数据模型
    危言耸听?Coinbase投资人解密加密近况,寒冬何时结束?如何应对?
    nodejs模板引擎(一)
  • 原文地址:https://blog.csdn.net/ppdouble/article/details/134516917