如何在http.Request中获取URL [英] How to get URL in http.Request
本文介绍了如何在http.Request中获取URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我建立了一个HTTP服务器。我正在使用下面的代码来获取请求URL,但它没有得到完整的URL。
$ b
func Handler(w http.ResponseWriter,r * http.Request){
fmt.Printf(Req: %s%s,r.URL.Host,r.URL.Path)
}
我只得到Req:/
和Req:/favicon.ico
。
我想将完整的客户端请求URL作为1.2.3.4 /
或1.2.3.4 /favicon.ico
。
谢谢。
解决方案
类型请求结构{
...
//寻找URL的主机。
//根据RFC 2616,这是Host:header
//的值或者URL本身给出的主机名。
//它可能是host:port的形式。
主机字符串
...
}
你的代码:
func Handler(w http.ResponseWriter,r * http.Request){
fmt.Printf Req:%s%s \\\
,r.Host,r.URL.Path)
}
示例输出:
需求:localhost:8888 /
I built an HTTP server. I am using the code below to get the request URL, but it does not get full URL.
func Handler(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Req: %s %s", r.URL.Host, r.URL.Path)
}
I only get "Req: / "
and "Req: /favicon.ico"
.
I want to get full client request URL as "1.2.3.4/"
or "1.2.3.4/favicon.ico"
.
Thanks.
解决方案
From the documentation of net/http package:
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
...
}
Modified version of your code:
func Handler(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Req: %s %s\n", r.Host, r.URL.Path)
}
Example output:
Req: localhost:8888 /
这篇关于如何在http.Request中获取URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文