13

gobox中的httpclient

 4 years ago
source link: http://blog.7rule.com/2018/08/18/gobox-http-client.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

gobox中的httpclient

Aug 18, 2018

今天来说下使用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

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


Recommend

  • 42
    • blog.7rule.com 6 years ago
    • Cache

    gobox中的连接池pool

    今天来说下gobox中的连接池底层实现pool 为什么需要连接池 我们的系统在访问外部资源(redis、mysql等)时,为了提高性能,通常会用到的一个优化方法就是把已经使用过的tcp连接保存起来,这样当需要再次使用时,就可...

  • 40
    • blog.7rule.com 6 years ago
    • Cache

    gobox中的log操作

    今天来说下使用gobox中的log操作 log级别定义 const ( LEVEL_EMERGENCY = 0 LEVEL_ALERT = 1 LEVEL_CRITICAL = 2 LEVEL_ERROR = 3 LEVEL_WARNING = 4 LEVEL_NOTICE = 5 LEVEL_INFO = 6...

  • 49
    • blog.7rule.com 6 years ago
    • Cache

    gobox中的http请求处理框架

    今天来说下使用gobox中的http请求处理框架 http请求处理架构图 重要的对象 System system用于实现g...

  • 39
    • blog.7rule.com 6 years ago
    • Cache

    gobox中的consumer处理框架

    我们都会有从异步队列中消费的需求,今天来说下gobox中的consumer处理框架 consumer处理架构图 重要的对象 IM...

  • 11
    • blog.7rule.com 4 years ago
    • Cache

    gobox中的常用工具包gomisc

    gobox中的常用工具包gomisc Sep 8, 2018 有一些常用的工具函数,我们把它们放到gomisc这个包中。 Slice中的值Unique func IntSliceUnique(s []int) []int func StringSli...

  • 15
    • blog.7rule.com 4 years ago
    • Cache

    gobox中的分页操作

    gobox中的分页操作 Aug 3, 2018 今天来说下使用gobox中的分页操作 分页也是我们开发时的一个常见需求,gobox中提供了page包做这个事情 package main import ( "github....

  • 17
    • blog.7rule.com 4 years ago
    • Cache

    gobox中redis操作

    gobox中redis操作 Jul 29, 2018 今天来说下使用gobox中redis操作相关 本包的driver部分使用了redigo:https://github.com/garyburd/redigo package main import ( "git...

  • 11
    • blog.7rule.com 4 years ago
    • Cache

    gobox中mysql操作

    gobox中mysql操作 Jul 21, 2018 今天来说下使用gobox中mysql操作相关 本包的driver部分使用了go-sql-driver:https://github.com/go-sql-driver/mysql 示例表结构为: | de...

  • 9
    • blog.7rule.com 4 years ago
    • Cache

    gobox中的simplecache和levelcache

    gobox中的simplecache和levelcache Jun 10, 2018 今天来说下gobox中的simplecache和levelcache simplecache simplecache提供了一个简单的内存kv package main i...

  • 13
    • blog.7rule.com 4 years ago
    • Cache

    gobox中的shardmap

    gobox中的shardmap May 25, 2018 今天来说下gobox中的shardmap。 golang中的map使用简单,但并发写入时,如果不加锁,会导致panic,所以性能很差。 shardmap就是为了解决这个问题,其核心思想就...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK