美文网首页
golang 并发请求API小demo

golang 并发请求API小demo

作者: 迷糊银儿 | 来源:发表于2019-07-23 13:26 被阅读0次

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

    https://www.cnblogs.com/cnsanshao/p/7084808.html 实例如下

  • 协程并非爬虫

    标签: tornado 、爬虫 并发请求demo

  • axios配置

    Example 执行 GET 请求 执行 POST 请求 执行多个并发请求 axios API 可以通过向 axi...

  • 2-1、处理ts-axios的url参数

    编写基础请求代码中最后的测试demo是 我们希望最后请求的url是/api/demo1/get?a=1&b=2,这...

  • redux 异步action

    redux 异步action yarn add redux-thunk 参考 Redux Thunk api请求demo

  • fetch vs axios

    axios 从 node.js 创建 http 请求。 支持 Promise API。 提供了一些并发请求的接口(...

  • 高并发优化

    慕课网Java高并发秒杀API之高并发优化笔记。基于该系列课程的Demo。 分析高并发发生在哪里 业务流程分析 详...

  • Markdown API接口文档demo

    Markdown API接口文档demo [TOC] 接口说明 1、新增机器人 请求URL api/v2/oper...

  • PHP - cURL 使用DEMO、并发请求

    使用CURL上传文件 CURL请求类 curl 实现并发请求 并发测试 传送门:飞鸿影的博客 curl_multi...

  • Golang net/http: HTTP/1.x transp

    简述 今日用golang开发微信小程序 请求wechat服务接口碰到一个问题记录一下 用golang 标准库请求发...

网友评论

      本文标题:golang 并发请求API小demo

      本文链接:https://www.haomeiwen.com/subject/mmpolctx.html