何时使用HTTP代理

huangapple go评论74阅读模式
英文:

When to use HTTP Proxy

问题

我正在遇到理解 net/http 包中的 ProxyFromEnvironment 和 ProxyURL 的困难。有人可以解释一下这两个功能是什么时候和为什么使用吗?

根据我目前的理解(至少对于 ProxyFromEnvironment),它用于从环境变量中获取代理服务器的 URL,并且该代理服务器用于进行 HTTP 请求。

英文:

I am having trouble understanding ProxyFromEnvironment and ProxyURL in net/http package. Can someone please explain me when and why these two functionalities are used?

My current understanding (at least for ProxyFromEnvironment) is that this is used to get the URL of a PROXY Server from the Environment Variables and this PROXY Server is used to make HTTP Requests.

答案1

得分: 1

这两个函数与你如何使用http.Transport机制有关。

其中一个函数用于允许传输机制从环境中动态获取代理设置,另一个函数用于提供一个静态URL,每次传输都使用该代理。

ProxyFromEnvironment是一个函数,返回一个描述在环境中配置的代理的URL;它可以赋值给Transport.Proxy字段,每次传输发出请求时,代理都会依赖环境中的值。

ProxyURL是一个函数,返回一个通用函数,每次调用时都返回给定的URL;它可以用于生成一个辅助函数,赋值给Transport.Proxy字段,这样你的传输机制在每次发出请求时都有一个一致的代理。

英文:

Both functions are related to how you use the http.Transport mechanism.

One can be used to allow the transport to dynamically retrieve the proxy settings from the environment, the other can be used to provide a static URL to be used by the transport for the proxy every time.

ProxyFromEnvironment is a func that returns a URL describing the proxy that is configured in the Environment; it can be assigned to the Transport.Proxy field, and every time the Transport makes a request, the Proxy will depend on the values in the Environment.

ProxyURL is a func that returns a general func which returns the given URL every time it is invoked; it can be used to generate a helper function to assign to the Transport.Proxy field, so that your Transport has a consistent Proxy every time the Transport makes a request.

huangapple
  • 本文由 发表于 2016年4月6日 02:55:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/36434788.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定