C#能否使用读取硬盘上IE浏览器的Cookie然后利用这个进行登录
比如我使用IE登录了人人网,然后使用了下次自动登录,所以相关Cookie存储了相应的信息。然后,我自己写了一个WinForm程序,此时,我能否通过C#去读取IE下的Cookie进行分析,然后使用WebClint或者HttpWebRequest进行登录呢?
[解决办法]
不记得什么时候写的了,仅供参考,楼主可以搜索一下“IE cookie 格式”
public static list<Cookie> get(string url, bool isRemove)
{
list<Cookie> cookies = new list<Cookie>();
if (url != null && url.Length != 0)
{
int index = url.IndexOf("://", StringComparison.Ordinal);
if (index != -1)
{
int pathIndex = url.IndexOf('/', index += 3);
string urlDomain = pathIndex == -1 ? url.Substring(index) : url.Substring(index, pathIndex - index);
if (urlDomain.Length != 0)
{
bool isCookie;
Cookie newCookie;
int lines, startIndex, removeCount;
long expiresLow, expiresHigh;
DateTime expires;
string[] cookieInfos;
bool[] isRemoves;
string domain, path, urlPath = pathIndex == -1 ? "/" : url.Substring(pathIndex);
urlDomain = "." + urlDomain;
foreach (string filename in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Cookies), "*.txt"))
{
try
{
lines = (cookieInfos = File.ReadAllLines(filename)).Length;
isRemoves = isRemove ? new bool[lines] : null;
for (startIndex = lines - 8, removeCount = index = 0; index <= startIndex; index++)
{
isCookie = false;
if (long.TryParse(cookieInfos[index + 4], out expiresLow)
&& long.TryParse(cookieInfos[index + 5], out expiresHigh))
{
expires = DateTime.FromFileTimeUtc((expiresHigh << 32) + expiresLow);
if (expires > date.NowTime)
{
if ((pathIndex = (domain = cookieInfos[index + 2]).IndexOf('/')) == -1)
{
path = "/";
}
else
{
path = domain.Substring(pathIndex);
domain = domain.Substring(0, pathIndex);
}
if (domain[0] != '.') domain = "." + domain;
//if ((domain[0] == '.' ? urlDomain.EndsWith(domain, StringComparison.OrdinalIgnoreCase)
[解决办法]
String.Compare(urlDomain, 0, domain, 1, urlDomain.Length, true) == 0 : String.Compare(urlDomain, domain, true) == 0) && urlPath.StartsWith(path, StringComparison.OrdinalIgnoreCase))
isCookie = urlDomain.EndsWith(domain, StringComparison.OrdinalIgnoreCase) && urlPath.StartsWith(path, StringComparison.OrdinalIgnoreCase);
if (isCookie && cookieInfos[index].Length != 0)
{
newCookie = new Cookie(fastCSharp.web.cookie.FormatCookieName(cookieInfos[index]), fastCSharp.web.cookie.FormatCookieValue(cookieInfos[index + 1]), path, domain);
cookies.Add(newCookie);
newCookie.Expires = expires;
}
}
}
if (isRemove && isCookie)
{
removeCount++;
isRemoves[index] = isRemoves[index + 1] = isRemoves[index + 2]
= isRemoves[index + 3] = isRemoves[index + 4] = isRemoves[index + 5]
= isRemoves[index + 6] = isRemoves[index + 7] = true;
index += 8;
while (index < lines
&& (cookieInfos[index].Length != 1
[解决办法]
cookieInfos[index][0] != '*'))
{
isRemoves[index++] = true;
}
if (index < lines) isRemoves[index] = true;
}
else
{
index += 8;
while (index < lines
&& (cookieInfos[index].Length != 1
[解决办法]
cookieInfos[index][0] != '*'))
{
index++;
}
}
}
if (removeCount != 0)
{
stringBuilder cookieText = new stringBuilder();
for (index = removeCount = 0; index < lines; ++index)
{
if (!isRemoves[index])
{
removeCount++;
cookieText.Add(cookieInfos[index]);
}
}
File.Delete(filename);
File.WriteAllText(filename, cookieText.ToString());
}
}
catch (Exception error)
{
log.Default.Add(error, "cookie文件 " + filename + " 相关操作失败", false);
}
}
}
}
}
return cookies;
}