-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
205 lines (176 loc) · 5.02 KB
/
main.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"bytes"
"fmt"
"github.com/valyala/fasthttp"
"io"
"io/ioutil"
"mime/multipart"
"os"
"path"
"path/filepath"
"strings"
"time"
)
const (
CONTENT_VIID_JSON string = "application/VIID+json; charset=utf-8"
CONTENT_PLAIN string = "text/plain"
CONTENT_APP_JSON string = "application/json"
CONTENT_JS string = "application/javascript"
CONTENT_APP_XML string = "application/xml"
CONTENT_TEXT_XML string = "text/xml"
CONTENT_TEXT_HTML string = "text/html"
CONTENT_FORMDATA string = "multipart/form-data"
CONTENT_FORM string = "application/x-www-form-urlencoded"
)
const (
METHOD_POST = "POST"
METHOD_GET = "GET"
)
const (
URI_POST_MULTIFILE = "http://localhost:20000/postmultifile"
URI_POST_SINGLEFILE = "http://localhost:20000/postfile"
URI_POST = "http://localhost:20000/post?id=hahaha"
URI_GET = "http://localhost:20000/get?username=admin&id=123"
)
var (
c = fasthttp.Client{}
)
func get() {
//请求
req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req)
req.Header.SetContentType(CONTENT_PLAIN)
req.Header.SetMethod(METHOD_GET)
req.SetRequestURI(URI_GET)
//回复
resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(resp)
if err := c.DoTimeout(req, resp, time.Duration(time.Millisecond*50)); err != nil {
fmt.Printf("发送数据失败,错误:%v", err)
}
}
func postForm() {
//请求
req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req)
req.Header.SetContentType(CONTENT_FORM)
req.Header.SetMethod(METHOD_POST)
req.SetRequestURI(URI_POST)
//POST参数
args := &fasthttp.Args{}
args.Add("username", "admin")
args.Add("password", "456")
args.WriteTo(req.BodyWriter())
//回复
resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(resp)
if err := c.DoTimeout(req, resp, time.Duration(time.Millisecond*50)); err != nil {
fmt.Printf("发送数据失败,错误:%v", err)
}
}
func postFile() {
//上传的文件
filename := `D:/0_XLServers/SeemmoSPJGHServer/MotorVehicle.txt`
//创建缓冲区,用于存放文件内容
bodyBuffer := &bytes.Buffer{}
//创建一个multipart文件写入器,方便按照http规定格式写入内容
bodyWrite := multipart.NewWriter(bodyBuffer)
//从bodyWriter生成fileWriter,并将文件内容写入fileWriter,多个文件可进行多次
f := path.Base(filename)
fmt.Printf("=======%v\n", f)
fileWriter, err := bodyWrite.CreateFormFile("uploadfile", f)
if err != nil {
fmt.Println(err.Error())
return
}
//打开上传文件
file, err := os.Open(filename)
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
_, err = io.Copy(fileWriter, file)
if err != nil {
fmt.Printf("copy file filed,%v", err)
return
}
//关闭bodyWriter停止写入数据
bodyWrite.Close()
contentType := bodyWrite.FormDataContentType()
//请求
req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req)
req.Header.SetContentType(contentType)
req.Header.SetMethod(METHOD_POST)
req.SetRequestURI(URI_POST_SINGLEFILE)
req.SetBody(bodyBuffer.Bytes())
//回复
resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(resp)
if err := c.DoTimeout(req, resp, time.Duration(time.Millisecond*2000)); err != nil {
fmt.Printf("发送数据失败,错误:%v", err)
return
}
fmt.Printf("收到回复信息:%v\n", string(resp.Body()))
}
func postMultiFile() {
//上传的文件
fatherDir := "C:/test"
var filenames = []string{}
files, _ := ioutil.ReadDir(fatherDir)
for _, f := range files {
filenames = append(filenames, filepath.Join(fatherDir, f.Name()))
}
//创建缓冲区,用于存放文件内容
bodyBuffer := &bytes.Buffer{}
//创建一个multipart文件写入器,方便按照http规定格式写入内容
bodyWrite := multipart.NewWriter(bodyBuffer)
//从bodyWriter生成fileWriter,并将文件内容写入fileWriter,多个文件可进行多次
for _, filename := range filenames {
f := path.Base(strings.Replace(filename, "\\", "/", -1))
fmt.Printf("filename:%v\n", f)
fileWriter, err := bodyWrite.CreateFormFile("files", f)
if err != nil {
fmt.Println(err.Error())
return
}
//打开上传文件
file, err := os.Open(filename)
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
_, err = io.Copy(fileWriter, file)
if err != nil {
fmt.Printf("copy file filed,%v", err)
return
}
}
//关闭bodyWriter停止写入数据
bodyWrite.Close()
contentType := bodyWrite.FormDataContentType()
//请求
req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req)
req.Header.SetContentType(contentType)
req.Header.SetMethod(METHOD_POST)
req.SetRequestURI(URI_POST_MULTIFILE)
req.SetBody(bodyBuffer.Bytes())
//回复
resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(resp)
if err := c.DoTimeout(req, resp, time.Duration(time.Millisecond*2000)); err != nil {
fmt.Printf("发送数据失败,错误:%v", err)
return
}
fmt.Printf("收到回复信息:%v\n", string(resp.Body()))
}
func main() {
get()
postForm()
postFile()
postMultiFile()
}