http NewRequest获取意外字符

时间:2016-11-24 12:11:51

标签: http go get

我尝试使用以下代码解析页面:

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获取https:// ******** / user / le **** in-vi ** rd @ e ******* / ?格式= JSON

然后对请求的调试打印出来:

  

GET / user /%00l%***** 0o% * 00.%00c *** 0-%00v%00i%0 * a%00r%00d%00 @%00e%0 < /strong>0i%00t%00e%00c%0***0.%0***00/?format=json   HTTP / 1.1主机:****** User-Agent:Go-http-client / 1.1 Cookie:   PHPSESSID = *********接受编码:gzip

1 个答案:

答案 0 :(得分:1)

我认为你的原始字符串以某种方式包含NUL字符。在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)
}

你会得到:

"GET /normal/path HTTP/1.1\r\nHost: some...

现在try it with a string in which you inserted NUL characters

req, err := http.NewRequest("GET", "https://some/\000n\000o\000rmal/path", nil)

...

"GET /%00n%00o%00rmal/path HTTP/1.1\r\nHost: some...

不太确定你的字符串是如何包含那些字符串的。详细了解percent encoding