我http.NewRequest用来发出几个http请求(显然)。现在,我需要发出请求并从最终URL中提取一些查询字符串(存在重定向)。
http.NewRequest
因此,问题是如何找到URL(如果重定向了客户端,则为最终URL)?Response中没有这样的字段。
请注意,我不需要停止重定向…仅在请求后查找URL是什么
您将回调添加到 http.Client.CheckRedirect
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
然后,您可以在发生新请求时对其进行检查。只要确保设置某种限制来防止重定向循环(如文档中所述,默认值在10点后中止)。