-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cookiemanager.cs
138 lines (118 loc) · 2.97 KB
/
Cookiemanager.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
135
136
137
138
using System.Collections.Generic;
using System.Text;
using System.Net;
namespace WinCachebox
{
public class CookieManager
{
private Dictionary<string, string> cookieValues;
private bool XWapProxySetCookie = false;
public Dictionary<string, string> CookieValues
{
get
{
if (this.cookieValues == null)
{
this.cookieValues = new Dictionary<string, string>();
}
return this.cookieValues;
}
}
public void PublishCookies(HttpWebRequest webRequest)
{
StringBuilder sb = new StringBuilder();
sb.Append("Cookie: ");
foreach (string key in this.CookieValues.Keys)
{
sb.Append(key);
sb.Append("=");
sb.Append(this.CookieValues[key]);
sb.Append("; ");
//sb.Append("$Path=\"/\"; ");
}
webRequest.Headers.Add(sb.ToString());
sb = null;
webRequest = null;
}
public void StoreCookies(HttpWebResponse webResponse)
{
for (int x = 0; x < webResponse.Headers.Count; x++)
{
if (webResponse.Headers.Keys[x].ToLower().Equals("set-cookie"))
{
this.AddRawCookie(webResponse.Headers[x]);
}
else if (webResponse.Headers.Keys[x].ToLower().Equals("x-wap-proxy-set-cookie"))
{
if (webResponse.Headers[x].ToLower().Equals("state"))
{
XWapProxySetCookie = true;
}
}
}
webResponse = null;
}
public bool CheckXWapProxySetCookie()
{
return XWapProxySetCookie;
}
private void AddRawCookie(string rawCookieData)
{
string key = null;
string value = null;
string[] entries = null;
if (rawCookieData.IndexOf(",") > 0)
{
entries = rawCookieData.Split(',');
}
else
{
entries = new string[] { rawCookieData };
}
foreach (string entry in entries)
{
string cookieData = entry.Trim();
if (cookieData.IndexOf(';') > 0)
{
string[] temp = cookieData.Split(';');
cookieData = temp[0];
}
int index = cookieData.IndexOf('=');
if (index > 0)
{
key = cookieData.Substring(0, index);
value = cookieData.Substring(index + 1);
}
if (key != null && value != null)
{
this.CookieValues[key] = value;
}
cookieData = null;
}
rawCookieData = null;
entries = null;
key = null;
value = null;
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append("[");
foreach (string key in this.CookieValues.Keys)
{
sb.Append("{");
sb.Append(key);
sb.Append(",");
sb.Append(this.CookieValues[key]);
sb.Append("}, ");
}
if (this.CookieValues.Keys.Count > 0)
{
sb.Remove(sb.Length - 2, 2);
}
sb.Append("]");
return sb.ToString();
}
}
}