GolangNote

Golang笔记

golang http 转向状态码的小区别

Permalink

http 跟转向相关的状态码,要恰当使用,避免直接跳转。

  • 300 Multiple Choices: 返回多个可供选择的资源
  • 301 Moved Permanently: 请求的资源已永久移动到新位置,并且将来任何对此资源的引用都应该使用本响应返回的若干个URI之一
  • 302 Found: 请求的资源现在临时从不同的URI响应请求。由于这样的重定向是临时的,客户端应当继续向原有地址发送以后的请求,HTTP 1.0中的意义是Moved Temporarily,但是很多浏览器的实现是按照303的处实现的,所以HTTP 1.1中增加了 303和307的状态码来区分不同的行为
  • 303 See Other (since HTTP/1.1): 对应当前请求的响应可以在另一个URI上被找到,而且客户端应当采用GET的方式访问那个资源
  • 304 Not Modified (RFC 7232): 请求的资源没有改变
  • 305 Use Proxy (since HTTP/1.1): 被请求的资源必须通过指定的代理才能被访问
  • 306 Switch Proxy: 在最新版的规范中,306状态码已经不再被使用
  • 307 Temporary Redirect (since HTTP/1.1): 请求的资源现在临时从不同的URI响应请求,和303不同,它还是使用原先的Method
  • 308 Permanent Redirect (RFC 7538): 请求的资源已永久移动到新位置,并且新请求的Method不能改变

对应的golang 常量:

Go: http 转向状态码
1
2
3
4
5
6
7
8
9
10
StatusMultipleChoices   = 300 // RFC 7231, 6.4.1
StatusMovedPermanently  = 301 // RFC 7231, 6.4.2
StatusFound             = 302 // RFC 7231, 6.4.3
StatusSeeOther          = 303 // RFC 7231, 6.4.4
StatusNotModified       = 304 // RFC 7232, 4.1
StatusUseProxy          = 305 // RFC 7231, 6.4.5
_                       = 306 // RFC 7231, 6.4.6 (Unused)
StatusTemporaryRedirect = 307 // RFC 7231, 6.4.7
StatusPermanentRedirect = 308 // RFC 7538, 3

本文网址: https://golangnote.com/topic/198.html 转摘请注明来源

Related articles

Golang http IPv4/IPv6 服务

Golang 的网络服务,如果不指定IPv4 或 IPv6,如果VPS 同时支持 IPv4 和 IPv6,`net.Listen()` 只会监听 IPv6 地址。但这不影响客户端使用 IPv4 地址来访问。...

Golang http client 处理重定向网页

假设一个网址有多个重定向,A-B-C-D,使用 http.Client.Get 最后取得的内容是网址D的内容,我们该手动处理包含重定向的网址。...

Write a Comment to "golang http 转向状态码的小区别"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.20 Processed in 0ms