<分区>
我用Go http包开发了一个代码,可以获取任意url的状态码。但经过一番挖掘后,我检查了为什么请求花费了很长时间,我发现 URL 一直在重定向,直到我获得状态 200 OK。
我想设法从网络服务器获得第一响应。我不想总是 200 OK,无论它给我什么,我都想接受。我该怎么办?
例子: https://apple.com当我使用 cURL 获取代码时,响应 301 永久移动。 这就是我想要的响应,而不是重定向到 200。
<分区>
我用Go http包开发了一个代码,可以获取任意url的状态码。但经过一番挖掘后,我检查了为什么请求花费了很长时间,我发现 URL 一直在重定向,直到我获得状态 200 OK。
我想设法从网络服务器获得第一响应。我不想总是 200 OK,无论它给我什么,我都想接受。我该怎么办?
例子: https://apple.com当我使用 cURL 获取代码时,响应 301 永久移动。 这就是我想要的响应,而不是重定向到 200。
最佳答案
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.
ct func(req *Request, via []*Request) error
免责声明:未经测试。
关于http - 如何获得第一个 HTTP(重定向)响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37725910/
相关文章:
java - 我如何摆脱 : HTTP Status 405 - HTTP method GET is not supported by this URL?
go - 是否可以从另一个文件调用测试 Func 来开始测试
go - 接受 *[128]uint8、*[256]uint8 等的函数
http - 使用UTLS和HTTP 1.1请求时通过代理连接
datetime - go中如何对时间对象进行 "greater than or equal"操作?
javascript - Angular 中没有主题标签的刷新页面不起作用