httper.go 4.5 KB

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