我建立了一个 HTTP 服务器。我使用下面的代码来获取请求 URL,但它没有获取完整的 URL。
func Handler(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Req: %s %s", r.URL.Host, r.URL.Path)
}
我只得到"Req: / "
and "Req: /favicon.ico"
.
我想获取完整的客户端请求 URL"1.2.3.4/"
or "1.2.3.4/favicon.ico"
.
Thanks.
从 net/http 包的文档中:
type Request struct {
...
// 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".
Host string
...
}
您的代码的修改版本:
func Handler(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Req: %s %s\n", r.Host, r.URL.Path)
}
输出示例:
Req: localhost:8888 /
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)