最佳答案
我想解析一个web请求的响应,但我遇到麻烦访问它作为字符串。
func main() {
resp, err := http.Get("http://google.hu/")
if err != nil {
// handle error
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
ioutil.WriteFile("dump", body, 0600)
for i:= 0; i < len(body); i++ {
fmt.Println( body[i] ) // This logs uint8 and prints numbers
}
fmt.Println( reflect.TypeOf(body) )
fmt.Println("done")
}
我如何访问响应作为字符串?ioutil.WriteFile
正确地写入对文件的响应。
我已经检查了包装参考,但它并没有真正的帮助。