Golang http 请求如何设置代理

news/2024/7/19 14:15:17 标签: golang, http, proxy
http://www.w3.org/2000/svg" style="display: none;">

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,
}

https://cs.opensource.google/go/go/+/refs/tags/go1.17:src/net/http/transport.go;l=40">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.niftyadmin.cn/n/5203050.html

相关文章

有意思网站合集2

1.无尽的马 网址:http://endless.horse/ 一匹由字符拼成的马,你会发现它的腿一直延生到网页的尽头。 不管你怎么样往下拖动网页,始终不会到达尽头。 2.In Pieces 网址:http://www.species-in-pieces.com/# In Pieces 是一个公益网…

[MICROSAR Adaptive] --- Communication Management

0 引言 本期会介绍communicationmanagement通信管理,首先介绍它的特点使用方式,然后介绍模型中的相关元素和c代码中的相关API,最后我们实现一个应用程序,他有两个Executable组成,一个是提供服务的provider&#xff0c…

Python 打开多个文件

Python 打开文件使用open 打开多个文件可是使用写多个open ,如果是2个或者3个数量很少的情况可以 写2个或者3个open 打开 with open(r"文件名1", "r", encoding"utf-8") as f1, open(r"文件名2", "r", encodi…

Spring Boot要如何学习?【云驻共创】

Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。我这里会分享一些学习Spring Boot的方法和干货,包括…

关于 win11 系统下12代/13代英特尔大小核架构 CPU 的 VMware 优化:输入延迟、卡顿,大小核调度

关于 win11 系统下12代/13代英特尔大小核架构 CPU 的 VMware 优化:输入延迟、卡顿,大小核调度 一、前言二、VMware 的优化2.1 键鼠输入延迟问题的解决2.1.1 搜索内核隔离2.1.2 关闭内存完整性并重启2.1.3 搜索启用或关闭windows功能2.1.4 关闭 hyper-v 和…

数字逻辑电路基础-时序逻辑电路之锁存器

文章目录 一、锁存器简介二、verilog源码三、综合及仿真结果一、锁存器简介 本文介绍数字逻辑电路中一种常用的基础时序逻辑电路-锁存,顾名思义,它的功能就是将输入在控制信号有效时透明传输到输出端,当控制信号无效时,输出值保持不变。它具有记忆和存储功能。这是它区别组…

如何打造适用的MES管理系统解决方案

在当前的制造业领域,项目型生产企业面临着独特的挑战。尽管国外的大型软件公司提供了某些解决方案,但由于地域、文化和制度的差异,这些方案并不完全满足企业的实际需求。为了解决这一难题,我们必须以客户为中心,围绕他…

麒麟v10系统,在虚拟机上直接连公司同一个局域网,设置静态ip

1.更改配置信息 cd /etc/sysconfig/network-scripts vi ifcfg-ens33 TYPEEthernet PROXY_METHODnone BROWSER_ONLYno BOOTPROTOstatic DEFROUTEyes IPV4_FAILURE_FATALno IPV6INITyes IPV6_AUTOCONFyes IPV6_DEFROUTEyes IPV6_FAILURE_FATALno IPV6_ADDR_GEN_MODEstable-pri…