This is a creation in
Article, where the information may have evolved or changed.
Package Mainimport ("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. Readcloserif 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)}}
The reason why the
is garbled is because the website uses gzip compression, and the request library does not extract the corresponding decompression, the manual decompression is resolved under the