当前位置: 首页 > news >正文

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. 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. 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. 使用代码禁用代理环境变量
    创建 client 时,可以使用自定义 transport
transport := http.DefaultTransport
transport.(*http.Transport).Proxy = nil
client := &http.Client{Transport: transport,
}

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)
}
http://www.lryc.cn/news/238516.html

相关文章:

  • 电子眼与无人机在城市安防中的协同应用研究
  • LVS+keepalived——高可用集群
  • 使用 AWS boto3 库从 s3 桶中批量下载数据
  • js ::after简单实战
  • 数据结构与算法实验(黑龙江大学)
  • 如何使用rclone将腾讯云COS桶中的数据同步到华为云OBS
  • gitlab
  • 3.计算机网络
  • Doris表的动态分区
  • docker小技能:部署mysql
  • “AI在未来”公益计划,亚马逊云科技将教育资源带到更多中西部学校
  • MyBatis的xml实现
  • dolphinscheduler任务莫名重跑
  • Modbus TCP/RTU协议转PROFINET协议网关
  • Caché for UNIX®, Linux及macOS的安装及配置
  • 【书籍篇】Git 学习指南(一)基础概念及入门
  • JWT知识点
  • UDP接收报文函数recvfrom和UDP发送报文函数sendto
  • Redisson 分布式锁实战应用解析
  • 【机器学习】对比学习(contrastive learning)
  • 开源和闭源的优劣势比较
  • html手势密码解锁插件(附源码)
  • Jetson JetPack-5.1.2-L4T-R35.4.1 修复deskew algorithm的问题
  • Doris的分区表和分桶表
  • 华为HCIE技术(HCIP、HCIE)汇总
  • React结合antd5实现整个表格编辑
  • 【C++进阶之路】第八篇:智能指针
  • 保护服务器免受攻击:解析攻击情境与解决之道
  • Python 获取两个数组中各个坐标点对之间最短的欧氏距离
  • 假ArrayList导致的线上事故......