requst_http.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package utils
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "net/http"
  8. "strings"
  9. "time"
  10. )
  11. func HttpGet(url string, params map[string]string) (result []byte, err error) {
  12. req, err := http.NewRequest("GET", url, nil)
  13. if err != nil {
  14. fmt.Println(err.Error())
  15. return
  16. }
  17. q := req.URL.Query()
  18. for k, v := range params {
  19. q.Add(k, v)
  20. }
  21. req.URL.RawQuery = q.Encode()
  22. fmt.Println(req.URL.String())
  23. var resp *http.Response
  24. resp, err = http.DefaultClient.Do(req)
  25. if err != nil || resp == nil {
  26. return
  27. }
  28. output, err := ioutil.ReadAll(resp.Body)
  29. if err != nil {
  30. return
  31. }
  32. result = output
  33. defer func() {
  34. if resp != nil {
  35. resp.Body.Close()
  36. }
  37. }()
  38. return
  39. }
  40. func HttpPost(url string, params interface{}) (result []byte, err error) {
  41. data, _ := json.Marshal(params)
  42. client := &http.Client{}
  43. req, err := http.NewRequest("POST", url, strings.NewReader(string(data)))
  44. if err != nil {
  45. return
  46. }
  47. fmt.Println(url)
  48. req.Header.Set("Content-Type", "application/json;charset=UTF-8")
  49. resp, err := client.Do(req)
  50. if err != nil {
  51. return
  52. }
  53. defer resp.Body.Close()
  54. body, err := ioutil.ReadAll(resp.Body)
  55. if err != nil {
  56. return
  57. }
  58. result = body
  59. return
  60. }
  61. func HttpPostReplyCode(url string, params interface{}) (result []byte, code int, err error) {
  62. data, _ := json.Marshal(params)
  63. client := &http.Client{}
  64. req, err := http.NewRequest("POST", url, strings.NewReader(string(data)))
  65. if err != nil {
  66. return
  67. }
  68. fmt.Println(url)
  69. req.Header.Set("Content-Type", "application/json;charset=UTF-8")
  70. resp, err := client.Do(req)
  71. if err != nil {
  72. return
  73. }
  74. code = resp.StatusCode
  75. defer resp.Body.Close()
  76. body, err := ioutil.ReadAll(resp.Body)
  77. if err != nil {
  78. return
  79. }
  80. result = body
  81. return
  82. }
  83. func HttpGetReplyCode(url string, params map[string]string) (result []byte, code int, err error) {
  84. req, err := http.NewRequest("GET", url, nil)
  85. if err != nil {
  86. fmt.Println(err.Error())
  87. return
  88. }
  89. q := req.URL.Query()
  90. for k, v := range params {
  91. q.Add(k, v)
  92. }
  93. req.URL.RawQuery = q.Encode()
  94. fmt.Println(req.URL.String())
  95. var resp *http.Response
  96. resp, err = http.DefaultClient.Do(req)
  97. if err != nil || resp == nil {
  98. return
  99. }
  100. code = resp.StatusCode
  101. output, err := ioutil.ReadAll(resp.Body)
  102. if err != nil {
  103. return
  104. }
  105. result = output
  106. defer func() {
  107. if resp != nil {
  108. resp.Body.Close()
  109. }
  110. }()
  111. return
  112. }
  113. func HttpPutReplyCode(url string, params interface{}) (result []byte, code int, err error) {
  114. data, _ := json.Marshal(params)
  115. client := &http.Client{}
  116. req, err := http.NewRequest("PUT", url, strings.NewReader(string(data)))
  117. if err != nil {
  118. return
  119. }
  120. fmt.Println(url)
  121. req.Header.Set("Content-Type", "application/json;charset=UTF-8")
  122. resp, err := client.Do(req)
  123. if err != nil {
  124. return
  125. }
  126. code = resp.StatusCode
  127. defer resp.Body.Close()
  128. body, err := ioutil.ReadAll(resp.Body)
  129. if err != nil {
  130. return
  131. }
  132. result = body
  133. return
  134. }
  135. func HttpDeleteReplyCode(url string, params interface{}) (result []byte, code int, err error) {
  136. data, _ := json.Marshal(params)
  137. client := &http.Client{}
  138. req, err := http.NewRequest("DELETE", url, strings.NewReader(string(data)))
  139. if err != nil {
  140. return
  141. }
  142. fmt.Println(url)
  143. req.Header.Set("Content-Type", "application/json;charset=UTF-8")
  144. resp, err := client.Do(req)
  145. if err != nil {
  146. return
  147. }
  148. code = resp.StatusCode
  149. defer resp.Body.Close()
  150. body, err := ioutil.ReadAll(resp.Body)
  151. if err != nil {
  152. return
  153. }
  154. result = body
  155. return
  156. }
  157. func Request(method, urlPath string, reqBody io.Reader, header map[string]string, cookies []*http.Cookie) (resp *http.Response, err error) {
  158. req, err := http.NewRequest(method, urlPath, reqBody)
  159. if err != nil {
  160. return
  161. }
  162. for k, v := range header {
  163. req.Header.Set(k, v)
  164. }
  165. for _, v := range cookies {
  166. req.AddCookie(v)
  167. }
  168. var (
  169. client = http.Client{
  170. Timeout: 15 * time.Second,
  171. //Transport: &http.Transport{Proxy: http.ProxyURL(&url.URL{Host: "localhost:8888"})},
  172. }
  173. )
  174. if resp, err = client.Do(req); err != nil {
  175. return
  176. }
  177. defer resp.Body.Close()
  178. return
  179. }