Golang http 请求如何设置代理

ENV

golang 1.17

使用代理

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

示例

go 复制代码
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 环境变量
go 复制代码
// 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 默认也不使用代理
go 复制代码
// 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
go 复制代码
transport := http.DefaultTransport
transport.(*http.Transport).Proxy = nil
client := &http.Client{
    Transport: transport,
}

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

go 复制代码
// 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)
}
相关推荐
Tony Bai22 分钟前
【分布式系统】11 理论的试金石:用 Go 从零实现一个迷你 Raft 共识
开发语言·后端·golang
浮尘笔记1 小时前
Go语言并发安全字典:sync.Map的使用与实现
开发语言·后端·golang
小二·2 小时前
Go 语言系统编程与云原生开发实战(第3篇):企业级 RESTful API 开发 —— 中间件、验证、文档与权限控制
云原生·golang·restful
还在忙碌的吴小二2 小时前
Go-View 数据可视化大屏使用手册
开发语言·后端·信息可视化·golang
冀辉3 小时前
局域网部署时实现基于https的业务系统访问
网络协议·http·https
小二·5 小时前
Go 语言系统编程与云原生开发实战(第4篇):数据持久化深度实战 —— PostgreSQL、GORM 与 Repository 模式
postgresql·云原生·golang
Selenium-Wang5 小时前
动态HTTP隧道代理IP:从配置到实战的完整指南
网络协议·tcp/ip·http
flashier5 小时前
ESP32学习笔记_WiFi(3)——HTTP
网络·笔记·单片机·网络协议·学习·http·esp32
im_AMBER5 小时前
消失的最后一秒:SSE 流式联调中的“时序竞争”
前端·笔记·学习·http·sse
女王大人万岁6 小时前
Go标准库 path 详解
服务器·开发语言·后端·golang