httper.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. req.BasicAuth()
  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 interface{}, contentType string) (content string) {
  53. jsonStr, _ := json.Marshal(data)
  54. req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
  55. req.Header.Add("content-type", contentType)
  56. if err != nil {
  57. panic(err)
  58. }
  59. client := &http.Client{Timeout: 5 * time.Second}
  60. resp, error := client.Do(req)
  61. if error != nil {
  62. panic(error)
  63. }
  64. defer resp.Body.Close()
  65. result, _ := ioutil.ReadAll(resp.Body)
  66. content = string(result)
  67. return
  68. }
  69. //发送POST请求
  70. //url:请求地址,data:POST请求提交的数据,contentType:请求体格式,如:application/json
  71. //content:请求放回的内容
  72. func ZeroTierPost(url string, data map[string]string, head map[string]string, cookies []*http.Cookie) (content string, code int) {
  73. b, _ := json.Marshal(data)
  74. req, err := http.NewRequest("POST", url, bytes.NewReader(b))
  75. for _, cookie := range cookies {
  76. req.AddCookie(cookie)
  77. }
  78. for k, v := range head {
  79. req.Header.Add(k, v)
  80. }
  81. if err != nil {
  82. panic(err)
  83. }
  84. client := &http.Client{Timeout: 20 * time.Second}
  85. resp, error := client.Do(req)
  86. if error != nil {
  87. panic(error)
  88. }
  89. defer resp.Body.Close()
  90. code = resp.StatusCode
  91. result, _ := ioutil.ReadAll(resp.Body)
  92. content = string(result)
  93. return
  94. }
  95. //发送POST请求
  96. //url:请求地址,data:POST请求提交的数据,contentType:请求体格式,如:application/json
  97. //content:请求放回的内容
  98. func ZeroTierPostJson(url string, data string, head map[string]string) (content string, code int) {
  99. var postData *bytes.Buffer
  100. jsonStr := []byte(data)
  101. postData = bytes.NewBuffer(jsonStr)
  102. req, err := http.NewRequest("POST", url, postData)
  103. for k, v := range head {
  104. req.Header.Add(k, v)
  105. }
  106. if err != nil {
  107. panic(err)
  108. }
  109. client := &http.Client{Timeout: 20 * time.Second}
  110. resp, error := client.Do(req)
  111. if error != nil {
  112. panic(error)
  113. }
  114. defer resp.Body.Close()
  115. result, _ := ioutil.ReadAll(resp.Body)
  116. content = string(result)
  117. code = resp.StatusCode
  118. return
  119. }
  120. func ZeroTierDelete(url string, head map[string]string) (content string, code int) {
  121. req, err := http.NewRequest("DELETE", url, nil)
  122. for k, v := range head {
  123. req.Header.Add(k, v)
  124. }
  125. if err != nil {
  126. panic(err)
  127. }
  128. client := &http.Client{Timeout: 20 * time.Second}
  129. resp, error := client.Do(req)
  130. if error != nil {
  131. panic(error)
  132. }
  133. defer resp.Body.Close()
  134. result, _ := ioutil.ReadAll(resp.Body)
  135. content = string(result)
  136. code = resp.StatusCode
  137. return
  138. }
  139. //发送POST请求
  140. //url:请求地址,data:POST请求提交的数据,contentType:请求体格式,如:application/json
  141. //content:请求放回的内容
  142. func ZeroTierGet(url string, head map[string]string) (content string, code int) {
  143. req, err := http.NewRequest(http.MethodGet, url, nil)
  144. for k, v := range head {
  145. req.Header.Add(k, v)
  146. }
  147. if err != nil {
  148. panic(err)
  149. }
  150. client := &http.Client{Timeout: 20 * time.Second}
  151. resp, error := client.Do(req)
  152. if error != nil {
  153. panic(error)
  154. }
  155. defer resp.Body.Close()
  156. code = resp.StatusCode
  157. result, _ := ioutil.ReadAll(resp.Body)
  158. content = string(result)
  159. return
  160. }
  161. //发送GET请求
  162. //url:请求地址
  163. //response:请求返回的内容
  164. func OasisGet(url string) (response string) {
  165. head := make(map[string]string)
  166. t := make(chan string)
  167. go func() {
  168. str := Get(config.ServerInfo.ServerApi+"/token", nil)
  169. t <- gjson.Get(str, "data").String()
  170. }()
  171. head["Authorization"] = <-t
  172. return Get(url, head)
  173. }