在开发golang http应用程序时,我使用了很多。 http.Request
当访问请求主机地址时,我会使用 req.Host
, 但我发现req.URL.Host
字段, 但是当我打印它时,它是空的。
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Println("uri Host: " + r.URL.Host + " Scheme: " + r.URL.Scheme)
fmt.Println("Host: " + r.Host)
}
http.Request 的文档给出以下注释,而net/url
net/url没有给出太多提示。
// For server requests Host specifies the host on which the
// URL is sought. Per RFC 2616, this is either the value of
// the "Host" header or the host name given in the URL itself.
// It may be of the form "host:port". For international domain
// names, Host may be in Punycode or Unicode form. Use
// golang.org/x/net/idna to convert it to either format if
// needed.
//
// For client requests Host optionally overrides the Host
// header to send. If empty, the Request.Write method uses
// the value of URL.Host. Host may contain an international
// domain name.
Host string
在我看来,在一个请求中有两个主机值:uri行和 host header,就像:
GET http://localhost:8080/ HTTP/1.1
Host: localhost:8080
但这并没有解决很多问题,它所带来的麻烦是:
- 为什么请求中有两个不同的
Host
字段? 我的意思是这不是重复的吗? - 两个主机字段在同一个请求中可以不同吗?
- 在什么情况下应该使用哪一个?
要是带有真实HTTP请求示例的答案是最好的。 提前致谢!