2016-11-24 77 views
0

我嘗試用這個代碼解析的頁面:HTTP NewRequest得到意想不到的人物

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 @ ?è******* /格式= JSON

然後該請求的調試打印我:

GET /用戶/%00升%***** 0O%* 00.%00c***0-%00v%00i%0*a%00r%00d%[email protected]%00e%0 0i%00t%00e%00c%0 *** 0.%0 *** 00/?format = json HTTP/1.1 Host:****** User-Agent:Go-http-client/1.1 Cookie: PHPSESSID = ********* Accept-Encoding:gzip

+0

請注意,您嘗試屏蔽的文字在百分比編碼輸出中很容易看到。 – cnicutar

+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... 

不太清楚如何你字符串結束了包含t軟管。閱讀更多關於percent encoding

+0

百分比編碼的部分是電子郵件地址,但我需要它不被編碼......問題是我沒有編碼它:/ –

+0

@BorisLeMéec你能展示你如何生成「登錄」?如果您密切關注您發佈的輸出內容,那麼只有該部分存在問題。 – cnicutar

+0

登錄來自:'csv.NewReader(file).ReadAll()'它給了我一小段字符串。所以'ret [id] [indexEmail]'是我的'login','indexEmail'定義爲28。 –

相關問題