You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

requests.go 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package main
  2. import (
  3. "io/ioutil"
  4. "log"
  5. "net/http"
  6. "net/url"
  7. "strings"
  8. )
  9. func getResultsPage(url string, cookie *http.Cookie) string {
  10. client := &http.Client{}
  11. request, err := http.NewRequest(http.MethodGet, url, nil)
  12. if err != nil {
  13. log.Fatalln(err)
  14. }
  15. request.Header.Add("User-Agent", userAgent)
  16. request.AddCookie(cookie)
  17. resp, err := client.Do(request)
  18. if err != nil {
  19. log.Fatalln(err)
  20. }
  21. if resp.StatusCode == http.StatusOK {
  22. bodyBytes, err := ioutil.ReadAll(resp.Body)
  23. if err != nil {
  24. log.Fatalln(err)
  25. }
  26. return string(bodyBytes)
  27. }
  28. return ""
  29. }
  30. func sendRequest(url string, cookie *http.Cookie, postData *url.Values) string {
  31. if postData == nil {
  32. log.Fatalln("Empty form")
  33. }
  34. client := &http.Client{}
  35. request, err := http.NewRequest(http.MethodPost, url, strings.NewReader(postData.Encode()))
  36. if err != nil {
  37. log.Fatalln(err)
  38. }
  39. request.Header.Add("User-Agent", userAgent)
  40. request.AddCookie(cookie)
  41. request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  42. resp, err := client.Do(request)
  43. if err != nil {
  44. log.Fatalln(err)
  45. }
  46. if resp.StatusCode == http.StatusOK {
  47. bodyBytes, err := ioutil.ReadAll(resp.Body)
  48. if err != nil {
  49. log.Fatalln(err)
  50. }
  51. return string(bodyBytes)
  52. }
  53. return ""
  54. }