edgexCall.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package bo
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "mime/multipart"
  8. "net"
  9. "net/url"
  10. "os"
  11. "strconv"
  12. "time"
  13. //"fmt"
  14. "io/ioutil"
  15. "net/http"
  16. "strings"
  17. )
  18. func Get(url string) (interface{}, error) {
  19. //host := controllers.ApiController.set
  20. timeout := time.Duration(0 * time.Second)
  21. client := http.Client{Timeout: timeout}
  22. result, httperr := client.Get(url)
  23. if httperr != nil {
  24. return nil, httperr
  25. }
  26. defer result.Body.Close()
  27. body, err := ioutil.ReadAll(result.Body)
  28. if err != nil {
  29. return nil, err
  30. }
  31. return returnBody(body)
  32. }
  33. func Post(url string, data interface{}) (interface{}, error) {
  34. result, httperr := http.Post(url, "application/json;charset=utf-8", strings.NewReader(data.(string)))
  35. if httperr != nil {
  36. fmt.Println(url)
  37. fmt.Println(data)
  38. return nil, httperr
  39. }
  40. defer result.Body.Close()
  41. body, err := ioutil.ReadAll(result.Body)
  42. if err != nil {
  43. return nil, err
  44. }
  45. return returnBody(body)
  46. }
  47. func PostRaw(url string, data interface{}) (interface{}, error) {
  48. result, httperr := http.Post(url, "text/plain;charset=utf-8", strings.NewReader(data.(string)))
  49. if httperr != nil {
  50. fmt.Println(url)
  51. fmt.Println(data)
  52. return nil, httperr
  53. }
  54. defer result.Body.Close()
  55. body, err := ioutil.ReadAll(result.Body)
  56. if err != nil {
  57. return nil, err
  58. }
  59. return returnBody(body)
  60. }
  61. func PostForm(url string, data url.Values) (interface{}, error) {
  62. result, httperr := http.PostForm(url, data)
  63. if httperr != nil {
  64. fmt.Println(url)
  65. fmt.Println(data)
  66. return nil, httperr
  67. }
  68. defer result.Body.Close()
  69. body, err := ioutil.ReadAll(result.Body)
  70. if err != nil {
  71. return nil, err
  72. }
  73. return returnBody(body)
  74. }
  75. func PostRawBasicAuth(url string, data interface{}, user string, pwd string) (interface{}, error) {
  76. client := http.Client{
  77. Transport: &http.Transport{
  78. Dial: func(netw, addr string) (net.Conn, error) {
  79. c, err := net.DialTimeout(netw, addr, time.Second*5)
  80. if err != nil {
  81. fmt.Println("dail timeout", err)
  82. return nil, err
  83. }
  84. return c, nil
  85. },
  86. MaxIdleConnsPerHost: 10,
  87. ResponseHeaderTimeout: time.Second * 5,
  88. },
  89. }
  90. req, _ := http.NewRequest("POST", url, strings.NewReader(data.(string)))
  91. req.SetBasicAuth(user, pwd)
  92. req.Header.Set("Content-Type", "text/plain;charset=utf-8")
  93. result, httperr := client.Do(req)
  94. //result, httperr := http.Post(url, "text/plain;charset=utf-8", strings.NewReader(data.(string)))
  95. if httperr != nil {
  96. fmt.Println(url)
  97. fmt.Println(data)
  98. return nil, httperr
  99. }
  100. defer result.Body.Close()
  101. body, err := ioutil.ReadAll(result.Body)
  102. if err != nil {
  103. return nil, err
  104. }
  105. return returnBody(body)
  106. }
  107. func Put(url string, data interface{}) (interface{}, error) {
  108. req, httperr := http.NewRequest("PUT", url, strings.NewReader(data.(string)))
  109. if httperr != nil {
  110. return nil, httperr
  111. }
  112. res, httperr := http.DefaultClient.Do(req)
  113. if httperr != nil {
  114. return nil, httperr
  115. }
  116. defer res.Body.Close()
  117. body, err := ioutil.ReadAll(res.Body)
  118. if err != nil {
  119. return nil, err
  120. }
  121. return returnBody(body)
  122. }
  123. func Delete(url string) (interface{}, error) {
  124. req, httperr := http.NewRequest("DELETE", url, nil)
  125. //req.Header.Add("Authorization", "5F6MJ3N2Tk9ruL_6XQpx-uxkkg:o56-nIwtgTzUX80YCNpbcjUL8iM=:eyJyZXNvdXJjZSI6IF9mbG93RGF0YVNvdXJjZTEiLCJleHBpcmVzIjoxNTM2NzU4ODE2LCJjb250ZW50TUQ1IjoiIiwiY29udGVudFR5cGUiOiIiLCJoZWFkZXJzIjoiIiwibWV0aG9kIjoiREVMRVRFIn0=")
  126. if httperr != nil {
  127. return nil, httperr
  128. }
  129. res, httperr := http.DefaultClient.Do(req)
  130. if httperr != nil {
  131. return nil, httperr
  132. }
  133. defer res.Body.Close()
  134. body, httperr := ioutil.ReadAll(res.Body)
  135. if httperr != nil {
  136. return nil, httperr
  137. }
  138. return returnBody(body)
  139. }
  140. func PostFile(targetUrl string, filename string, filed string) (interface{}, error) {
  141. bodyBuf := &bytes.Buffer{}
  142. bodyWriter := multipart.NewWriter(bodyBuf)
  143. //关键的一步操作
  144. fileWriter, err := bodyWriter.CreateFormFile(filed, filename)
  145. if err != nil {
  146. fmt.Println("error writing to buffer")
  147. return nil, err
  148. }
  149. //打开文件句柄操作
  150. fh, err := os.Open(filename)
  151. if err != nil {
  152. fmt.Println("error opening file")
  153. return nil, err
  154. }
  155. defer fh.Close()
  156. //iocopy
  157. _, err = io.Copy(fileWriter, fh)
  158. if err != nil {
  159. return nil, err
  160. }
  161. contentType := bodyWriter.FormDataContentType()
  162. bodyWriter.Close()
  163. resp, err := http.Post(targetUrl, contentType, bodyBuf)
  164. if err != nil {
  165. return nil, err
  166. }
  167. defer resp.Body.Close()
  168. body, err := ioutil.ReadAll(resp.Body)
  169. if err != nil {
  170. return nil, err
  171. }
  172. return returnBody(body)
  173. }
  174. func Soap11(url string, databody string) (interface{}, error) {
  175. reqBody := `<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  176. xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  177. xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  178. <soap:Body>` + databody + `</soap:Body>
  179. </soap:Envelope>`
  180. res, err := http.Post(url, "text/xml; charset=UTF-8", strings.NewReader(reqBody))
  181. if nil != err {
  182. fmt.Println("http post err:", err)
  183. return nil, err
  184. }
  185. defer res.Body.Close()
  186. // return status
  187. if http.StatusOK != res.StatusCode {
  188. fmt.Println("WebService soap1.1 request fail, status: %s\n", res.StatusCode)
  189. return strconv.Itoa(http.StatusOK), nil
  190. }
  191. data, err := ioutil.ReadAll(res.Body)
  192. if nil != err {
  193. fmt.Println("ioutil ReadAll err:", err)
  194. return nil, err
  195. }
  196. return string(data), nil
  197. }
  198. func Soap12(url string, databody string) (interface{}, error) {
  199. reqBody := `<?xml version="1.0" encoding="utf-8"?><soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  200. xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  201. xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  202. <soap12:Body>` + databody + `</soap12:Body>
  203. </soap12:Envelope>`
  204. res, err := http.Post(url, "application/soap+xml; charset=UTF-8", strings.NewReader(reqBody))
  205. if nil != err {
  206. fmt.Println("http post err:", err)
  207. return nil, err
  208. }
  209. defer res.Body.Close()
  210. // return status
  211. if http.StatusOK != res.StatusCode {
  212. fmt.Println("WebService soap1.1 request fail, status: %s\n", res.StatusCode)
  213. return strconv.Itoa(http.StatusOK), nil
  214. }
  215. data, err := ioutil.ReadAll(res.Body)
  216. if nil != err {
  217. fmt.Println("ioutil ReadAll err:", err)
  218. return nil, err
  219. }
  220. return string(data), nil
  221. }
  222. func returnBody(body []byte) (interface{}, error) {
  223. if len(body) == 0 {
  224. return "", nil
  225. }
  226. jsonStr := string(body)
  227. firstChar := jsonStr[0:1]
  228. if firstChar == "[" || firstChar == "{" {
  229. jsondata := make(map[string]interface{})
  230. newStr := `{"a":` + jsonStr + `}`
  231. err := json.Unmarshal([]byte(newStr), &jsondata)
  232. if err != nil {
  233. return jsonStr, err
  234. }
  235. return jsondata["a"], nil
  236. } else {
  237. return jsonStr, nil
  238. }
  239. }