Golang net/http.Response.Cookies() and Location() functions example

package net/http

Golang net/http.Response.Cookies() and Location() functions usage example

 ...
 var network string // private
 var address string // private
 var Jar CookieJar // public

 conn, err := net.Dial(network, address)
 if err != nil {
  panic(err)
 }
 resp, err := http.ReadResponse(bufio.NewReader(conn), &http.Request{Method: "CONNECT"})

 if err == nil && resp.Status == connected {
  
  if rc := resp.Cookies(); len(rc) > 0 { // <----------- here!
 Jar.SetCookies(targetURL, rc)

 locationURL, err := resp.Location() //<----- here !

 if err != nil {
 fmt.Println("Location URL error ", err)
 return
 } 
 }
  
 }
 if err == nil {
  err = errors.New("unexpected HTTP response: " + resp.Status)
 }
 conn.Close()
 ...

References :

http://golang.org/pkg/net/http/#Response.Location

http://golang.org/pkg/net/http/#Response.Cookies

Advertisement