Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "net/http"
- "net/url"
- "io/ioutil"
- "sync"
- )
- type Jar struct {
- lk sync.Mutex
- cookies map[string][]*http.Cookie
- }
- func NewJar() *Jar {
- jar := new(Jar)
- jar.cookies = make(map[string][]*http.Cookie)
- return jar
- }
- // SetCookies handles the receipt of the cookies in a reply for the
- // given URL. It may or may not choose to save the cookies, depending
- // on the jar's policy and implementation.
- func (jar *Jar) SetCookies(u *url.URL, cookies []*http.Cookie) {
- jar.lk.Lock()
- jar.cookies[u.Host] = cookies
- jar.lk.Unlock()
- }
- // Cookies returns the cookies to send in a request for the given URL.
- // It is up to the implementation to honor the standard cookie use
- // restrictions such as in RFC 6265.
- func (jar *Jar) Cookies(u *url.URL) []*http.Cookie {
- return jar.cookies[u.Host]
- }
- func main() {
- jar := NewJar()
- client := http.Client{nil, nil, jar}
- resp, _ := client.PostForm("http://www.somesite.com/login", url.Values{
- "email": {"myemail"},
- "password": {"mypass"},
- })
- resp.Body.Close()
- resp, _ = client.Get("http://www.somesite.com/protected")
- b, _ := ioutil.ReadAll(resp.Body)
- resp.Body.Close()
- fmt.Println(string(b))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement