golang 并发请求API小demo

迷糊银儿 · · 2323 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

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)
}


有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:迷糊银儿

查看原文:golang 并发请求API小demo

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2323 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传