我试图用Golang的net/http设置cookie。我有在Golang中设置Cookie(net/http)
package main
import "io"
import "net/http"
import "time"
func indexHandler(w http.ResponseWriter, req *http.Request) {
expire := time.Now().AddDate(0, 0, 1)
cookie := http.Cookie{"test", "tcookie", "/", "www.domain.com", expire, expire.Format(time.UnixDate), 86400, true, true, "test=tcookie", []string{"test=tcookie"}}
req.AddCookie(&cookie)
io.WriteString(w, "Hello world!")
}
func main() {
http.HandleFunc("/", indexHandler)
http.ListenAndServe(":80", nil)
}
我试着用'cookies'搜索googling'Golang',但没有得到任何好结果。如果任何人都可以指出我正确的方向,将不胜感激。
谢谢。
谢谢。这似乎工作。我错误地看着http://golang.org/pkg/net/http/#Request.AddCookie早些时候 – Tech163
是的,它很混乱。如果您的go程序充当HTTP客户端并且您想要将cookie值发送到HTTP服务器,那么您需要Request.AddCookie ... –