三种添加Cookie的方式(有其他方式请留言告知):
服务端代码:
服务端很简单,请求时有Cookie则返回cookie的结果,withcookie为ture,没有则设置cookie,withcookie为falsepackage main
import (
"fmt"
"github.com/gin-gonic/gin"
)
func main(){
engine := gin.Default()
engine.GET("/cookie_test", func(context *gin.Context) {
data:=map[string]string{}
value,err:= context.Cookie("ds")
if err!=nil {
context.SetCookie("ds","xxxxxxxx",10,"/","localhost",false,true)
data["withcookie"] = "false"
data["value"] = "xxxxxxxxxx"
context.JSON(200,data)
} else {
fmt.Println("Request with cookie,value is:",value)
data["withcookie"] = "true"
data["value"] = value
context.JSON(200,data)
}
})
engine.Run(":8082")
}
一、Header中添加Cookie:
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
client := &http.Client{}
req, _ := http.NewRequest("GET", "http://localhost:8082/cookie_test",nil)
req.Header.Add("Cookie","ds=req.Header.Add")
resp, err := client.Do(req)
if err != nil {
panic(nil)
}
body, _ := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
fmt.Println(string(body))
}
二、通过req添加Cookie:
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
client := &http.Client{}
req, _ := http.NewRequest("GET", "http://localhost:8082/cookie_test",nil)
req.AddCookie(&http.Cookie{
Name: "ds",
Value: "req.AddCookie",
Path: "/",
Domain: "localhost",
})
resp, err := client.Do(req)
if err != nil {
panic(nil)
}
body, _ := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
fmt.Println(string(body))
}
三、通过cookiejar来管理Cookie:
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
)
func main() {
jar, _ := cookiejar.New(nil)
var cookies []*http.Cookie
firstCookie := &http.Cookie{
Name: "ds",
Value: "CookieJar",
Path: "/",
Domain: "localhost",
}
cookies = append(cookies, firstCookie)
cookieURL, _ := url.Parse("https://localhost:8082/cookie_test")
//fmt.Println("cookie url is:",cookieURL)
jar.SetCookies(cookieURL, cookies)
// 查看添加结果
fmt.Println(jar.Cookies(cookieURL))
client := &http.Client{
Jar: jar,
}
req, _ := http.NewRequest("GET", "http://localhost:8082/cookie_test",nil)
resp, err := client.Do(req)
if err != nil {
panic(nil)
}
body, _ := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
fmt.Println(string(body))
}
Cookie自动管理Cookie:
将代码稍做改动,去掉预先设置的Cookie,发送两次请求package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/cookiejar"
)
func main() {
jar, _ := cookiejar.New(nil)
client := &http.Client{
Jar: jar,
}
req, _ := http.NewRequest("GET", "http://localhost:8082/cookie_test",nil)
resp, err := client.Do(req)
if err != nil {
panic(nil)
}
body, _ := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
fmt.Println(string(body))
fmt.Println("-------------------------------------------------------------------------------------------------")
req, _ = http.NewRequest("GET", "http://localhost:8082/cookie_test",nil)
resp, err = client.Do(req)
if err != nil {
panic(nil)
}
body, _ = ioutil.ReadAll(resp.Body)
resp.Body.Close()
fmt.Println(string(body))
}
CookieJar的坑:
package main
import (
"fmt"
"net/http"
"net/http/cookiejar"
"net/url"
)
func main() {
jar, _ := cookiejar.New(nil)
var cookies []*http.Cookie
firstCookie := &http.Cookie{
Name: "ds",
Value: "CookieJar",
Path: "/",
Domain: "localhost",
}
cookies = append(cookies, firstCookie)
cookieURL, _ := url.Parse("https://localhost:8082/cookie_test")
fmt.Println("cookie url is:",cookieURL)
jar.SetCookies(cookieURL, cookies)
fmt.Println("Cookie is:", jar.Cookies(cookieURL))
}
上面的结果是没有问题的,但是我们把localhost改成127.0.0.1试试:
package main
import (
"fmt"
"net/http"
"net/http/cookiejar"
"net/url"
)
func main() {
jar, _ := cookiejar.New(nil)
var cookies []*http.Cookie
firstCookie := &http.Cookie{
Name: "ds",
Value: "CookieJar",
Path: "/",
Domain: "127.0.0.1",
}
cookies = append(cookies, firstCookie)
cookieURL, _ := url.Parse("https://127.0.0.1:8082/cookie_test")
fmt.Println("cookie url is:",cookieURL)
jar.SetCookies(cookieURL, cookies)
fmt.Println("Cookie is:", jar.Cookies(cookieURL))
}
结果为空,发请求的时候也不会带上Cookie,所以大家测试的时候不要填IP