微信相关接口
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
2.3 KiB

  1. package wechat
  2. import (
  3. "bytes"
  4. "encoding/xml"
  5. "io/ioutil"
  6. "log"
  7. "net/http"
  8. "strings"
  9. )
  10. /**
  11. * 自定义HTTP请求
  12. */
  13. func SendHttp(method, url string, param map[string]string, header ...map[string]string) ([]byte, error) {
  14. httpClient := &http.Client{}
  15. paramStr := ""
  16. if len(param) > 0 {
  17. for key, value := range param {
  18. paramStr += key + "=" + value + "&"
  19. }
  20. paramStr = paramStr[0 : len(paramStr)-1]
  21. }
  22. req, err := http.NewRequest(method, url, strings.NewReader(paramStr))
  23. if err != nil {
  24. return []byte(""), err
  25. }
  26. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  27. if len(header) > 0 {
  28. for _, item := range header {
  29. for k, v := range item {
  30. req.Header.Set(k, v)
  31. }
  32. }
  33. }
  34. resp, err := httpClient.Do(req)
  35. if err != nil {
  36. return []byte(""), err
  37. }
  38. defer resp.Body.Close()
  39. body, err := ioutil.ReadAll(resp.Body)
  40. if err != nil {
  41. return []byte(""), err
  42. }
  43. return body, nil
  44. }
  45. /**
  46. * send xml
  47. */
  48. func SendXml(method, url string, requestxml interface{}, header ...map[string]string) ([]byte, error) {
  49. bytexml, err := xml.Marshal(&requestxml)
  50. if err != nil {
  51. return []byte(""), err
  52. }
  53. httpClient := &http.Client{}
  54. req, err := http.NewRequest(method, url, bytes.NewBuffer(bytexml))
  55. if err != nil {
  56. return []byte(""), err
  57. }
  58. req.Header.Add("Content-Type", "application/xml; charset=utf-8")
  59. if len(header) > 0 {
  60. for _, item := range header {
  61. for k, v := range item {
  62. req.Header.Add(k, v)
  63. }
  64. }
  65. }
  66. resp, err := httpClient.Do(req)
  67. if err != nil {
  68. return []byte(""), err
  69. }
  70. defer resp.Body.Close()
  71. body, err := ioutil.ReadAll(resp.Body)
  72. if err != nil {
  73. return []byte(""), err
  74. }
  75. return body, nil
  76. }
  77. /**
  78. * post 请求
  79. */
  80. func PostJson(url string, param []byte, header ...map[string]string) ([]byte, error) {
  81. httpClient := &http.Client{}
  82. log.Println("param_code", bytes.NewBuffer(param))
  83. req, err := http.NewRequest("POST", url, bytes.NewBuffer(param))
  84. if err != nil {
  85. return []byte(""), err
  86. }
  87. req.Header.Set("Content-Type", "application/json")
  88. if len(header) > 0 {
  89. for _, item := range header {
  90. for k, v := range item {
  91. req.Header[k] = []string{v}
  92. }
  93. }
  94. }
  95. resp, err := httpClient.Do(req)
  96. if err != nil {
  97. return []byte(""), err
  98. }
  99. defer resp.Body.Close()
  100. body, err := ioutil.ReadAll(resp.Body)
  101. if err != nil {
  102. return []byte(""), err
  103. }
  104. return body, nil
  105. }