HttpUtil.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package NetUtils
  2. import (
  3. "errors"
  4. "forward-core/Utils"
  5. "io/ioutil"
  6. "net/http"
  7. "net/url"
  8. "strings"
  9. "bytes"
  10. "github.com/astaxie/beego"
  11. "github.com/astaxie/beego/logs"
  12. )
  13. func GetIP(c *beego.Controller) string {
  14. //utils.GetIP(&c.Controller)
  15. //也可以直接用 c.Ctx.Input.IP() 取真实IP
  16. ip := c.Ctx.Request.Header.Get("X-Real-IP")
  17. if ip != "" {
  18. return ip
  19. }
  20. ip = c.Ctx.Request.Header.Get("Remote_addr")
  21. if ip == "" {
  22. ip = c.Ctx.Request.RemoteAddr
  23. }
  24. return ip
  25. }
  26. func HttpGet(url string) (string, error) {
  27. resp, err := http.Get(url)
  28. if err != nil {
  29. logs.Error("HttpGet error: ", err)
  30. return "", err
  31. }
  32. if resp == nil {
  33. return "", errors.New("返回对象为空")
  34. }
  35. defer resp.Body.Close()
  36. result := ""
  37. body, err := ioutil.ReadAll(resp.Body)
  38. if err == nil {
  39. result = string(body)
  40. //logs.Debug("HttpGet result: ", result)
  41. } else {
  42. logs.Error("HttpGet error: ", err)
  43. }
  44. return result, nil
  45. }
  46. func HttpPostJsonReturnByte(url string, json string) ([]byte, error) {
  47. resp, err := http.Post(url, "application/json", strings.NewReader(json))
  48. if err != nil {
  49. logs.Error("HttpPostJson error: ", err)
  50. return nil, err
  51. }
  52. if resp == nil {
  53. return nil, errors.New("返回对象为空")
  54. }
  55. defer resp.Body.Close()
  56. body, err := ioutil.ReadAll(resp.Body)
  57. if err == nil {
  58. return body, err
  59. //logs.Debug("HttpPostJson result: ", result)
  60. } else {
  61. logs.Error("HttpPostJson error: ", err)
  62. return nil, err
  63. }
  64. }
  65. func HttpPost(url string, param map[string]string) (string, error) {
  66. var paramBuf bytes.Buffer
  67. paramBuf.WriteString("curTime=" + Utils.GetCurrentTime())
  68. for k, v := range param {
  69. paramBuf.WriteString("&" + k + "=" + v)
  70. }
  71. resp, err := http.Post(url, "application/x-www-form-urlencoded", strings.NewReader(paramBuf.String()))
  72. if err != nil {
  73. logs.Error("HttpPost error: ", err)
  74. return "", err
  75. }
  76. if resp == nil {
  77. return "", errors.New("返回对象为空")
  78. }
  79. defer resp.Body.Close()
  80. result := ""
  81. body, err := ioutil.ReadAll(resp.Body)
  82. if err == nil {
  83. result = string(body)
  84. logs.Debug("HttpPost result: ", result)
  85. } else {
  86. logs.Error("HttpPost error: ", err)
  87. }
  88. return result, nil
  89. }
  90. func UrlEncode(input string) string {
  91. if Utils.IsEmpty(input) {
  92. return ""
  93. }
  94. return url.QueryEscape(input)
  95. }
  96. func UrlDecode(input string) string {
  97. if Utils.IsEmpty(input) {
  98. return ""
  99. }
  100. result, err := url.QueryUnescape(input)
  101. if err != nil {
  102. return input
  103. } else {
  104. return result
  105. }
  106. }
粤ICP备19079148号