Skip to content

Commit 38ba0f6

Browse files
authored
浏览器复制的cookies和GetCookiesString获取的有所不同 (#59)
1 parent 993b25d commit 38ba0f6

File tree

3 files changed

+57
-19
lines changed

3 files changed

+57
-19
lines changed

README.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,19 @@ if err == nil && result.Status == 0 {
145145
// 获取cookiesString,自行存储,方便下次启动程序时不需要重新登录
146146
cookiesString := client.GetCookiesString()
147147

148-
// 设置cookiesString,就不需要登录操作了
148+
// 下次启动时,把存储的cookiesString设置进来,就不需要登录操作了
149149
client.SetCookiesString(cookiesString)
150-
// 你也可以直接把浏览器的Cookie复制过来调用SetCookiesString,这样也可以不需要登录操作了
150+
151+
// 如果你是从浏览器request的header中直接复制出来的cookies,则改为调用SetRawCookies
152+
client.SetRawCookies("cookie1=xxx; cookie2=xxx")
151153
```
152154

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

155163
你可以很方便的调用其它接口,以下举个例子:

client.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,18 @@ func (c *Client) GetCookiesString() string {
4545
for _, cookie := range c.resty.Cookies {
4646
cookieStrings = append(cookieStrings, cookie.String())
4747
}
48-
return strings.Join(cookieStrings, "; ")
48+
return strings.Join(cookieStrings, "\n")
4949
}
5050

5151
// SetCookiesString 设置Cookies,但是是字符串格式,配合 GetCookiesString 使用。有些功能必须登录或设置Cookies后才能使用。
52-
//
53-
// 你也可以将浏览器中的Cookie传入这个函数使用。
5452
func (c *Client) SetCookiesString(cookiesString string) {
55-
rawCookies := strings.ReplaceAll(cookiesString, "\n", "; ") // 兼容之前的换行符
53+
c.resty.SetCookies((&resty.Response{RawResponse: &http.Response{Header: http.Header{
54+
"Set-Cookie": strings.Split(cookiesString, "\n"),
55+
}}}).Cookies())
56+
}
57+
58+
// SetRawCookies 如果你是从浏览器request的header中直接复制出来的cookies,调用这个函数。
59+
func (c *Client) SetRawCookies(rawCookies string) {
5660
header := http.Header{}
5761
header.Add("Cookie", rawCookies)
5862
req := http.Request{Header: header}

client_test.go

+39-13
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,58 @@ package bilibili
22

33
import (
44
"net/http"
5-
"reflect"
5+
"strings"
66
"testing"
77
)
88

9-
func TestCookie(t *testing.T) {
10-
result := []*http.Cookie{
11-
{Name: "a", Value: "1"},
12-
{Name: "b", Value: "2"},
9+
func deepEquals(a, b []*http.Cookie) bool {
10+
if len(a) != len(b) {
11+
return false
1312
}
13+
for i := range a {
14+
if a[i].Name != b[i].Name {
15+
return false
16+
}
17+
if a[i].Value != b[i].Value {
18+
return false
19+
}
20+
if a[i].Domain != b[i].Domain {
21+
return false
22+
}
23+
if a[i].Path != b[i].Path {
24+
return false
25+
}
26+
}
27+
return true
28+
}
29+
30+
func TestCookie(t *testing.T) {
1431
{
15-
c := New()
16-
c.SetCookiesString("a=1; b=2")
17-
if c.GetCookiesString() != "a=1; b=2" {
18-
t.Fail()
32+
result := []*http.Cookie{ // 不测试Expires,因为time.Time转化成string再转化回来会丢失单调时钟
33+
{Name: "a", Value: "1", Domain: "bilibili.com", Path: "/"},
34+
{Name: "b", Value: "2", Domain: "bilibili.com", Path: "/"},
1935
}
20-
if !reflect.DeepEqual(c.GetCookies(), result) {
36+
s := make([]string, 0, len(result))
37+
for _, cookie := range result {
38+
s = append(s, cookie.String())
39+
}
40+
c := New()
41+
c.SetCookiesString(strings.Join(s, "\n"))
42+
if !deepEquals(result, c.GetCookies()) {
2143
t.Fail()
2244
}
2345
}
2446
{
47+
result := []*http.Cookie{
48+
{Name: "a", Value: "1"},
49+
{Name: "b", Value: "2"},
50+
}
2551
c := New()
26-
c.SetCookiesString("a=1\nb=2")
27-
if c.GetCookiesString() != "a=1; b=2" {
52+
c.SetRawCookies("a=1; b=2")
53+
if c.GetCookiesString() != "a=1\nb=2" {
2854
t.Fail()
2955
}
30-
if !reflect.DeepEqual(c.GetCookies(), result) {
56+
if !deepEquals(result, c.GetCookies()) {
3157
t.Fail()
3258
}
3359
}

0 commit comments

Comments
 (0)