英文:
Unexpected EOF using Go http client
问题
我正在学习Go,并遇到了这个问题。
我只是使用HTTP客户端下载网页内容:
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
)
func main() {
client := &http.Client{}
req, err := http.NewRequest("GET", "https://mail.ru/", nil)
req.Close = true
response, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
content, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(content)[:100])
}
当读取响应体时,我遇到了一个"unexpected EOF"错误。与此同时,content变量中包含了完整的页面内容。
只有当我下载https://mail.ru/
的内容时才会出现这个错误。对于其他URL,一切正常,没有任何错误。
我使用curl下载这个页面的内容时一切正常。
我有点困惑 - 这里发生了什么?
Go版本为v1.2,在Ubuntu和MacOS X上尝试过。
英文:
I am learning Go and came across this problem.
I am just downloading web page content using HTTP client:
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
)
func main() {
client := &http.Client{}
req, err := http.NewRequest("GET", "https://mail.ru/", nil)
req.Close = true
response, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
content, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(content)[:100])
}
I get an unexpected EOF
error when reading response body. At the same time content variable has full page content.
This error appear only when I downloading https://mail.ru/
content. With other URLs everything works fine - without any errors.
I used curl for downloading this page content - everything works as expected.
I am confused a bit - what's happening here?
Go v1.2, tried on Ubuntu and MacOS X
答案1
得分: 9
看起来那个服务器(Apache 1.3,哇!)正在提供一个被截断的gzip响应。如果你明确请求identity
编码(防止Go传输添加gzip
),你就不会得到ErrUnexpectedEOF
错误:
req.Header.Add("Accept-Encoding", "identity")
英文:
It looks like the that server (Apache 1.3, wow!) is serving up a truncated gzip response. If you explicitly request the identity
encoding (preventing the Go transport from adding gzip
itself), you won't get the ErrUnexpectedEOF
:
req.Header.Add("Accept-Encoding", "identity")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论