-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_req1.go
164 lines (154 loc) · 4.55 KB
/
test_req1.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
)
//OptionFun 函数的参数
type OptionFun func(client *http.Client)
//with
func withTimeout(duration time.Duration) OptionFun {
return func(client *http.Client) {
client.Timeout = duration * time.Microsecond
}
}
func NewByOption(opts ...OptionFun) http.Client {
client := http.Client{}
for _, opt := range opts {
opt(&client)
}
return client
}
func main() {
GetTest()
//PostTest()
//PutTest()
//DeleteTest()
//TimeoutTest()
}
func GetTest() {
reqest,err := http.NewRequest(http.MethodGet, "https://www.baidu.com", nil)
if err != nil {
fmt.Println("http.NewRequest :",err)
}
//创建http客户端
var c = NewByOption()
response,err := c.Do(reqest)
defer response.Body.Close()
if response.StatusCode != 200 {
fmt.Println("response.StatusCode :",response.StatusCode)
}
body, _ := io.ReadAll(response.Body) // 读取响应 body, 返回为 []byte
sBody := string(body)
//bodyMap := make(map[string]interface{})
//err = json.Unmarshal(body,&bodyMap)
fmt.Println(sBody)
doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
if err != nil {
fmt.Println("goquery.NewDocumentFromReader :",err)
}
doc.Find
}
func PostTest() {
//POST
//在 POST 方式一般常用的为 2 中,
//
//通过 kv 形式传送,例如 form-data 和 x-www-form-urlencoded
//通过 json 形式传送,例如 application/json
//kv 形式
//payload := strings.NewReader("a=111")
//json 形式
payload := strings.NewReader("{\"name\":\"张三\"}")
reqest,err := http.NewRequest(http.MethodPost,"http://127.0.0.1:8080/user",payload)
if err != nil {
fmt.Println("http.NewRequest",err)
}
reqest.Header.Add("Content-Type", "application/json")
//创建http客户端
var c = NewByOption()
response,err := c.Do(reqest)
if err != nil {
fmt.Println("c.Do",err)
}
defer response.Body.Close()
if response.StatusCode != 200 {
fmt.Println("response.StatusCode :",response.StatusCode)
}
body, _ := io.ReadAll(response.Body) // 读取响应 body, 返回为 []byte
bodyMap := make(map[string]interface{})
err = json.Unmarshal(body,&bodyMap)
fmt.Println(bodyMap)
}
func PutTest() {
//由于 net/http 没有提供简化的 PUT 请求,这里需要使用 http.NewRequest 来创建请求
payload := strings.NewReader("{\"name\":\"张三\"}")
reqest,err := http.NewRequest(http.MethodPut,"http://127.0.0.1:8080/user",payload)
if err != nil {
fmt.Println("http.NewRequest",err)
}
reqest.Header.Add("Content-Type", "application/json")
//创建http客户端
var c = NewByOption()
response,err := c.Do(reqest)
if err != nil {
fmt.Println("c.Do",err)
}
defer response.Body.Close()
if response.StatusCode != 200 {
fmt.Println("response.StatusCode :",response.StatusCode)
}
body, _ := io.ReadAll(response.Body) // 读取响应 body, 返回为 []byte
bodyMap := make(map[string]interface{})
err = json.Unmarshal(body,&bodyMap)
fmt.Println(bodyMap)
}
func DeleteTest() {
payload := strings.NewReader("{\"name\":\"张三\"}")
reqest,err := http.NewRequest(http.MethodDelete,"http://127.0.0.1:8080/user",payload)
if err != nil {
fmt.Println("http.NewRequest",err)
}
reqest.Header.Add("Content-Type", "application/json")
//创建http客户端
var c = NewByOption()
response,err := c.Do(reqest)
if err != nil {
fmt.Println("c.Do",err)
}
defer response.Body.Close()
if response.StatusCode != 200 {
fmt.Println("response.StatusCode :",response.StatusCode)
}
body, _ := io.ReadAll(response.Body) // 读取响应 body, 返回为 []byte
bodyMap := make(map[string]interface{})
err = json.Unmarshal(body,&bodyMap)
fmt.Println(bodyMap)
}
func TimeoutTest() {
reqest,err := http.NewRequest(http.MethodGet,"https://www.baidu.com",nil)
if err != nil {
fmt.Println("http.NewRequest :",err)
}
//创建http客户端
//var c = NewByOption() //不报错
//var c = NewByOption(withTimeout(3)) //不报错
var c = NewByOption(withTimeout(1)) //报错
response,err := c.Do(reqest)
if err != nil {
fmt.Println("c.Do(reqest) err ======",err)
return
}
//fmt.Println("response.Body==",response.Body)
defer response.Body.Close()
if response.StatusCode != 200 {
fmt.Println("response.StatusCode :",response.StatusCode)
}
body, _ := io.ReadAll(response.Body) // 读取响应 body, 返回为 []byte
bodyMap := make(map[string]interface{})
err = json.Unmarshal(body,&bodyMap)
fmt.Println(bodyMap)
}