http.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package libs
  2. import (
  3. "github.com/astaxie/beego/httplib"
  4. "errors"
  5. "crypto/tls"
  6. )
  7. type Http struct {
  8. }
  9. func NewHttp()*Http {
  10. return new(Http)
  11. }
  12. func (http *Http)Post(url string,params map[string]interface{}) string {
  13. request := httplib.Post(url).Debug(true)
  14. setHeader(request)
  15. setParams(params,request)
  16. response,err := request.String()
  17. if err != nil{
  18. errors.New(err.Error())
  19. }
  20. return response
  21. }
  22. func (http *Http)Get(url string,params map[string]interface{}) string {
  23. request := httplib.Get(url).Debug(true)
  24. setParams(params,request)
  25. setHeader(request)
  26. response,err := request.String()
  27. if err != nil{
  28. errors.New(err.Error())
  29. }
  30. return response
  31. }
  32. func setParams(params map[string]interface{},request *httplib.BeegoHTTPRequest) {
  33. for k,v := range params {
  34. request.Param(k,ConvertString(v))
  35. }
  36. request.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
  37. }
  38. func setHeader(request *httplib.BeegoHTTPRequest) {
  39. request.Header("Accept-Encoding","gzip,deflate,sdch")
  40. request.Header("Content-Type","application/json;charset=utf-8")
  41. request.Header("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36")
  42. }