本帖最后由 庸亼臫扰]。 于 2014-9-25 19:00 编辑
[C#] 纯文本查看 复制代码 [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool InternetGetCookieEx(string pchURL, string pchCookieName, StringBuilder pchCookieData, ref
System.UInt32 pcchCookieData, int dwFlags, IntPtr lpReserved);
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int InternetSetCookieEx(string lpszURL, string lpszCookieName, string lpszCookieData, int dwFlags,
IntPtr dwReserved);
private static string GetCookies(string url)
{
uint datasize = 256;
StringBuilder cookieData = new StringBuilder((int)datasize);
if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x2000, IntPtr.Zero))
{
if (datasize < 0)
return null;
cookieData = new StringBuilder((int)datasize);
if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, IntPtr.Zero))
return null;
}
return cookieData.ToString();
}
public static string SetCookieForIE(string url,[color=#ff0000][b]CookieCollection cookiestring[/b][/color],string loginsuccess="www.baidu.com", string path="/")
{
try
{
foreach (Cookie c in [color=#ff0000][b]cookiestring[/b][/color])
{
string name =c.Name;
string value = c.Value + ";expires=" + DateTime.Now.AddDays(1).ToString("r");
if (!InternetSetCookie(url, name, value))
{
return GetLastError().ToString();
}
}
//注册字符串类型cookie
//InternetSetCookie(url, "", cookiestring);
System.Diagnostics.Process.Start("IEXPLORE.EXE", "http://19.133.112.8/jszm/hold.asp");
return GetCookies(url);
}
catch (Exception)
{
return "false";
throw;
}
}
GetCookies(url) 的确获取到了, 就是在process 打开浏览器后还需要登陆, 查看了下IE面的cookie文件都有
|