https://www.cnblogs.com/cnsanshao/p/7084808.html
实例如下
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"strings"
"sync"
)
func httpGet() {
resp, err := http.Get("http://10.84.135.139:8089/inter/xpvbvan/inter?method=config&type=set&value=[]&key=wap_recommend")
if err != nil {
// handle error
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
// handle error
}
//fmt.Println(string(body))
fmt.Println("request done", body)
}
// 编码类型为:application/json
func httpPost(requestBody, url, USER_UID, USER_IS_LOGIN string) {
var jsonStr = []byte(requestBody)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Add("Content-Type", "application/json")
req.Header.Add("USER_UID", USER_UID)
req.Header.Add("USER_IS_LOGIN", USER_IS_LOGIN)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Body:", string(body))
}
// 编码为 application/x-www-form-urlencoded
func httpDo() {
client := &http.Client{}
const uriBase = "http://10.84.135.139:8089"
const uriPath = "/inter/xpsfsdfsdgan/inter?method=batchcashlist"
const url = uriBase + uriPath
req, err := http.NewRequest("POST", url, strings.NewReader("pids=1829587349042601,985162418600072,985162418617402"))
if err != nil {
// handle error
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("USER_UID", "4093715774")
req.Header.Add("USER_IS_LOGIN", "1")
resp, err := client.Do(req)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
// handle error
}
fmt.Println(string(body))
}
// 并发测试方法
func testBingfa(n int) {
requestBody := `{
"pid": "985162418722700",
"proname": "这是",
"bussid": "1",
"anchorname":"这是",
"imgurl": "https://pcsdata.hello.com/thumbnail/e7235204f92202431714eb0d47ef3ad8?fid=2073169351-250528-355546491061776&rt=pr&sign=FDTAER-DCb740ccc5511e5e8fedcff06b081203-KFRL8PgNZVaZg0lE%2Fe%2BlSuJuTDY%3D&expires=2h&chkv=0&chkbd=0&chkpc=&dp-logid=1656043183&dp-callid=0&time=1554706800&size=c200_u200&quality=100&vuk=-&ft=video",
"prourl":"https://pasdfgsdgn.hello.com/mall/wap/home#/albumList?pid=1266637396195769&skuid=",
"theme_id":"6"
}`
url := "http://10.84.135.139:8089/rest/2.0/xpsgsgan/fx?method=poster"
USER_UID := "2222342870"
USER_IS_LOGIN := "1"
var wg sync.WaitGroup
wg.Add(n)
for i := 0; i < n; i++ {
go func() {
httpPost(requestBody, url, USER_UID, USER_IS_LOGIN) //被测试方法
wg.Done()
}()
}
wg.Wait()
fmt.Println("ending----")
}
func main() {
testBingfa(3)
}
有疑问加站长微信联系(非本文作者)