Created
May 7, 2019 03:23
-
-
Save codcodog/6f6c011922247e1080dfaf441e89f17d to your computer and use it in GitHub Desktop.
HTTP 请求示例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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