requst_http.go 4.4 KB

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