Skip to content

浏览器复制的cookies和GetCookiesString获取的有所不同 #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,19 @@ if err == nil && result.Status == 0 {
// 获取cookiesString,自行存储,方便下次启动程序时不需要重新登录
cookiesString := client.GetCookiesString()

// 设置cookiesString,就不需要登录操作了
// 下次启动时,把存储的cookiesString设置进来,就不需要登录操作了
client.SetCookiesString(cookiesString)
// 你也可以直接把浏览器的Cookie复制过来调用SetCookiesString,这样也可以不需要登录操作了

// 如果你是从浏览器request的header中直接复制出来的cookies,则改为调用SetRawCookies
client.SetRawCookies("cookie1=xxx; cookie2=xxx")
```

> [!NOTE]
> - `GetCookiesString`和`SetCookiesString`使用的字符串是`"cookie1=xxx; expires=xxx; domain=xxx.com; path=/\ncookie2=xxx; expires=xxx; domain=xxx.com; path=/"`,包含过期时间、domain等一些其它信息,以`"\n"`分隔多个cookie
> - `SetRawCookies`使用的字符串是`"cookie1=xxx; cookie2=xxx"`,只包含key=value,以`"; "`分隔多个cookie,这和在浏览器F12里复制的一样
>
> 请注意不要混用。

### 其它接口

你可以很方便的调用其它接口,以下举个例子:
Expand Down
12 changes: 8 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,18 @@ func (c *Client) GetCookiesString() string {
for _, cookie := range c.resty.Cookies {
cookieStrings = append(cookieStrings, cookie.String())
}
return strings.Join(cookieStrings, "; ")
return strings.Join(cookieStrings, "\n")
}

// SetCookiesString 设置Cookies,但是是字符串格式,配合 GetCookiesString 使用。有些功能必须登录或设置Cookies后才能使用。
//
// 你也可以将浏览器中的Cookie传入这个函数使用。
func (c *Client) SetCookiesString(cookiesString string) {
rawCookies := strings.ReplaceAll(cookiesString, "\n", "; ") // 兼容之前的换行符
c.resty.SetCookies((&resty.Response{RawResponse: &http.Response{Header: http.Header{
"Set-Cookie": strings.Split(cookiesString, "\n"),
}}}).Cookies())
}

// SetRawCookies 如果你是从浏览器request的header中直接复制出来的cookies,调用这个函数。
func (c *Client) SetRawCookies(rawCookies string) {
header := http.Header{}
header.Add("Cookie", rawCookies)
req := http.Request{Header: header}
Expand Down
52 changes: 39 additions & 13 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,58 @@ package bilibili

import (
"net/http"
"reflect"
"strings"
"testing"
)

func TestCookie(t *testing.T) {
result := []*http.Cookie{
{Name: "a", Value: "1"},
{Name: "b", Value: "2"},
func deepEquals(a, b []*http.Cookie) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i].Name != b[i].Name {
return false
}
if a[i].Value != b[i].Value {
return false
}
if a[i].Domain != b[i].Domain {
return false
}
if a[i].Path != b[i].Path {
return false
}
}
return true
}

func TestCookie(t *testing.T) {
{
c := New()
c.SetCookiesString("a=1; b=2")
if c.GetCookiesString() != "a=1; b=2" {
t.Fail()
result := []*http.Cookie{ // 不测试Expires,因为time.Time转化成string再转化回来会丢失单调时钟
{Name: "a", Value: "1", Domain: "bilibili.com", Path: "/"},
{Name: "b", Value: "2", Domain: "bilibili.com", Path: "/"},
}
if !reflect.DeepEqual(c.GetCookies(), result) {
s := make([]string, 0, len(result))
for _, cookie := range result {
s = append(s, cookie.String())
}
c := New()
c.SetCookiesString(strings.Join(s, "\n"))
if !deepEquals(result, c.GetCookies()) {
t.Fail()
}
}
{
result := []*http.Cookie{
{Name: "a", Value: "1"},
{Name: "b", Value: "2"},
}
c := New()
c.SetCookiesString("a=1\nb=2")
if c.GetCookiesString() != "a=1; b=2" {
c.SetRawCookies("a=1; b=2")
if c.GetCookiesString() != "a=1\nb=2" {
t.Fail()
}
if !reflect.DeepEqual(c.GetCookies(), result) {
if !deepEquals(result, c.GetCookies()) {
t.Fail()
}
}
Expand Down
Loading