Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Go http客户端时出现意外的EOF_Http_Error Handling_Go - Fatal编程技术网

使用Go http客户端时出现意外的EOF

使用Go http客户端时出现意外的EOF,http,error-handling,go,Http,Error Handling,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 r

我在学围棋时遇到了这个问题

我正在使用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])
}
读取响应正文时,我收到一个
意外的EOF
错误。同时内容变量具有完整的页面内容

此错误仅在我下载
https://mail.ru/
content。使用其他URL时,一切正常-没有任何错误

我用curl下载了这个页面的内容——一切都正常

我有点困惑——这里发生了什么


Go v1.2,在Ubuntu和MacOS X上试用过,看起来该服务器(Apache1.3,哇!)正在提供一个截断的gzip响应。如果您明确请求
标识
编码(阻止Go传输添加
gzip
本身),您将不会得到
errunexpectedof

req.Header.Add("Accept-Encoding", "identity")

也许mail.ru返回的内容长度已中断?手动检查整个响应。Curl“正常工作”,不会抱怨一些小问题。服务器不发送内容长度头,只在发送响应后断开客户端的连接。这导致了一个“意外的EOF”,因为客户不知道会发生这种情况——但显然这很好。以tact方式接收内容。响应具有内容长度标头。我已经手动检查了内容长度标题值和响应的实际长度-它们是相等的。奇怪的是,当我运行你的代码时,我没有得到一个。嗯。。是的,这很奇怪,可能是另一个Go版本?旁注:默认情况下,“标识”编码是可以接受的-如果没有显式抑制()。