代码拉取完成,页面将自动刷新
package easy_http
import (
"fmt"
"net/http"
"testing"
"time"
)
func TestClient(t *testing.T) {
//new 一个构造器
builder := NewClientBuilder()
//是否跳过服务器证书校验
builder.SkipVerify(false)
//设置超时时间
builder.TimeOut(time.Second * 5)
//设置代理
builder.ProxyUrl("http://127.0.0.1:10809")
//设置根证书
var certPool [1]string
certPool[0] = "D:\\server.pem"
builder.Cert(certPool[:])
//设置双向校验证书
var tlsPath [1]*TlsPath
tlsPath[0] = &TlsPath{
CertFile: "D:\\client.pem",
KeyFile: "D:\\client.key",
}
builder.Tls(tlsPath[:])
//设置http请求header
header := make(map[string]string)
header["Accept-Language"] = "Accept-Language: en,zh"
builder.Header(header)
//设置http请求cookie
cookie := make(map[string]string)
cookie["name"] = "value"
builder.Cookie(EasyCookie(cookie))
//设置 Response 处理函数
builder.BuildResponse(EasyBuildResponse)
//构造client
client, err := builder.Build()
if err != nil {
fmt.Println(err)
return
}
response := client.Get("https://baidu.com")
fmt.Println(response.Error())
fmt.Println(response.StatusCode())
fmt.Println(string(response.Content()))
}
func TestClientGetAsyn(t *testing.T) {
builder := NewClientBuilder()
client, _ := builder.Build()
err := client.GetAsyn("http://baidu.com", call)
if err != nil {
panic(err)
}
time.Sleep(5 * time.Second)
}
func TestClientGetAsynWithCallback(t *testing.T) {
builder := NewClientBuilder()
client, err := builder.Build()
if err != nil {
panic(err.Error())
}
err = client.GetAsynWithCallback("http://baidu.com", &Get{})
if err != nil {
panic(err)
}
time.Sleep(5 * time.Second)
}
func call(response IResponse) {
fmt.Println(response.Error())
fmt.Println(string(response.Content()))
}
type Get struct {
}
func (g Get) EasyResponseCallback(response IResponse) {
fmt.Println(response.Error())
fmt.Println(string(response.Content()))
}
func TestClientProxy(t *testing.T) {
builder := NewClientBuilder()
builder.ProxyUrl("http://127.0.0.1:10809")
client, err := builder.Build()
if err != nil {
fmt.Println(err)
return
}
response := client.Get("https://google.com")
fmt.Println(response.Error())
fmt.Println(response.StatusCode())
fmt.Println(string(response.Content()))
}
func TestClientStl(t *testing.T) {
builder := NewClientBuilder()
//builder.Tls("D:\\client.pem", "D:\\client.key")
client, err := builder.Build()
if err != nil {
fmt.Println(err)
return
}
response := client.Get("https://lqx.com")
fmt.Println(response.Error())
fmt.Println(response.StatusCode())
fmt.Println(string(response.Content()))
}
func TestClientHeader(t *testing.T) {
builder := NewClientBuilder()
header := make(map[string]string)
header["token"] = "ttttt"
client, err := builder.Header(header).Build()
if err != nil {
panic(err)
}
header2 := make(map[string]string)
header2["User-Agent"] = "dsdsdsdsdsd"
client.Header(header2).Get("http://127.0.0.1:8088")
client.Get("http://127.0.0.1:8088")
}
func TestClientPostForm(t *testing.T) {
builder := NewClientBuilder()
client, err := builder.Build()
if err != nil {
panic(err)
}
data := make(map[string]string)
data["name"] = "vvvvv1"
data["name2"] = "2222222"
client.PostForm("http://127.0.0.1:8088", EasyPost(data))
}
func TestClientPostMultipart(t *testing.T) {
builder := NewClientBuilder()
client, err := builder.Build()
if err != nil {
panic(err)
}
data := make(map[string]string)
data["name"] = "vvvvv1"
data["name2"] = "2222222"
multipartBuilder := NewMultipartBuilder()
multipart, err := multipartBuilder.FromDate(data).AddFile("file", "D:\\a.txt").
AddFile("file2", "D:\\b.log").Builder()
if err != nil {
panic(err)
}
client.PostMultipart("http://127.0.0.1:8088", multipart)
}
func TestClientSendWithMethod(t *testing.T) {
builder := NewClientBuilder()
client, err := builder.Build()
if err != nil {
panic(err)
}
response := client.SendWithMethod("http://127.0.0.1:8088", http.MethodGet, nil, func(request *http.Request) {
})
fmt.Println(response)
}
func TestClientHttpRequest(t *testing.T) {
builder := NewClientBuilder()
client, err := builder.Build()
if err != nil {
panic(err)
}
request, err := http.NewRequest(http.MethodGet, "http://baidu.com", nil)
if err != nil {
panic(err)
}
response, err := client.DoRequest(request)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(response)
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。