package main
import (
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"net/http"
"reflect"
"github.com/mozillazg/request"
)
func test1(url string) (bt []byte, err error) {
testResp, err := http.Get(url)
if err != nil {
return
}
bt, err = ioutil.ReadAll(testResp.Body)
return
}
func test2(url string) (bt []byte, err error) {
c := new(http.Client)
req := request.NewRequest(c)
resp, err := req.Get(url)
if err != nil {
return
}
bt, err = ioutil.ReadAll(resp.Body)
return
}
func test3(url string) (bt []byte, err error) {
c := new(http.Client)
req := request.NewRequest(c)
resp, err := req.Get(url)
if err != nil {
return
}
var reader io.ReadCloser
if resp.Header.Get("Content-Encoding") == "gzip" {
reader, err = gzip.NewReader(resp.Body)
if err != nil {
return
}
} else {
reader = resp.Body
}
bt, err = ioutil.ReadAll(reader)
return
}
func main() {
urls := [...]string{"https://www.baidu.com", "https://httpbin.org/status/418"}
for i := range urls {
url := urls[i]
fmt.Println("\n----------\n")
fmt.Println("url =", url)
b1, _ := test1(url)
b2, _ := test2(url)
fmt.Println(b1)
fmt.Println(b2)
fmt.Println(reflect.DeepEqual(b1, b2))
fmt.Println("\n----------\n")
b3, _ := test3(url)
fmt.Println(b3)
fmt.Println(reflect.DeepEqual(b1, b3))
}
}
乱码产生的原因是因为网站使用了gzip进行压缩,而request库没有对其进行相应的解压,手动解压下就解决了
有疑问加站长微信联系(非本文作者)