mirror of
https://gitee.com/johng/gf.git
synced 2024-12-02 12:17:53 +08:00
Merge pull request #1823 from huangqian1985/master
add gClient ExampleNew function
This commit is contained in:
commit
e4edbe25b2
@ -71,9 +71,8 @@ func (r *Response) ReadAllString() string {
|
||||
|
||||
// Close closes the response when it will never be used.
|
||||
func (r *Response) Close() error {
|
||||
if r == nil || r.Response == nil || r.Response.Close {
|
||||
if r == nil || r.Response == nil {
|
||||
return nil
|
||||
}
|
||||
r.Response.Close = true
|
||||
return r.Response.Body.Close()
|
||||
}
|
||||
|
@ -9,6 +9,9 @@ package gclient_test
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/net/gclient"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
@ -98,6 +101,49 @@ func init() {
|
||||
time.Sleep(time.Millisecond * 500)
|
||||
}
|
||||
|
||||
func ExampleNew() {
|
||||
var (
|
||||
ctx = gctx.New()
|
||||
client = gclient.New()
|
||||
)
|
||||
|
||||
if r, err := client.Get(ctx, "http://127.0.0.1:8999/var/json"); err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
defer r.Close()
|
||||
fmt.Println(r.ReadAllString())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// {"id":1,"name":"john"}
|
||||
}
|
||||
|
||||
func ExampleNew_MultiConn_Recommend() {
|
||||
var (
|
||||
ctx = gctx.New()
|
||||
client = g.Client()
|
||||
)
|
||||
|
||||
// controls the maximum idle(keep-alive) connections to keep per-host
|
||||
client.Transport.(*http.Transport).MaxIdleConnsPerHost = 5
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
if r, err := client.Get(ctx, "http://127.0.0.1:8999/var/json"); err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
fmt.Println(r.ReadAllString())
|
||||
r.Close()
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
//{"id":1,"name":"john"}
|
||||
//{"id":1,"name":"john"}
|
||||
//{"id":1,"name":"john"}
|
||||
//{"id":1,"name":"john"}
|
||||
//{"id":1,"name":"john"}
|
||||
}
|
||||
|
||||
func ExampleClient_Header() {
|
||||
var (
|
||||
url = "http://127.0.0.1:8999/header"
|
||||
|
Loading…
Reference in New Issue
Block a user