http.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package http
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "net/http"
  9. "unsafe"
  10. )
  11. func GetReq(url string) *http.Response {
  12. client := &http.Client{}
  13. req, err := http.NewRequest("GET", url, nil)
  14. if err != nil {
  15. //panic(err)
  16. fmt.Println(err.Error())
  17. }
  18. resp, err := client.Do(req)
  19. if err != nil {
  20. //panic(err)
  21. fmt.Println(err.Error())
  22. }
  23. //defer func(Body io.ReadCloser) {
  24. // err := Body.Close()
  25. // if err != nil {
  26. // fmt.Print("Body close err" + err.Error())
  27. // }
  28. //}(resp.Body)
  29. return resp
  30. }
  31. //func postReq(name string, age int) {
  32. // client := &http.Client{}
  33. //
  34. // req, err := http.NewRequest("POST", "http://localhost:5000/post",
  35. // strings.NewReader(fmt.Sprintf("name=%s&age=%d", name, age)))
  36. // if err != nil {
  37. // panic(err)
  38. // }
  39. //
  40. // req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  41. //
  42. // resp, err := client.Do(req)
  43. //
  44. // defer resp.Body.Close()
  45. //
  46. //}
  47. // PostReqJson 通过json提交post
  48. func PostReqJson(url string, json []byte) *http.Response {
  49. req, err := http.NewRequest("POST", url, bytes.NewBuffer(json))
  50. req.Header.Set("Content-Type", "application/json")
  51. client := &http.Client{}
  52. resp, err := client.Do(req)
  53. if err != nil {
  54. panic(err)
  55. }
  56. defer func(Body io.ReadCloser) {
  57. err := Body.Close()
  58. if err != nil {
  59. fmt.Print("Body close err" + err.Error())
  60. }
  61. }(resp.Body)
  62. return resp
  63. }
  64. type JsonPost struct {
  65. }
  66. func (jp *JsonPost) postReqJson2() {
  67. post := make(map[string]interface{})
  68. post["name"] = "王柳"
  69. post["age"] = 21
  70. bytesData, err := json.Marshal(post)
  71. if err != nil {
  72. fmt.Println(err.Error())
  73. return
  74. }
  75. reader := bytes.NewReader(bytesData)
  76. url := "http://localhost:5000/post"
  77. request, err := http.NewRequest("POST", url, reader)
  78. if err != nil {
  79. fmt.Println(err.Error())
  80. return
  81. }
  82. request.Header.Set("Content-Type", "application/json;charset=UTF-8")
  83. client := http.Client{}
  84. resp, err := client.Do(request)
  85. if err != nil {
  86. fmt.Println(err.Error())
  87. return
  88. }
  89. respBytes, err := ioutil.ReadAll(resp.Body)
  90. if err != nil {
  91. fmt.Println(err.Error())
  92. return
  93. }
  94. //byte数组直接转成string,优化内存
  95. str := (*string)(unsafe.Pointer(&respBytes))
  96. fmt.Println(*str)
  97. }
  98. // DeleteReq 发送delete请求
  99. func DeleteReq(url string) *http.Response {
  100. client := &http.Client{}
  101. req, err := http.NewRequest("DELETE", url, nil)
  102. if err != nil {
  103. panic(err)
  104. }
  105. resp, err := client.Do(req)
  106. if err != nil {
  107. panic(err)
  108. }
  109. return resp
  110. }