golang 测试代码
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
package main
import (
"net/http"
"net/url"
"fmt"
"io/ioutil"
"time"
"net"
"crypto/tls"
)
func TimeoutDialer(cTimeout time.Duration, rwTimeout time.Duration) func(net, addr string) (c net.Conn, err error) {
return func(netw, addr string) (net.Conn, error) {
conn, err := net.DialTimeout(netw, addr, cTimeout)
if err != nil {
return nil, err
}
conn.SetDeadline(time.Now().Add(rwTimeout))
return conn, nil
}
}
func main() {
connectTimeout := 5 * time.Second
readWriteTimeout := 100 * time.Millisecond
c := http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Dial: TimeoutDialer(connectTimeout, readWriteTimeout),
},
}
uri := "https://www.youtobe.com"
req, err := http.NewRequest(http.MethodPost, uri, nil)
if err != nil {
fmt.Println("req error:" + err.Error())
return
}
req.Header = http.Header{}
req.Header["Connection"] = []string{"Close"}
req.Header["User-Agent"] = []string{"wpt-http-client/1.1"}
data := make(url.Values)
data["name"] = []string{"baixs"}
data["hobby"] = []string{"runing"}
resp, err := c.Do(req)
if err != nil {
fmt.Println("do error,err:" + err.Error())
return
}
respBody, err := ioutil.ReadAll(resp.Body)
fmt.Println(string(respBody))
}
|
测试结果
连接超时(包括握手)
传输超时(也就是读写,从accept开始计算 )
以上这篇golang http 连接超时和传输超时的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/baixiaoshi/article/details/80449730