本文介绍了在Go中读取gzip压缩的HTTP响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Go阅读gzip压缩的HTTP响应!但我总是收到以下错误消息:

I am trying to read a gzipped HTTP response with Go! but I always get the following error message :

panic: gzip: invalid header
[...] stack trace [...]

如果我运行"curl -H" Accept-Encoding:gzip" http://foo.com/ | gunzip-我正确地将响应压缩了下来.我还对ngrep进行了仔细检查,并正确发送/返回了对Accept-Encoding/Content-Encoding.

If I run "curl -H "Accept-Encoding: gzip" http://foo.com/ | gunzip -" I get the response correctly gunzipped. I also double checked with ngrep and the pair Accept-Encoding/Content-Encoding is correctly sent/returned.

如果我创建了一个包含一些虚拟内容的文件并将其gzip压缩,则可以从Go中读取它!程序.

If I create a file with some dummy content and gzip it, I can read it from my Go! program.

我用于测试的程序:

package main

import (
    "io"
    //"os"
    "fmt"
    "compress/gzip"
    "net/http"
)

func main() {
    /* This works fine
    f, _ := os.Open("/tmp/test.gz")
    defer f.Close()
    reader, err := gzip.NewReader(f)
    */

    // This does not :/
    resp, _ := http.Get("http://foo.com/")
    defer resp.Body.Close()
    reader, err := gzip.NewReader(resp.Body)

    if err != nil { panic(err) }

    buff := make([]byte, 1024)
    for {
        n, err := reader.Read(buff)

        if err != nil && err != io.EOF {
            panic(err)
        }

        if n == 0 {
            break
        }
    }

    s := fmt.Sprintf("%s", buff)
    fmt.Println(s)
}

我忽略了什么吗?

推荐答案

编辑:以下是手动处理压缩的示例.如果未设置标题,则默认的传输将为您完成标题,然后在读取响应时解压缩.正文.

The following is an example of manually handling compression. If you don't set the header, the default Transport will do it for you and then decompress while you read the response.Body.

client := new(http.Client)

request, err := http.NewRequest("GET", "http://stackoverflow.com", nil)
request.Header.Add("Accept-Encoding", "gzip")

response, err := client.Do(request)
defer response.Body.Close()

// Check that the server actually sent compressed data
var reader io.ReadCloser
switch response.Header.Get("Content-Encoding") {
case "gzip":
    reader, err = gzip.NewReader(response.Body)
    defer reader.Close()
default:
    reader = response.Body
}

io.Copy(os.Stdout, reader) // print html to standard out

为简洁起见,已删除错误处理.我保留了延期.

Error handling removed for brevity. I kept the defers.

这篇关于在Go中读取gzip压缩的HTTP响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 18:50
查看更多