【发布时间】:2020-02-12 04:50:57
【问题描述】:
不明白哪里出了问题。 ioutil.ReadAll 应该像其他 URL 一样使用 gzip。
可以通过网址复制:romboutskorea.co.kr
错误:
gzip:标题无效
代码:
resp, err := http.Get("http://" + url)
if err == nil {
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
fmt.Printf("HTTP Response Status : %v\n", resp.StatusCode)
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("HTTP Response Read error. Url: %v\n", url)
log.Fatal(err)
}
bodyString := string(bodyBytes)
fmt.Printf("HTTP Response Content Length : %v\n", len(bodyString))
}
}
【问题讨论】:
-
Disabling compression 似乎有效(因此 Go 不请求压缩并忽略 Content-Encoding 响应标头)。全局禁用压缩:
http.DefaultTransport.(*http.Transport).DisableCompression = true. -
一般来说,通过网络通信时会出现错误,您应该优雅地处理错误,而不是调用
log.Fatal。