-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCookieHelper.cs
134 lines (111 loc) · 4.1 KB
/
CookieHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
namespace MusicBeePlugin
{
class CookieHelper
{
private static CookieContainer myCookieContainer;
private static CookieHelper self;
public CookieHelper()
{
myCookieContainer = new CookieContainer();
}
public static CookieHelper get()
{
if (self == null) self = new CookieHelper();
return self;
}
public void addCookie(Cookie cookie)
{
myCookieContainer.Add(cookie);
}
public void addCookie(CookieCollection cookies)
{
myCookieContainer.Add(cookies);
}
public void addCookie(Uri domain_uri, Cookie cookie)
{
myCookieContainer.Add(cookie);
}
public CookieContainer GetCookieCollection()
{
return myCookieContainer;
}
public void loadCookie(string default_cookie, string domain, string configPath)
{
Uri domain_uri = new Uri(domain);
// string configPath = Path.Combine(mbApiInterface.Setting_GetPersistentStoragePath(), about.Name);
string cookie = default_cookie;
if (File.Exists(configPath))
{
try
{
cookie = cookie + (File.ReadAllText(configPath, System.Text.Encoding.UTF8));
}
catch (Exception ex)
{
// mbApiInterface.MB_Trace(about.Name + " Failed to load config" + ex);
}
string[] tempCookies = cookie.Split(';');
string tempCookie = null;
int Equallength = 0;// =的位置
string cookieKey = null;
string cookieValue = null;
for (int i = 0; i < tempCookies.Length; i++)
{
if (!string.IsNullOrEmpty(tempCookies[i]))
{
tempCookie = tempCookies[i];
Equallength = tempCookie.IndexOf("=");
if (Equallength != -1) //有可能cookie 无=,就直接一个cookiename;比如:a=3;ck;abc=;
{
cookieKey = tempCookie.Substring(0, Equallength).Trim();
//cookie=
if (Equallength == tempCookie.Length - 1) //这种是等号后面无值,如:abc=;
{
cookieValue = "";
}
else
{
cookieValue = tempCookie.Substring(Equallength + 1, tempCookie.Length - Equallength - 1).Trim();
}
}
else
{
cookieKey = tempCookie.Trim();
cookieValue = "";
}
CookieHelper.get().addCookie(domain_uri, new Cookie(cookieKey, cookieValue));
}
}
}
}
public void saveCookie(string site, string configPath)
{
// string configPath = Path.Combine(mbApiInterface.Setting_GetPersistentStoragePath(), about.Name);
// if (File.Exists(configPath))
{
try
{
var cookies = myCookieContainer.GetCookies(new Uri(site));
string tmp = "";
foreach (Cookie cookie in cookies)
{
tmp = tmp + cookie.ToString() + ";";
}
File.WriteAllText(configPath, tmp);
Console.WriteLine("saveCookie path=" + configPath + "; site=" + site);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
}
}