英文:
How to get the current URL in http.go?
问题
我正在使用http.NewRequest
来进行多个HTTP请求(显然)。现在我需要发送请求并从最终的URL中提取一些查询字符串(因为有重定向)。
所以问题是,如果客户端被重定向,我如何找到URL(即最终的URL)?在Response中没有这样的字段。
请注意,我不需要停止重定向...只需要找到请求后的URL是什么。
英文:
I'm using http.NewRequest
to make several http requests ( obviously ). Now I need to make request and extract some query strings from the final URL ( there is a redirect ) .
So the question is how can I find the URL (if the final URL if the client was redirected ) ? There is no such field in Response.
Please note that I don't need to stop the redirect... only to find what's the URL after request
答案1
得分: 3
尽管@JimB实际上回答了这个问题,但我还是发布这个回答,因为它可能对某人有帮助。我使用了一个匿名函数。也许使用闭包可以更好地完成,但我还没有弄清楚闭包的实际工作原理。
req, err = http.NewRequest("GET", URL, nil)
cl := http.Client{}
var lastUrlQuery string
cl.CheckRedirect = func(req *http.Request, via []*http.Request) error {
if len(via) > 10 {
return errors.New("too many redirects")
}
lastUrlQuery = req.URL.RequestURI()
return nil
}
resp, err := cl.Do(req)
if err != nil {
log.Fatal(err)
}
fmt.Printf("最后的URL查询是 %v", lastUrlQuery)
英文:
Although @JimB actually answered the question I'm posting this as it may help someone. I've used an anonymous function. Perhaps could be done better using a closures but I didn't figured it out yet how closures actually work.
req, err = http.NewRequest("GET", URL, nil)
cl := http.Client{}
var lastUrlQuery string
cl.CheckRedirect = func(req *http.Request, via []*http.Request) error {
if len(via) > 10 {
return errors.New("too many redirects")
}
lastUrlQuery = req.URL.RequestURI()
return nil
}
resp, err := cl.Do(req)
if err != nil {
log.Fatal(err)
}
fmt.Printf("last url query is %v", lastUrlQuery)
答案2
得分: 2
你可以向http.Client.CheckRedirect
添加一个回调函数。
// CheckRedirect指定处理重定向的策略。
// 如果CheckRedirect不为nil,在跟随HTTP重定向之前,客户端会调用它。
// 参数req和via分别是即将发起的请求和已经发起的请求,最早的请求排在最前面。
// 如果CheckRedirect返回一个错误,Client的Get方法会返回之前的响应和CheckRedirect的错误(包装在url.Error中),而不是发起请求req。
//
// 如果CheckRedirect为nil,Client会使用默认策略,即在连续10次请求后停止。
CheckRedirect func(req *Request, via []*Request) error
然后,你可以检查每次发生的新请求。只需确保设置某种限制以防止重定向循环(如文档中所述,默认情况下在10次重定向后会中止)。
英文:
You add a callback to http.Client.CheckRedirect
// CheckRedirect specifies the policy for handling redirects.
// If CheckRedirect is not nil, the client calls it before
// following an HTTP redirect. The arguments req and via are
// the upcoming request and the requests made already, oldest
// first. If CheckRedirect returns an error, the Client's Get
// method returns both the previous Response and
// CheckRedirect's error (wrapped in a url.Error) instead of
// issuing the Request req.
//
// If CheckRedirect is nil, the Client uses its default policy,
// which is to stop after 10 consecutive requests.
CheckRedirect func(req *Request, via []*Request) error
You can then inspect the new Request as they happen. Just be sure to institute some sort of limit to prevent redirect loops (as stated in the docs, the default aborts after 10).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论