英文:
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")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论