httpClient.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package httpClient
  2. import (
  3. "bytes"
  4. "crypto/tls"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "net/url"
  9. "strings"
  10. "time"
  11. )
  12. const (
  13. timeout = 5 * time.Second
  14. )
  15. type HttpRequest struct {
  16. Url string
  17. Method string
  18. TimeOut time.Duration
  19. Values map[string]string
  20. Bytes []byte
  21. }
  22. type HttpRequestWithHead struct {
  23. Url string
  24. Method string
  25. Head map[string]string
  26. TimeOut time.Duration
  27. Query map[string]string
  28. Body []byte
  29. }
  30. func HttpPostWithHead(path string, head map[string]string,
  31. query map[string]string, body []byte) ([]byte, error) {
  32. if query != nil {
  33. path += joinGetRequestArgs(query)
  34. }
  35. h := HttpRequestWithHead{
  36. Url: path,
  37. Method: http.MethodPost,
  38. Head: head,
  39. Body: body,
  40. TimeOut: timeout,
  41. }
  42. return h.request()
  43. }
  44. func HttpPutWithHead(path string, head map[string]string,
  45. query map[string]string, body []byte) ([]byte, error) {
  46. if query != nil {
  47. path += joinGetRequestArgs(query)
  48. }
  49. path += joinGetRequestArgs(query)
  50. h := HttpRequestWithHead{
  51. Url: path,
  52. Method: http.MethodPut,
  53. Head: head,
  54. Body: body,
  55. TimeOut: timeout,
  56. }
  57. return h.request()
  58. }
  59. func HttpPost(path string, data map[string]string) ([]byte, error) {
  60. h := HttpRequest{
  61. Url: path,
  62. Method: http.MethodPost,
  63. Values: data,
  64. TimeOut: timeout,
  65. }
  66. return h.request()
  67. }
  68. func HttpPostBytes(path string, data []byte) ([]byte, error) {
  69. h := HttpRequest{
  70. Url: path,
  71. Method: http.MethodPost,
  72. TimeOut: timeout,
  73. Bytes: data,
  74. }
  75. return h.requestBytes()
  76. }
  77. func HttpPut(path string, data map[string]string) ([]byte, error) {
  78. h := HttpRequest{
  79. Url: path,
  80. Method: http.MethodPut,
  81. Values: data,
  82. TimeOut: timeout,
  83. }
  84. return h.request()
  85. }
  86. func HttpDelete(path string, data map[string]string) ([]byte, error) {
  87. h := HttpRequest{
  88. Url: path,
  89. Method: http.MethodDelete,
  90. Values: data,
  91. TimeOut: timeout,
  92. }
  93. return h.request()
  94. }
  95. func HttpGet(path string, data map[string]string) ([]byte, error) {
  96. path += joinGetRequestArgs(data)
  97. h := HttpRequest{
  98. Url: path,
  99. Method: http.MethodGet,
  100. TimeOut: timeout,
  101. }
  102. return h.request()
  103. }
  104. func (h HttpRequest) request() ([]byte, error) {
  105. values := url.Values{}
  106. if h.Values != nil {
  107. for k, v := range h.Values {
  108. values.Add(k, v)
  109. }
  110. }
  111. request, err := http.NewRequest(h.Method, h.Url, strings.NewReader(values.Encode()))
  112. if h.Method == http.MethodPost ||
  113. h.Method == http.MethodPut ||
  114. h.Method == http.MethodDelete {
  115. request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  116. }
  117. //跳过证书验证
  118. tr := &http.Transport{
  119. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  120. }
  121. client := http.Client{
  122. Transport: tr,
  123. Timeout: timeout,
  124. }
  125. resp, err := client.Do(request)
  126. if err != nil {
  127. return nil, err
  128. }
  129. defer resp.Body.Close()
  130. if resp.StatusCode != http.StatusOK {
  131. return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode)
  132. }
  133. contents, err := ioutil.ReadAll(resp.Body)
  134. if err != nil {
  135. return nil, err
  136. }
  137. return contents, nil
  138. }
  139. func (h HttpRequestWithHead) request() ([]byte, error) {
  140. request, err := http.NewRequest(h.Method, h.Url, bytes.NewReader(h.Body))
  141. if h.Method == http.MethodPost ||
  142. h.Method == http.MethodPut ||
  143. h.Method == http.MethodDelete {
  144. request.Header.Add("Content-Type", "application/json")
  145. }
  146. for k, v := range h.Head {
  147. request.Header.Add(k, v)
  148. }
  149. //跳过证书验证
  150. tr := &http.Transport{
  151. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  152. }
  153. client := http.Client{
  154. Transport: tr,
  155. Timeout: timeout,
  156. }
  157. resp, err := client.Do(request)
  158. if err != nil {
  159. return nil, err
  160. }
  161. defer resp.Body.Close()
  162. if resp.StatusCode != http.StatusOK {
  163. return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode)
  164. }
  165. contents, err := ioutil.ReadAll(resp.Body)
  166. if err != nil {
  167. return nil, err
  168. }
  169. return contents, nil
  170. }
  171. func (h HttpRequest) requestBytes() ([]byte, error) {
  172. request, err := http.NewRequest(h.Method, h.Url, bytes.NewReader(h.Bytes))
  173. if h.Method == http.MethodPost ||
  174. h.Method == http.MethodPut ||
  175. h.Method == http.MethodDelete {
  176. request.Header.Add("Content-Type", "application/json")
  177. }
  178. //跳过证书验证
  179. tr := &http.Transport{
  180. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  181. }
  182. client := http.Client{
  183. Transport: tr,
  184. Timeout: timeout,
  185. }
  186. resp, err := client.Do(request)
  187. if err != nil {
  188. return nil, err
  189. }
  190. defer resp.Body.Close()
  191. if resp.StatusCode != http.StatusOK {
  192. return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode)
  193. }
  194. contents, err := ioutil.ReadAll(resp.Body)
  195. if err != nil {
  196. return nil, err
  197. }
  198. return contents, nil
  199. }
  200. func joinGetRequestArgs(data map[string]string) string {
  201. var path string
  202. if data != nil {
  203. path += "?"
  204. for k, v := range data {
  205. path += k + "=" + v + "&"
  206. }
  207. path = strings.TrimRight(path, "&")
  208. return path
  209. }
  210. return path
  211. }