使用Go抛出HTTP请求并接收响应时,考虑到ResponseBody很大(1 GB或更大)的情况,我想在流式传输时接收响应。
resp, err: = http.Client.Do(req)
在这种情况下,如果主体很大,我将无法阅读标题,并且我也不知道响应状态。 有什么解决办法吗?
答案 0 :(得分:3)
(编辑:如果您无法从响应中获取“ Content-length”标头,则您所访问的Web服务很可能没有返回该标头。在这种情况下,无法实现无需完全阅读即可了解响应主体的长度。您可以在下面的示例中通过删除在响应中设置Content-length标头的行来进行模拟。)
标准的Go net/http
包可以很好地处理较大的响应。这是一个自我演示的示例:
// Start a mock HTTP server that returns 2GB of data in the response. Make a
// HTTP request to this server and print the amount of data read from the
// response.
package main
import (
"fmt"
"io"
"log"
"net/http"
"strings"
"time"
)
const oneMB = 1024 * 1024
const oneGB = 1024 * oneMB
const responseSize = 2 * oneGB
const serverAddr = "localhost:9999"
func startServer() {
// Mock HTTP server that always returns 2GB of data
go http.ListenAndServe(serverAddr, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-length", fmt.Sprintf("%d", responseSize))
// 1MB buffer that'll be copied multiple times to the response
buf := []byte(strings.Repeat("x", oneMB))
for i := 0; i < responseSize/len(buf); i++ {
if _, err := w.Write(buf); err != nil {
log.Fatal("Failed to write to response. Error: ", err.Error())
}
}
}))
// Some grace period for the server to start
time.Sleep(100 * time.Millisecond)
}
func main() {
startServer()
// HTTP client
req, err := http.NewRequest("GET", "http://"+serverAddr, nil)
if err != nil {
log.Fatal("Error creating HTTP request: ", err.Error())
}
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal("Error making HTTP request: ", err.Error())
}
// Read the response header
fmt.Println("Response: Content-length:", resp.Header.Get("Content-length"))
bytesRead := 0
buf := make([]byte, oneMB)
// Read the response body
for {
n, err := resp.Body.Read(buf)
bytesRead += n
if err == io.EOF {
break
}
if err != nil {
log.Fatal("Error reading HTTP response: ", err.Error())
}
}
fmt.Println("Response: Read", bytesRead, "bytes")
}
如果太大,您将不想读取内存中的整个响应。而是将其写入临时文件,然后进行处理。
如果您正在寻找在网络不太可靠的情况下能够可靠地执行此操作的选项,请寻找“ HTTP范围请求”,使用它可以恢复部分下载的数据。