mirror of
https://github.com/astaxie/beego.git
synced 2025-02-16 22:37:02 +00:00
update httplib support https
This commit is contained in:
parent
3a0b2e3b95
commit
efd285a6c4
@ -43,4 +43,12 @@ set post timeout:
|
|||||||
## debug
|
## debug
|
||||||
if you want to debug the request info, set the debug on
|
if you want to debug the request info, set the debug on
|
||||||
|
|
||||||
httplib.Get("").Debug(true)
|
httplib.Get("").Debug(true)
|
||||||
|
|
||||||
|
## support HTTPS client
|
||||||
|
if request url is https. You can set the client support tsl:
|
||||||
|
|
||||||
|
httplib.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
|
||||||
|
|
||||||
|
more info about the tls.Config please visit http://golang.org/pkg/crypto/tls/#Config
|
||||||
|
|
@ -2,6 +2,7 @@ package httplib
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"crypto/tls"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"io"
|
"io"
|
||||||
@ -22,7 +23,7 @@ func Get(url string) *BeegoHttpRequest {
|
|||||||
req.Method = "GET"
|
req.Method = "GET"
|
||||||
req.Header = http.Header{}
|
req.Header = http.Header{}
|
||||||
req.Header.Set("User-Agent", defaultUserAgent)
|
req.Header.Set("User-Agent", defaultUserAgent)
|
||||||
return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second}
|
return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second, nil}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Post(url string) *BeegoHttpRequest {
|
func Post(url string) *BeegoHttpRequest {
|
||||||
@ -30,7 +31,7 @@ func Post(url string) *BeegoHttpRequest {
|
|||||||
req.Method = "POST"
|
req.Method = "POST"
|
||||||
req.Header = http.Header{}
|
req.Header = http.Header{}
|
||||||
req.Header.Set("User-Agent", defaultUserAgent)
|
req.Header.Set("User-Agent", defaultUserAgent)
|
||||||
return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second}
|
return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second, nil}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Put(url string) *BeegoHttpRequest {
|
func Put(url string) *BeegoHttpRequest {
|
||||||
@ -38,7 +39,7 @@ func Put(url string) *BeegoHttpRequest {
|
|||||||
req.Method = "PUT"
|
req.Method = "PUT"
|
||||||
req.Header = http.Header{}
|
req.Header = http.Header{}
|
||||||
req.Header.Set("User-Agent", defaultUserAgent)
|
req.Header.Set("User-Agent", defaultUserAgent)
|
||||||
return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second}
|
return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second, nil}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Delete(url string) *BeegoHttpRequest {
|
func Delete(url string) *BeegoHttpRequest {
|
||||||
@ -46,7 +47,7 @@ func Delete(url string) *BeegoHttpRequest {
|
|||||||
req.Method = "DELETE"
|
req.Method = "DELETE"
|
||||||
req.Header = http.Header{}
|
req.Header = http.Header{}
|
||||||
req.Header.Set("User-Agent", defaultUserAgent)
|
req.Header.Set("User-Agent", defaultUserAgent)
|
||||||
return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second}
|
return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second, nil}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Head(url string) *BeegoHttpRequest {
|
func Head(url string) *BeegoHttpRequest {
|
||||||
@ -54,7 +55,7 @@ func Head(url string) *BeegoHttpRequest {
|
|||||||
req.Method = "HEAD"
|
req.Method = "HEAD"
|
||||||
req.Header = http.Header{}
|
req.Header = http.Header{}
|
||||||
req.Header.Set("User-Agent", defaultUserAgent)
|
req.Header.Set("User-Agent", defaultUserAgent)
|
||||||
return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second}
|
return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second, nil}
|
||||||
}
|
}
|
||||||
|
|
||||||
type BeegoHttpRequest struct {
|
type BeegoHttpRequest struct {
|
||||||
@ -64,6 +65,7 @@ type BeegoHttpRequest struct {
|
|||||||
showdebug bool
|
showdebug bool
|
||||||
connectTimeout time.Duration
|
connectTimeout time.Duration
|
||||||
readWriteTimeout time.Duration
|
readWriteTimeout time.Duration
|
||||||
|
tlsClientConfig *tls.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BeegoHttpRequest) Debug(isdebug bool) *BeegoHttpRequest {
|
func (b *BeegoHttpRequest) Debug(isdebug bool) *BeegoHttpRequest {
|
||||||
@ -77,6 +79,11 @@ func (b *BeegoHttpRequest) SetTimeout(connectTimeout, readWriteTimeout time.Dura
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *BeegoHttpRequest) SetTLSClientConfig(config *tls.Config) *BeegoHttpRequest {
|
||||||
|
b.tlsClientConfig = config
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
func (b *BeegoHttpRequest) Header(key, value string) *BeegoHttpRequest {
|
func (b *BeegoHttpRequest) Header(key, value string) *BeegoHttpRequest {
|
||||||
b.req.Header.Set(key, value)
|
b.req.Header.Set(key, value)
|
||||||
return b
|
return b
|
||||||
@ -146,7 +153,8 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
|
|||||||
|
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Transport: &http.Transport{
|
Transport: &http.Transport{
|
||||||
Dial: TimeoutDialer(b.connectTimeout, b.readWriteTimeout),
|
TLSClientConfig: b.tlsClientConfig,
|
||||||
|
Dial: TimeoutDialer(b.connectTimeout, b.readWriteTimeout),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
resp, err := client.Do(b.req)
|
resp, err := client.Do(b.req)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user