【发布时间】:2016-07-05 05:48:36
【问题描述】:
我试图避免浪费流量,仅当我看到正确的 content-type 和 content-length 小于设置的阈值时才读取 http 响应正文。
httpRequest, err := http.NewRequest("GET", url, nil)
httpResponse, err := httpClient.Do(httpRequest)
contentType := httpResponse.Header.Get("Content-Type")
// ... check for correct contentType
// Read body into memory?
content, err := ioutil.ReadAll(httpResponse.Body)
是否正确假设如果我发出 GET 请求,无论我是否调用最后一行 iotuil.ReadAll(httpResponse.Body),我都会获得所有正文?
如果是这样,我能想到的避免浪费流量的唯一方法是使用 HEAD 请求,但如果我真的想读取正文,我将不得不发出另一个 GET 请求。如果我提出 HEAD 请求,我是否也能得到正确的 content-length 值?
最好的策略是什么?
【问题讨论】:
标签: http go web-crawler