如何获取http.go中的当前网址? [英] How to get the current URL in http.go?

查看:112
本文介绍了如何获取http.go中的当前网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 http.NewRequest 来做出几个http请求(很明显)。现在我需要提出请求并从最终的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 ) .

所以问题是如何找到URL(如果客户端被重定向到最终的URL)? 响应中没有此字段。

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

推荐答案

您将回调添加到 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之后的默认中止)。

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).

这篇关于如何获取http.go中的当前网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆