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库没有对其进行相应的解压,手动解压下就解决了
go-get-response.body-problem's Introduction
go-get-response.body-problem's People
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.