往返/传输代理地址

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

Go RoundTrip/Transport Proxy Address

问题

我理解了,http.Transport的Proxy字段要求一个生成代理服务器地址的函数。所以这是我的roundtripper:

roundtripper := &http.Transport{
    Proxy: proxyrouter.Calculateproxy,
    ...
}

所以Proxy的类型是func(*Request) (*url.URL, error)。它与服务器进行链接,并在之后被调用:

response := roundtripper.RoundTrip(request)

它返回响应。现在有没有办法知道用于获取此响应的代理地址?(因为我的Calculateproxy函数只是随机选择地址)

英文:

I understand that the Proxy field of http.Transport asks for a function that generates proxy server addresses. So this is my roundtripper:

roundtripper := &http.Transport{
	Proxy: proxyrouter.Calculateproxy,
...
}

So the type of Proxy is func(*Request) (*url.URL, error). This gets linked to the server and is later on called with:

response := roundtripper.RoundTrip(request)

Which returns the response. Now is there any way to know what proxy address was used to get this response? (since my Calculateproxy function just takes random addresses)

答案1

得分: 0

请稍等,我会为您进行翻译。

英文:

Have the Proxy function add a header to record the proxy server used:

Transport{
	Proxy: func(req *Request) (*url.URL, error) {
		p, err := proxyrouter.Calculateproxy(req)
		if err != nil {
			return err
		}
		req.Header.Set("X-Proxy-Addr", p.String())
		return req, nil
	},
}

The http.Response has a reference to the originating request

proxy := resp.Request.Header.Get("X-Proxy-Addr")

huangapple
  • 本文由 发表于 2016年10月4日 17:10:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/39848379.html
匿名

发表评论

匿名网友

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

确定