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.

62 lines
1.3 KiB

  1. # httplib
  2. httplib is an libs help you to curl remote url.
  3. # How to use?
  4. ## GET
  5. you can use Get to crawl data.
  6. import "httplib"
  7. str, err := httplib.Get("http://beego.me/").String()
  8. if err != nil {
  9. t.Fatal(err)
  10. }
  11. fmt.Println(str)
  12. ## POST
  13. POST data to remote url
  14. b:=httplib.Post("http://beego.me/")
  15. b.Param("username","astaxie")
  16. b.Param("password","123456")
  17. str, err := b.String()
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. fmt.Println(str)
  22. ## set timeout
  23. you can set timeout in request.default is 60 seconds.
  24. set Get timeout:
  25. httplib.Get("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second)
  26. set post timeout:
  27. httplib.Post("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second)
  28. - first param is connectTimeout.
  29. - second param is readWriteTimeout
  30. ## debug
  31. if you want to debug the request info, set the debug on
  32. httplib.Get("http://beego.me/").Debug(true)
  33. ## support HTTPS client
  34. if request url is https. You can set the client support TSL:
  35. httplib.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
  36. more info about the tls.Config please visit http://golang.org/pkg/crypto/tls/#Config
  37. ## set cookie
  38. some http request need setcookie. So set it like this:
  39. cookie := &http.Cookie{}
  40. cookie.Name = "username"
  41. cookie.Value = "astaxie"
  42. httplib.Get("http://beego.me/").SetCookie(cookie)