golang 使用 http client 的正确方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package main
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"time"
)
func main() {
var netTransport = &http.Transport{
Dial: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 5 * time.Second,
ResponseHeaderTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
var netClient = &http.Client{
Timeout: time.Second * 30,
Transport: netTransport,
}
// get
response, _ := netClient.Get("https://golangnote.com/")
defer response.Body.Close()
if response.StatusCode == 200 {
body, _ := ioutil.ReadAll(response.Body)
fmt.Println(string(body))
}
}
本文网址: https://golangnote.com/topic/180.html 转摘请注明来源