github.com/qjfoidnh/BaiduPCS-Go@v0.0.0-20231011165705-caa18a3765f3/requester/util.go (about)

     1  package requester
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"strings"
     7  )
     8  
     9  // ParseCookieStr 解析 Cookie 字符串
    10  func ParseCookieStr(cookieStr string) []*http.Cookie {
    11  	rawCookies := strings.SplitN(cookieStr, ";", -1)
    12  	cookies := make([]*http.Cookie, 0, len(rawCookies))
    13  
    14  	for _, rawCookie := range rawCookies {
    15  		s2 := strings.SplitN(rawCookie, "=", 2)
    16  		if len(s2) < 2 {
    17  			fmt.Println(s2)
    18  			continue
    19  		}
    20  
    21  		s2[0] = strings.TrimSpace(s2[0])
    22  		s2[1] = strings.TrimSpace(s2[1])
    23  
    24  		cookies = append(cookies, &http.Cookie{
    25  			Name:  s2[0],
    26  			Value: s2[1],
    27  		})
    28  	}
    29  	return cookies
    30  }