紫色飞猪的研发之旅--03golang:获取cookie

在调用第三方接口时,需要用到凭证,大多数接口使用token即可,那自然有小多数接口使用cookie等认证。此系列将围绕我的研发之旅进行。

获取cookie相对简单独立,直接上代码

package main

import (
	"bytes"
	"fmt"
	"mime/multipart"
	"net/http"
	"time"
)

var (
	CookieName  string // cookie k
	CookieValue string // cookie v
	Time        int    // cookie 限期
)

// 初始化cookie所需信息
func cookieInit() {
	postData := make(map[string]string)
	postData["username"] = "axxxn"
	postData["password"] = "axxxxxt"
	url := "http://txxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxkens"
	postWithFormData("POST", url, &postData)

}

// PostWithFormData 获取cookie和过期时间
func postWithFormData(method, url string, postData *map[string]string) {
	body := new(bytes.Buffer)
	w := multipart.NewWriter(body)
	for k, v := range *postData {
		w.WriteField(k, v)
	}
	w.Close()
	req, _ := http.NewRequest(method, url, body)
	req.Header.Set("Content-Type", w.FormDataContentType())
	resp, err := http.DefaultClient.Do(req)
	if err != nil || resp.StatusCode > 300 {
		fmt.Println("err = ", err)
		fmt.Printf("%+v\n", resp.Cookies())
		return
	}
	defer resp.Body.Close()
	for _, cookie := range resp.Cookies() {
		if cookie.Name == "tke" {
			CookieName = cookie.Name
			CookieValue = cookie.Value
			nowtime := time.Now().Unix()
			Time = cookie.MaxAge + int(nowtime)
		}
	}
}

// GetCookie 判断cookie过期时间范围cookie
func GetCookie() (string, string) {
	nowtime := time.Now().Unix()
	if len(CookieValue) == 0 {
		cookieInit()
	} else {
		if int(nowtime) >= Time {
			cookieInit()
		}
	}
	return CookieName, CookieValue
}

func main()  {
	n,v := GetCookie()
	fmt.Println("n = ", n)
	fmt.Println("v = ", v)
}
// n = hubxxxxxxxen
// v = eyJhbGxxxxx9pZCI6MCwib3Blbl91c2VyX2lkIjowLCJxxxxiLCJhdmF0YXJfdXJsIjoiIiwic3BhY2VfaWQiOiIiLCJhcHBfa2V5xxxfaWQiOiIyZWNiMDkyNi03ZWYzLxxxxN1ZWRfYXQiOjE2MzA0ODxxxxiOIOfC0TJxxAcc=
posted @   紫色飞猪  阅读(318)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
点击右上角即可分享
微信分享提示