gobox中的httpclient

作者: ligang1109 | 来源:发表于2018-08-18 15:23 被阅读6次

今天来说下使用gobox中httpclient,这个包就相当于命令行的curl工具,用于发起http请求。

重要的对象

config

const (
    DEFAULT_TIMEOUT        = 30 * time.Second
    DEFAULT_KEEPALIVE_TIME = 30 * time.Second

    DEFAULT_MAX_IDLE_CONNS_PER_HOST = 10
)

type Config struct {
    LogLevel int

    Timeout       time.Duration      // 连接及读写超时
    KeepAliveTime time.Duration

    MaxIdleConnsPerHost int
}

request

type Request struct {
    Method     string
    Url        string
    Body       []byte
    Ip         string     // 相当于设置hostIp
    ExtHeaders map[string]string

    *http.Request
}

response

type Response struct {
    T        time.Duration  // 请求耗时
    Contents []byte         // 响应内容

    *http.Response
}

代码示例

package main

import (
    "github.com/goinbox/gohttp/httpclient"

    "time"
    "net/http"
    "fmt"
)

func main() {
    config := httpclient.NewConfig()
    config.Timeout = time.Second * 1

    client := httpclient.NewClient(config, nil)

    fmt.Println("clientGet")
    clientGet(client)

    fmt.Println("clientPost")
    clientPost(client)
}

func clientGet(client *httpclient.Client) {
    extHeaders := map[string]string{
        "GO-CLIENT-1": "gobox-httpclient-1",
        "GO-CLIENT-2": "gobox-httpclient-2",
    }
    req, _ := httpclient.NewRequest(http.MethodGet, "http://www.vmubt.com/test.php?a=1&b=2", nil, "127.0.0.1", extHeaders)

    resp, err := client.Do(req, 1)
    if err != nil {
        fmt.Println(err)
    } else {
        fmt.Println(string(resp.Contents), resp.T.String())
    }
}

func clientPost(client *httpclient.Client) {
    extHeaders := map[string]string{
        "GO-CLIENT-1":  "gobox-httpclient-1",
        "GO-CLIENT-2":  "gobox-httpclient-2",
        "Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
    }
    params := map[string]interface{}{
        "a": 1,
        "b": "bb",
        "c": "测试post",
    }
    req, _ := httpclient.NewRequest(http.MethodPost, "http://www.vmubt.com/test.php", httpclient.MakeRequestBodyUrlEncoded(params), "127.0.0.1", extHeaders)

    resp, err := client.Do(req, 1)
    if err != nil {
        fmt.Println(err)
    } else {
        fmt.Println(string(resp.Contents), resp.T.String())
    }
}

输出:

clientGet
array(0) {
}
 1.516315ms

clientPost
array(3) {
  ["a"]=>
  string(1) "1"
  ["b"]=>
  string(2) "bb"
  ["c"]=>
  string(10) "测试post"
}
 1.616384ms

欢迎大家使用,使用中有遇到问题随时反馈,我们会尽快响应,谢谢!

相关文章

  • gobox中的httpclient

    今天来说下使用gobox中httpclient,这个包就相当于命令行的curl工具,用于发起http请求。 重要的...

  • gobox中的shardmap

    今天来说下gobox中的shardmap。 golang中的map使用简单,但并发写入时,如果不加锁,会导致pan...

  • gobox中的分页操作

    今天来说下使用gobox中的分页操作 说明 分页也是我们开发时的一个常见需求,gobox中提供了page包做这个事...

  • gobox中的simplecache和levelcache

    今天来说下gobox中的simplecache和levelcache simplecache simplecach...

  • HttpClient详细梳理

    HttpClient整理资料 1、httpClient HttpClient是Apache中的一个开源的项目。它实...

  • gobox中的log操作

    今天来说下使用gobox中的log操作 log级别定义 重要的interface IWriter 定义消息写入到哪...

  • gobox中redis操作

    今天来说下使用gobox中redis操作相关 说明 本包的driver部分使用了redigo:https://gi...

  • gobox中mysql操作

    今天来说下使用gobox中mysql操作相关 说明 本包的driver部分使用了go-sql-driver:htt...

  • Spring RestTemplate 请求乱码的问题

    我们知道 Spring 中 HttpClient 请求使用的 RestTemplate 封装的HttpClient...

  • Android的网络通信机制

    HttpClient接口 在Android6.0中,HttpClient库已经被移除 Android SDK附带的...

网友评论

    本文标题:gobox中的httpclient

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