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)
}
相关推荐
Generalzy2 小时前
从本地 Demo 到生产级检索:Milvus 学习笔记(1)
golang·prompt·软件工程
go不是csgo3 小时前
GORM 上手:一个 main.go 跑通 Go 数据库增删改查
jvm·数据库·golang
知彼解己4 小时前
RAG 核心实战:检索增强生成
后端·golang·ai编程
子安柠5 小时前
Go语言并发编程:协程与管道详解
开发语言·后端·golang
basketball6167 小时前
Go 语言从入门到进阶:5. 玩转Go函数
开发语言·后端·golang
组合缺一8 小时前
Solon Server 启动模式深度解析:从 0.3MB 内核到 10+ Server 插件
java·websocket·http·solon·server
不爱编程的小陈9 小时前
Go内存模型与GC机制:高性能编程的核心
开发语言·后端·golang
go不是csgo9 小时前
sync.Map 源码大变天:一棵 16 叉树如何干掉 read/dirty 双 Map
golang
jieyucx9 小时前
Go 语言零基础入门:标准库 log 包完全教程
golang·日志·log
会编程的土豆9 小时前
Go 语言匿名函数详解
c++·golang·xcode