go搭建server学习浏览器如何得到cookie的生成过程

大家好!cookie今天终于有了进一步的认识了,大家一起来学习

首先用go搭建server端:
代码如下:

package main

import (
	"encoding/base64"
	"fmt"
	"net/http"
	"net/url"
	"time"
)

// 设置cookie 示例:例如后端检查输入的用户密码 是否正确,正确的话,才下发cookie
func SetCookie(w http.ResponseWriter, r *http.Request) {
	c1 := &http.Cookie{
		Name: "name",
		//Value:    "lizhisheng",
		Value:    url.QueryEscape("学院君"), //包含了中文字符,需要通过 url.QueryEscape 方法进行 URL 编码,否则无法正常显示
		HttpOnly: true,
		Expires:  time.Now().AddDate(0, 0, 1), // Cookie 有效期设置为1天
	}

	c2 := &http.Cookie{
		Name:     "name1",
		Value:    "zhoujielun",
		HttpOnly: true,
		MaxAge:   1000, // Cookie 有效期设置为 1000s
		//MaxAge: -1,    // Cookie 有效期设置为 -1,就会在当前响应发送给客户端后销毁该 Cookie
	}

	w.Header().Set("Set-Cookie", c1.String())
	w.Header().Add("Set-Cookie", c2.String())
	//w.Header().Set("Set-Cookie", c2.String()) //用 Set 方法,此 Set-Cookie 头会覆盖前面的

	/*
		//便捷方法
		http.SetCookie(w, c1)
		http.SetCookie(w, c2)
	*/

	fmt.Fprint(w, "cookie给你了")
}

// 获取cookie 示例:有了cookie之后,才能进行某些操作,否则跳转到登录页面
func GetCookie(w http.ResponseWriter, r *http.Request) {
	// 指定获取哪一个cookie
	name, err := r.Cookie("name")
	if err != nil {
		fmt.Fprintln(w, "cannot get cookie of name")
	}
	username, _ := url.QueryUnescape(name.Value)

	// 指定获取哪一个cookie
	name1, err := r.Cookie("name1")
	if err != nil {
		fmt.Fprintln(w, "cannot get cookie of name")
	}

	// 获取全部cookie
	cookies := r.Cookies()
	fmt.Println(cookies)

	fmt.Println("------------------------------")
	fmt.Fprintln(w, username, name1)
	fmt.Fprintln(w, cookies[0])
	fmt.Fprintln(w, "取Cookie")

}

func SetWelcomeMessage(w http.ResponseWriter, r *http.Request) {
	msg := "欢迎访问学院君网站?"
	cookie := http.Cookie{
		Name:  "welcome_message",
		Value: base64.URLEncoding.EncodeToString([]byte(msg)),
	}
	http.SetCookie(w, &cookie)
	http.Redirect(w, r, "/get_welcome_message", 302)
}
func GetWelcomeMessage(w http.ResponseWriter, r *http.Request) {
	cookie, err := r.Cookie("welcome_message")
	if err != nil {
		fmt.Fprintln(w, "没有在 Cookie 中找到欢迎消息")
	} else {
		delCookie := http.Cookie{
			Name:   "welcome_message",
			MaxAge: -1,
		}
		http.SetCookie(w, &delCookie)
		msg, _ := base64.URLEncoding.DecodeString(cookie.Value)
		fmt.Fprintln(w, string(msg))
	}
}

func main() {
	http.HandleFunc("/setcookie", SetCookie)
	http.HandleFunc("/getcookie", GetCookie)
	http.HandleFunc("/set_welcome_message", SetWelcomeMessage)
	http.HandleFunc("/get_welcome_message", GetWelcomeMessage)
	http.ListenAndServe(":8080", nil)

}

2:浏览器端
在浏览器中访问 http://localhost:8080/setcookies,就可以在响应结果中看到对应的 Cookie 信息了:
通过如下方式观察现象。在这里插入图片描述

http://localhost:8080/setcookies
http://localhost:8080/getcookie
http://localhost:8080/set_welcome_message
http://localhost:8080/get_welcome_message

3参考文章
https://laravelacademy.org/post/21668

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值