http.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package util
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "encoding/xml"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "mime/multipart"
  10. "net/http"
  11. "os"
  12. )
  13. //HTTPGet get 请求
  14. func HTTPGet(uri string) ([]byte, error) {
  15. response, err := http.Get(uri)
  16. if err != nil {
  17. return nil, err
  18. }
  19. defer response.Body.Close()
  20. if response.StatusCode != http.StatusOK {
  21. return nil, fmt.Errorf("http get error : uri=%v , statusCode=%v", uri, response.StatusCode)
  22. }
  23. return ioutil.ReadAll(response.Body)
  24. }
  25. //PostJSON post json 数据请求
  26. func PostJSON(uri string, obj interface{}) ([]byte, error) {
  27. jsonData, err := json.Marshal(obj)
  28. if err != nil {
  29. return nil, err
  30. }
  31. jsonData = bytes.Replace(jsonData, []byte("\\u003c"), []byte("<"), -1)
  32. jsonData = bytes.Replace(jsonData, []byte("\\u003e"), []byte(">"), -1)
  33. jsonData = bytes.Replace(jsonData, []byte("\\u0026"), []byte("&"), -1)
  34. body := bytes.NewBuffer(jsonData)
  35. response, err := http.Post(uri, "application/json;charset=utf-8", body)
  36. if err != nil {
  37. return nil, err
  38. }
  39. defer response.Body.Close()
  40. if response.StatusCode != http.StatusOK {
  41. return nil, fmt.Errorf("http get error : uri=%v , statusCode=%v", uri, response.StatusCode)
  42. }
  43. return ioutil.ReadAll(response.Body)
  44. }
  45. //PostFile 上传文件
  46. func PostFile(fieldname, filename, uri string) ([]byte, error) {
  47. fields := []MultipartFormField{
  48. {
  49. IsFile: true,
  50. Fieldname: fieldname,
  51. Filename: filename,
  52. },
  53. }
  54. return PostMultipartForm(fields, uri)
  55. }
  56. //MultipartFormField 保存文件或其他字段信息
  57. type MultipartFormField struct {
  58. IsFile bool
  59. Fieldname string
  60. Value []byte
  61. Filename string
  62. }
  63. //PostMultipartForm 上传文件或其他多个字段
  64. func PostMultipartForm(fields []MultipartFormField, uri string) (respBody []byte, err error) {
  65. bodyBuf := &bytes.Buffer{}
  66. bodyWriter := multipart.NewWriter(bodyBuf)
  67. for _, field := range fields {
  68. if field.IsFile {
  69. fileWriter, e := bodyWriter.CreateFormFile(field.Fieldname, field.Filename)
  70. if e != nil {
  71. err = fmt.Errorf("error writing to buffer , err=%v", e)
  72. return
  73. }
  74. fh, e := os.Open(field.Filename)
  75. if e != nil {
  76. err = fmt.Errorf("error opening file , err=%v", e)
  77. return
  78. }
  79. defer fh.Close()
  80. if _, err = io.Copy(fileWriter, fh); err != nil {
  81. return
  82. }
  83. } else {
  84. partWriter, e := bodyWriter.CreateFormField(field.Fieldname)
  85. if e != nil {
  86. err = e
  87. return
  88. }
  89. valueReader := bytes.NewReader(field.Value)
  90. if _, err = io.Copy(partWriter, valueReader); err != nil {
  91. return
  92. }
  93. }
  94. }
  95. contentType := bodyWriter.FormDataContentType()
  96. bodyWriter.Close()
  97. resp, e := http.Post(uri, contentType, bodyBuf)
  98. if e != nil {
  99. err = e
  100. return
  101. }
  102. defer resp.Body.Close()
  103. if resp.StatusCode != http.StatusOK {
  104. return nil, err
  105. }
  106. respBody, err = ioutil.ReadAll(resp.Body)
  107. return
  108. }
  109. //PostXML perform a HTTP/POST request with XML body
  110. func PostXML(uri string, obj interface{}) ([]byte, error) {
  111. xmlData, err := xml.Marshal(obj)
  112. if err != nil {
  113. return nil, err
  114. }
  115. body := bytes.NewBuffer(xmlData)
  116. response, err := http.Post(uri, "application/xml;charset=utf-8", body)
  117. if err != nil {
  118. return nil, err
  119. }
  120. defer response.Body.Close()
  121. if response.StatusCode != http.StatusOK {
  122. return nil, fmt.Errorf("http code error : uri=%v , statusCode=%v", uri, response.StatusCode)
  123. }
  124. return ioutil.ReadAll(response.Body)
  125. }