普通网友 2014-01-15 20:30
浏览 552
已采纳

使用Go http客户端发生意外的EOF

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条回答 默认 最新

  • duanfei8897 2014-01-16 11:50
    关注

    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")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度