C#能否使用读取硬盘上IE浏览器的Cookie然后利用这个进行登录 比如我使用IE登录了人人网,然后使用了下次自动登录,所以相关Cookie存储了相应的信息。然后,我自己写了一个WinForm程序,此时,我能否通过C#去读取IE下的Cookie进行分析,然后使用WebClint或者HttpWebRequest进行登录呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以的,读取文件后,自行设置cookies但现在的浏览器Cookie不好提取的,IE6是可以的 网上找下cookies的内容,可以读取 Cookie一般是以txt文本形式存在的,其内部内容没有加密么? 不记得什么时候写的了,仅供参考,楼主可以搜索一下“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; } 像搜狗啊,360啊,这些浏览器用的都是IE的内核,那Cookie啥的,跟IE一样么,求教 如果你没有选择“记住密码”一类的选项,是没法登录的,CCTV有些骇人听闻了。 怎样能执行"GetA()+GetB()",并返回数据? 这个界面如何实现 不能序列化,随机密钥如何保存?急呀........~~ 关于WinForm中ListBox的问题,很急,请各位帮忙!! javascript 高手請進,問時間比較問題???? 如何对内存中的数据进行写操作 請教高手!C#中一個文件到了文件尾怎麼返回到文件頭? 串口读取硬件数据 能提升程序中某一段代码的执行权限吗 祝福上海国际!上海申花! 互斥量 Mutex 疑问 c#datagridview新建行
但现在的浏览器Cookie不好提取的,IE6是可以的
Cookie一般是以txt文本形式存在的,其内部内容没有加密么?
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;
}