英文:
http NewRequest get unexpected characters
问题
我尝试使用以下代码解析一个页面:
client := &http.Client{}
profile := getEpiURL(login)
log.Print("Fetch " + profile)
req, err := http.NewRequest("GET", profile, nil)
if err != nil {
log.Fatal(err)
}
req.AddCookie(cookieSessionIntra)
body, _ := httputil.DumpRequestOut(req, true)
其中getEpiURL
函数如下:
func getEpiURL(login string) (url string) {
url = "https://********/user/" + login + "/?format=json"
return
}
当我查看输出时,profile
变量是正确的,但在请求中似乎明显错误...
2016/11/24 12:53:53 Fetch https://******/user/le****in-vird@e*******/?format=json
然后请求的调试输出如下:
GET /user/%00l%0o%00.%00c0-%00v%00i%0*a%00r%00d%00@%00e%00i%00t%00e%00c%00.%000/?format=json
HTTP/1.1 Host: ****** User-Agent: Go-http-client/1.1 Cookie: PHPSESSID=****** Accept-Encoding: gzip
英文:
I try to parse a page with this code :
client := &http.Client{}
profile := getEpiURL(login)
log.Print("Fetch " + profile)
req, err := http.NewRequest("GET", profile, nil)
if err != nil {
log.Fatal(err)
}
req.AddCookie(cookieSessionIntra)
body, _ := httputil.DumpRequestOut(req, true)
With this as getEpiURL function :
func getEpiURL(login string) (url string) {
url = "https://********/user/" + login + "/?format=json"
return
}
And when i look at the output the profile
variable is good, but in the Request it seems obviously wrong...
> 2016/11/24 12:53:53 Fetch https://******/user/le****in-vird@e*******/?format=json
Then the debug for the request prints me :
> GET /user/%00l%0o%00.%00c0-%00v%00i%0*a%00r%00d%00@%00e%00i%00t%00e%00c%00.%000/?format=json
> HTTP/1.1 Host: ****** User-Agent: Go-http-client/1.1 Cookie:
> PHPSESSID=****** Accept-Encoding: gzip
答案1
得分: 1
我认为你的原始字符串中包含了NUL字符。在playground中尝试一下:
func main() {
req, err := http.NewRequest("GET", "https://some/normal/path", nil)
if err != nil {
log.Fatal(err)
}
body, _ := httputil.DumpRequestOut(req, true)
fmt.Printf("Hello, playground, %q", body)
}
你会得到:
"GET /normal/path HTTP/1.1\r\nHost: some...
现在,试试在字符串中插入NUL字符:
req, err := http.NewRequest("GET", "https://some/req, err := http.NewRequest("GET", "https://some/\000n\000o\000rmal/path", nil)
0nreq, err := http.NewRequest("GET", "https://some/\000n\000o\000rmal/path", nil)
0oreq, err := http.NewRequest("GET", "https://some/\000n\000o\000rmal/path", nil)
0rmal/path", nil)
...
"GET /%00n%00o%00rmal/path HTTP/1.1\r\nHost: some...
不太确定你的字符串是怎么包含这些字符的。可以阅读更多关于百分比编码的信息。
英文:
I think your original string somehow contains NUL characters. Try this out in the playground:
func main() {
req, err := http.NewRequest("GET", "https://some/normal/path", nil)
if err != nil {
log.Fatal(err)
}
body, _ := httputil.DumpRequestOut(req, true)
fmt.Printf("Hello, playground, %q", body)
}
You'll get:
"GET /normal/path HTTP/1.1\r\nHost: some...
Now try it with a string in which you inserted NUL characters:
req, err := http.NewRequest("GET", "https://some/req, err := http.NewRequest("GET", "https://some/\000n\000o\000rmal/path", nil)
0nreq, err := http.NewRequest("GET", "https://some/\000n\000o\000rmal/path", nil)
0oreq, err := http.NewRequest("GET", "https://some/\000n\000o\000rmal/path", nil)
0rmal/path", nil)
...
"GET /%00n%00o%00rmal/path HTTP/1.1\r\nHost: some...
Not quite sure how your string ended up containing those. Read more about percent encoding.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论