Skip to content

Instantly share code, notes, and snippets.

@codcodog
Created May 7, 2019 03:23
Show Gist options
  • Save codcodog/6f6c011922247e1080dfaf441e89f17d to your computer and use it in GitHub Desktop.
Save codcodog/6f6c011922247e1080dfaf441e89f17d to your computer and use it in GitHub Desktop.
HTTP 请求示例
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
req, err := http.NewRequest("GET", "https://cn.bing.com", nil)
if err != nil {
fmt.Println(err)
return
}
req.Close = true
//or do this:
//req.Header.Add("Connection", "close")
resp, err := http.DefaultClient.Do(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
fmt.Println(err)
return
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(len(string(body)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment