http.go 781 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package util
  2. import (
  3. "io/ioutil"
  4. "net/http"
  5. "strings"
  6. )
  7. func HttpGet(url string) ([]byte, error) {
  8. client := &http.Client{}
  9. //提交请求
  10. reqest, err := http.NewRequest("GET", url, nil)
  11. if err != nil {
  12. return []byte{}, err
  13. }
  14. //处理返回结果
  15. response, _ := client.Do(reqest)
  16. bodyBytes, _ := ioutil.ReadAll(response.Body)
  17. return bodyBytes, nil
  18. }
  19. func PostHeader(url string, msg string, headers map[string]string) ([]byte, error) {
  20. client := &http.Client{}
  21. req, err := http.NewRequest("POST", url, strings.NewReader(msg))
  22. if err != nil {
  23. return nil, err
  24. }
  25. for key, header := range headers {
  26. req.Header.Set(key, header)
  27. }
  28. resp, err := client.Do(req)
  29. if err != nil {
  30. return nil, err
  31. }
  32. defer resp.Body.Close()
  33. return ioutil.ReadAll(resp.Body)
  34. }