httper.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package httper
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io"
  6. "io/ioutil"
  7. "net/http"
  8. "time"
  9. "github.com/IceWhaleTech/CasaOS/pkg/config"
  10. "github.com/tidwall/gjson"
  11. )
  12. //发送GET请求
  13. //url:请求地址
  14. //response:请求返回的内容
  15. func Get(url string, head map[string]string) (response string) {
  16. client := &http.Client{Timeout: 30 * time.Second}
  17. req, err := http.NewRequest("GET", url, nil)
  18. for k, v := range head {
  19. req.Header.Add(k, v)
  20. }
  21. if err != nil {
  22. return ""
  23. }
  24. resp, err := client.Do(req)
  25. if err != nil {
  26. //需要错误日志的处理
  27. //loger.Error(error)
  28. return ""
  29. //panic(error)
  30. }
  31. defer resp.Body.Close()
  32. var buffer [512]byte
  33. result := bytes.NewBuffer(nil)
  34. for {
  35. n, err := resp.Body.Read(buffer[0:])
  36. result.Write(buffer[0:n])
  37. if err != nil && err == io.EOF {
  38. break
  39. } else if err != nil {
  40. //loger.Error(err)
  41. return ""
  42. // panic(err)
  43. }
  44. }
  45. response = result.String()
  46. return
  47. }
  48. //发送POST请求
  49. //url:请求地址,data:POST请求提交的数据,contentType:请求体格式,如:application/json
  50. //content:请求放回的内容
  51. func Post(url string, data []byte, contentType string, head map[string]string) (content string) {
  52. req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
  53. req.Header.Add("content-type", contentType)
  54. for k, v := range head {
  55. req.Header.Add(k, v)
  56. }
  57. if err != nil {
  58. panic(err)
  59. }
  60. client := &http.Client{Timeout: 5 * time.Second}
  61. resp, error := client.Do(req)
  62. if error != nil {
  63. panic(error)
  64. }
  65. defer resp.Body.Close()
  66. result, _ := ioutil.ReadAll(resp.Body)
  67. content = string(result)
  68. return
  69. }
  70. //发送POST请求
  71. //url:请求地址,data:POST请求提交的数据,contentType:请求体格式,如:application/json
  72. //content:请求放回的内容
  73. func ZeroTierPost(url string, data map[string]string, head map[string]string, cookies []*http.Cookie) (content string, code int) {
  74. b, _ := json.Marshal(data)
  75. req, err := http.NewRequest("POST", url, bytes.NewReader(b))
  76. for _, cookie := range cookies {
  77. req.AddCookie(cookie)
  78. }
  79. for k, v := range head {
  80. req.Header.Add(k, v)
  81. }
  82. if err != nil {
  83. panic(err)
  84. }
  85. client := &http.Client{Timeout: 20 * time.Second}
  86. resp, error := client.Do(req)
  87. if error != nil {
  88. panic(error)
  89. }
  90. defer resp.Body.Close()
  91. code = resp.StatusCode
  92. result, _ := ioutil.ReadAll(resp.Body)
  93. content = string(result)
  94. return
  95. }
  96. //发送POST请求
  97. //url:请求地址,data:POST请求提交的数据,contentType:请求体格式,如:application/json
  98. //content:请求放回的内容
  99. func ZeroTierPostJson(url string, data string, head map[string]string) (content string, code int) {
  100. var postData *bytes.Buffer
  101. jsonStr := []byte(data)
  102. postData = bytes.NewBuffer(jsonStr)
  103. req, err := http.NewRequest("POST", url, postData)
  104. for k, v := range head {
  105. req.Header.Add(k, v)
  106. }
  107. if err != nil {
  108. panic(err)
  109. }
  110. client := &http.Client{Timeout: 20 * time.Second}
  111. resp, error := client.Do(req)
  112. if error != nil {
  113. panic(error)
  114. }
  115. defer resp.Body.Close()
  116. result, _ := ioutil.ReadAll(resp.Body)
  117. content = string(result)
  118. code = resp.StatusCode
  119. return
  120. }
  121. func ZeroTierDelete(url string, head map[string]string) (content string, code int) {
  122. req, err := http.NewRequest("DELETE", url, nil)
  123. for k, v := range head {
  124. req.Header.Add(k, v)
  125. }
  126. if err != nil {
  127. panic(err)
  128. }
  129. client := &http.Client{Timeout: 20 * time.Second}
  130. resp, error := client.Do(req)
  131. if error != nil {
  132. panic(error)
  133. }
  134. defer resp.Body.Close()
  135. result, _ := ioutil.ReadAll(resp.Body)
  136. content = string(result)
  137. code = resp.StatusCode
  138. return
  139. }
  140. //发送POST请求
  141. //url:请求地址,data:POST请求提交的数据,contentType:请求体格式,如:application/json
  142. //content:请求放回的内容
  143. func ZeroTierGet(url string, head map[string]string) (content string, code int) {
  144. req, err := http.NewRequest(http.MethodGet, url, nil)
  145. for k, v := range head {
  146. req.Header.Add(k, v)
  147. }
  148. if err != nil {
  149. panic(err)
  150. }
  151. client := &http.Client{Timeout: 20 * time.Second}
  152. resp, error := client.Do(req)
  153. if error != nil {
  154. panic(error)
  155. }
  156. defer resp.Body.Close()
  157. code = resp.StatusCode
  158. result, _ := ioutil.ReadAll(resp.Body)
  159. content = string(result)
  160. return
  161. }
  162. //发送GET请求
  163. //url:请求地址
  164. //response:请求返回的内容
  165. func OasisGet(url string) (response string) {
  166. head := make(map[string]string)
  167. t := make(chan string)
  168. go func() {
  169. str := Get(config.ServerInfo.ServerApi+"/token", nil)
  170. t <- gjson.Get(str, "data").String()
  171. }()
  172. head["Authorization"] = <-t
  173. return Get(url, head)
  174. }