Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "io/ioutil"
  8. "sync"
  9. )
  10.  
  11. type Jar struct {
  12. lk sync.Mutex
  13. cookies map[string][]*http.Cookie
  14. }
  15.  
  16. func NewJar() *Jar {
  17. jar := new(Jar)
  18. jar.cookies = make(map[string][]*http.Cookie)
  19. return jar
  20. }
  21.  
  22. // SetCookies handles the receipt of the cookies in a reply for the
  23. // given URL. It may or may not choose to save the cookies, depending
  24. // on the jar's policy and implementation.
  25. func (jar *Jar) SetCookies(u *url.URL, cookies []*http.Cookie) {
  26. jar.lk.Lock()
  27. jar.cookies[u.Host] = cookies
  28. jar.lk.Unlock()
  29. }
  30.  
  31. // Cookies returns the cookies to send in a request for the given URL.
  32. // It is up to the implementation to honor the standard cookie use
  33. // restrictions such as in RFC 6265.
  34. func (jar *Jar) Cookies(u *url.URL) []*http.Cookie {
  35. return jar.cookies[u.Host]
  36. }
  37.  
  38. func main() {
  39. jar := NewJar()
  40. client := http.Client{nil, nil, jar}
  41.  
  42. resp, _ := client.PostForm("http://www.somesite.com/login", url.Values{
  43. "email": {"myemail"},
  44. "password": {"mypass"},
  45. })
  46. resp.Body.Close()
  47.  
  48. resp, _ = client.Get("http://www.somesite.com/protected")
  49.  
  50. b, _ := ioutil.ReadAll(resp.Body)
  51. resp.Body.Close()
  52.  
  53. fmt.Println(string(b))
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement