http.go 728 B

123456789101112131415161718192021222324
  1. package util
  2. import (
  3. "io"
  4. "net/http"
  5. )
  6. func GetHtml(url string) ([]byte, error) {
  7. request, err := http.NewRequest("GET", url, nil)
  8. if err != nil {
  9. return nil, err
  10. }
  11. // 设置请求投
  12. request.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
  13. request.Header.Add("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
  14. request.Header.Add("Connection", "keep-alive")
  15. request.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36")
  16. client := http.Client{}
  17. response, err := client.Do(request)
  18. if err != nil {
  19. return nil, err
  20. }
  21. return io.ReadAll(response.Body)
  22. }