# Httplib

Httplib 主要是用来模拟客户端发送的 Http 请求。

# 基础使用

不仅支持简单的 Http 请求,还支持 Https 的请求。

package main

import (
	"crypto/tls"

	"github.com/astaxie/beego/httplib"
	"github.com/astaxie/beego/logs"
)

func main() {
	// Get
	req := httplib.Get("http://beego.me/")
	str, err := req.String()
	if err != nil {
		logs.Error(err)
	}

	logs.Info(str)

	// Post
	req = httplib.Post("http://beego.me/")
	req.Param("username", "astaxie")

	// Https
	req = httplib.Get("http://beego.me/")
	req.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) 	// Set TLS
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

# Debug

可以根据返回的数据进行 Debug 调试。

import (
	"github.com/astaxie/beego/httplib"
	"github.com/astaxie/beego/logs"
)

func main() {
	resp, err := httplib.Get("http://beego.me/").Debug(true).Response()
	if err != nil {
		logs.Error(err)

	}

	logs.Info(resp)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 超时设置

import (
	"time"

	"github.com/astaxie/beego/httplib"
	"github.com/astaxie/beego/logs"
)

func main() {
	// use SetTimeout(connectTimeout, readWriteTimeout)
	resp, err := httplib.Get("http://beego.me/").SetTimeout(100*time.Second, 30*time.Second).Response()
	if err != nil {
		logs.Error(err)
	}

	logs.Info(resp)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 设置请求头部

import (
	"github.com/astaxie/beego/httplib"
	"github.com/astaxie/beego/logs"
)

func main() {
	req := httplib.Post("http://beego.me/")
	req.Header("Accept-Encoding", "gzip,deflate,sdch")
	req.Header("Host", "beego.me")

	header := req.GetRequest().Header
	logs.Info(header)
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# 模拟文件请求

import (
	"io/ioutil"

	"github.com/astaxie/beego/httplib"
	"github.com/astaxie/beego/logs"
)

func main() {
	// Upload File
	fileReq := httplib.Post("http://beego.me/")
	fileReq.Param("username", "astaxie")
	fileReq.Param("password", "123456")
	fileReq.PostFile("uploadfile", "hello.txt")

	// Bigfile
	bigFileReq := httplib.Post("http://beego.me/")
	bt, err := ioutil.ReadFile("hello.txt")
	if err != nil {
		logs.Error(err)
	}
	bigFileReq.Body(bt)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# 引用

示例代码地址:https://github.com/beego-dev/beego-example/tree/master/httplib