efd285a6 by astaxie

update httplib support https

1 parent 3a0b2e3b
...@@ -43,4 +43,12 @@ set post timeout: ...@@ -43,4 +43,12 @@ set post timeout:
43 ## debug 43 ## debug
44 if you want to debug the request info, set the debug on 44 if you want to debug the request info, set the debug on
45 45
46 httplib.Get("").Debug(true)
...\ No newline at end of file ...\ No newline at end of file
46 httplib.Get("").Debug(true)
47
48 ## support HTTPS client
49 if request url is https. You can set the client support tsl:
50
51 httplib.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
52
53 more info about the tls.Config please visit http://golang.org/pkg/crypto/tls/#Config
54
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -2,6 +2,7 @@ package httplib ...@@ -2,6 +2,7 @@ package httplib
2 2
3 import ( 3 import (
4 "bytes" 4 "bytes"
5 "crypto/tls"
5 "encoding/json" 6 "encoding/json"
6 "encoding/xml" 7 "encoding/xml"
7 "io" 8 "io"
...@@ -22,7 +23,7 @@ func Get(url string) *BeegoHttpRequest { ...@@ -22,7 +23,7 @@ func Get(url string) *BeegoHttpRequest {
22 req.Method = "GET" 23 req.Method = "GET"
23 req.Header = http.Header{} 24 req.Header = http.Header{}
24 req.Header.Set("User-Agent", defaultUserAgent) 25 req.Header.Set("User-Agent", defaultUserAgent)
25 return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second} 26 return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second, nil}
26 } 27 }
27 28
28 func Post(url string) *BeegoHttpRequest { 29 func Post(url string) *BeegoHttpRequest {
...@@ -30,7 +31,7 @@ func Post(url string) *BeegoHttpRequest { ...@@ -30,7 +31,7 @@ func Post(url string) *BeegoHttpRequest {
30 req.Method = "POST" 31 req.Method = "POST"
31 req.Header = http.Header{} 32 req.Header = http.Header{}
32 req.Header.Set("User-Agent", defaultUserAgent) 33 req.Header.Set("User-Agent", defaultUserAgent)
33 return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second} 34 return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second, nil}
34 } 35 }
35 36
36 func Put(url string) *BeegoHttpRequest { 37 func Put(url string) *BeegoHttpRequest {
...@@ -38,7 +39,7 @@ func Put(url string) *BeegoHttpRequest { ...@@ -38,7 +39,7 @@ func Put(url string) *BeegoHttpRequest {
38 req.Method = "PUT" 39 req.Method = "PUT"
39 req.Header = http.Header{} 40 req.Header = http.Header{}
40 req.Header.Set("User-Agent", defaultUserAgent) 41 req.Header.Set("User-Agent", defaultUserAgent)
41 return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second} 42 return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second, nil}
42 } 43 }
43 44
44 func Delete(url string) *BeegoHttpRequest { 45 func Delete(url string) *BeegoHttpRequest {
...@@ -46,7 +47,7 @@ func Delete(url string) *BeegoHttpRequest { ...@@ -46,7 +47,7 @@ func Delete(url string) *BeegoHttpRequest {
46 req.Method = "DELETE" 47 req.Method = "DELETE"
47 req.Header = http.Header{} 48 req.Header = http.Header{}
48 req.Header.Set("User-Agent", defaultUserAgent) 49 req.Header.Set("User-Agent", defaultUserAgent)
49 return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second} 50 return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second, nil}
50 } 51 }
51 52
52 func Head(url string) *BeegoHttpRequest { 53 func Head(url string) *BeegoHttpRequest {
...@@ -54,7 +55,7 @@ func Head(url string) *BeegoHttpRequest { ...@@ -54,7 +55,7 @@ func Head(url string) *BeegoHttpRequest {
54 req.Method = "HEAD" 55 req.Method = "HEAD"
55 req.Header = http.Header{} 56 req.Header = http.Header{}
56 req.Header.Set("User-Agent", defaultUserAgent) 57 req.Header.Set("User-Agent", defaultUserAgent)
57 return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second} 58 return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second, nil}
58 } 59 }
59 60
60 type BeegoHttpRequest struct { 61 type BeegoHttpRequest struct {
...@@ -64,6 +65,7 @@ type BeegoHttpRequest struct { ...@@ -64,6 +65,7 @@ type BeegoHttpRequest struct {
64 showdebug bool 65 showdebug bool
65 connectTimeout time.Duration 66 connectTimeout time.Duration
66 readWriteTimeout time.Duration 67 readWriteTimeout time.Duration
68 tlsClientConfig *tls.Config
67 } 69 }
68 70
69 func (b *BeegoHttpRequest) Debug(isdebug bool) *BeegoHttpRequest { 71 func (b *BeegoHttpRequest) Debug(isdebug bool) *BeegoHttpRequest {
...@@ -77,6 +79,11 @@ func (b *BeegoHttpRequest) SetTimeout(connectTimeout, readWriteTimeout time.Dura ...@@ -77,6 +79,11 @@ func (b *BeegoHttpRequest) SetTimeout(connectTimeout, readWriteTimeout time.Dura
77 return b 79 return b
78 } 80 }
79 81
82 func (b *BeegoHttpRequest) SetTLSClientConfig(config *tls.Config) *BeegoHttpRequest {
83 b.tlsClientConfig = config
84 return b
85 }
86
80 func (b *BeegoHttpRequest) Header(key, value string) *BeegoHttpRequest { 87 func (b *BeegoHttpRequest) Header(key, value string) *BeegoHttpRequest {
81 b.req.Header.Set(key, value) 88 b.req.Header.Set(key, value)
82 return b 89 return b
...@@ -146,7 +153,8 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { ...@@ -146,7 +153,8 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
146 153
147 client := &http.Client{ 154 client := &http.Client{
148 Transport: &http.Transport{ 155 Transport: &http.Transport{
149 Dial: TimeoutDialer(b.connectTimeout, b.readWriteTimeout), 156 TLSClientConfig: b.tlsClientConfig,
157 Dial: TimeoutDialer(b.connectTimeout, b.readWriteTimeout),
150 }, 158 },
151 } 159 }
152 resp, err := client.Do(b.req) 160 resp, err := client.Do(b.req)
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!