http_request.go 659 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package xnet
  2. import (
  3. "fmt"
  4. "github.com/parnurzeal/gorequest"
  5. "net/http"
  6. "time"
  7. )
  8. func HTTPGet(url, proxy, param string, headers map[string]string, cookies []*http.Cookie) ([]byte, error) {
  9. s := gorequest.New().Get(url)
  10. //设置timeout
  11. s = s.Timeout(15 * time.Second)
  12. //添加proxy
  13. if len(proxy) > 3 {
  14. s = s.Proxy(proxy)
  15. }
  16. //添加headers
  17. for k, v := range headers {
  18. s = s.Set(k, v)
  19. }
  20. //传递query参数
  21. s = s.Send(param)
  22. //添加cookies
  23. s = s.AddCookies(cookies)
  24. resp, body, errs := s.EndBytes()
  25. if errs != nil || resp.StatusCode >= 300 {
  26. return body, fmt.Errorf("fail to request,error : %v\n", errs)
  27. }
  28. return body, nil
  29. }