http.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package lxCommon
  2. import (
  3. "bytes"
  4. "crypto/tls"
  5. "github.com/bytedance/sonic"
  6. "golang.org/x/net/publicsuffix"
  7. "io"
  8. "io/ioutil"
  9. "mime/multipart"
  10. "net/http"
  11. "net/http/cookiejar"
  12. "os"
  13. "path/filepath"
  14. "time"
  15. )
  16. // url 路径 filename 文件的上传参数
  17. func PostFile(url string, files map[string]string, ext map[string]string) ([]byte, error) {
  18. res := []byte{}
  19. //创建一个模拟的form中的一个选项,这个form项现在是空的
  20. bodyBuf := &bytes.Buffer{}
  21. bodyWriter := multipart.NewWriter(bodyBuf)
  22. for k, v := range files {
  23. //打开文件句柄操作
  24. file, err := os.Open(v)
  25. if err != nil {
  26. //fmt.Println("error opening file")
  27. return res, err
  28. }
  29. defer file.Close()
  30. //相当于现在还没选择文件, form项里选择文件的选项
  31. fileWriter, err := bodyWriter.CreateFormFile(k, filepath.Base(v))
  32. if err != nil {
  33. //fmt.Println("error writing to buffer")
  34. return res, err
  35. }
  36. //iocopy 这里相当于选择了文件,将文件放到form中
  37. _, err = io.Copy(fileWriter, file)
  38. if err != nil {
  39. return res, err
  40. }
  41. }
  42. //获取上传文件的类型,multipart/form-data; boundary=...
  43. contentType := bodyWriter.FormDataContentType()
  44. //上传的其他参数
  45. for key, val := range ext {
  46. _ = bodyWriter.WriteField(key, val)
  47. }
  48. //这个很关键,必须这样写关闭,不能使用defer关闭,不然会导致错误
  49. bodyWriter.Close()
  50. //发送post请求到服务端
  51. resp, err := http.Post(url, contentType, bodyBuf)
  52. if err != nil {
  53. return res, err
  54. }
  55. defer resp.Body.Close()
  56. res, err = ioutil.ReadAll(resp.Body)
  57. if err != nil {
  58. return res, err
  59. }
  60. return res, err
  61. }
  62. func NewClient() *http.Client {
  63. tr := &http.Transport{
  64. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  65. }
  66. var c = &http.Client{
  67. Timeout: time.Duration(1000 * time.Second),
  68. Transport: tr,
  69. }
  70. options := cookiejar.Options{
  71. PublicSuffixList: publicsuffix.List,
  72. }
  73. curCookieJar, _ := cookiejar.New(&options)
  74. c.Jar = curCookieJar
  75. return c
  76. }
  77. func HttpJsonStr(c *http.Client, method, url string, header map[string]string, body io.Reader, v *string) error {
  78. req, err := http.NewRequest(method, url, body)
  79. if err != nil {
  80. return err
  81. }
  82. for k, v := range header {
  83. req.Header.Add(k, v)
  84. }
  85. resp, err := c.Do(req)
  86. if err != nil {
  87. return err
  88. }
  89. defer resp.Body.Close()
  90. by, err := ioutil.ReadAll(resp.Body)
  91. *v = string(by)
  92. return nil
  93. }
  94. func HttpJson(c *http.Client, method, url string, header map[string]string, body io.Reader, data interface{}) error {
  95. req, err := http.NewRequest(method, url, body)
  96. if err != nil {
  97. return err
  98. }
  99. for k, v := range header {
  100. req.Header.Add(k, v)
  101. }
  102. resp, err := c.Do(req)
  103. if err != nil {
  104. return err
  105. }
  106. defer resp.Body.Close()
  107. by, err := ioutil.ReadAll(resp.Body)
  108. if err != nil {
  109. return err
  110. }
  111. err = sonic.Unmarshal(by, data)
  112. return err
  113. }
  114. func HttpGetFile(url, path string) error {
  115. res, err := http.Get(url)
  116. if err != nil {
  117. return err
  118. }
  119. f, err := os.Create(path)
  120. defer f.Close()
  121. if err != nil {
  122. return err
  123. }
  124. _, err = io.Copy(f, res.Body)
  125. return err
  126. }
  127. // 流式上传文件
  128. func PostFilePipe(url string, files map[string]string) {
  129. r, w := io.Pipe()
  130. m := multipart.NewWriter(w)
  131. go func() {
  132. defer w.Close()
  133. defer m.Close()
  134. for k, f := range files {
  135. part, err := m.CreateFormFile(k, f)
  136. if err != nil {
  137. continue
  138. }
  139. file, err := os.Open(f)
  140. if err != nil {
  141. continue
  142. }
  143. defer file.Close()
  144. if _, err = io.Copy(part, file); err != nil {
  145. continue
  146. }
  147. }
  148. }()
  149. http.Post(url, m.FormDataContentType(), r)
  150. }