首页 > 代码库 > C# 调用动态链接库,给游览器写入Cookie

C# 调用动态链接库,给游览器写入Cookie

给游览器写入Cookie

  1. class Program {
  2. /// <summary>
  3. /// 写
  4. /// </summary>
  5. /// <param name="lpszUrlName"></param>
  6. /// <param name="lbszCookieName"></param>
  7. /// <param name="lpszCookieData"></param>
  8. /// <returns></returns>
  9. [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
  10. public static extern bool InternetSetCookie ( string lpszUrlName, string lbszCookieName, string lpszCookieData );
  11. /// <summary>
  12. /// 读
  13. /// </summary>
  14. /// <param name="lpszUrlName"></param>
  15. /// <param name="lbszCookieName"></param>
  16. /// <param name="lpszCookieData"></param>
  17. /// <param name="lpdwSize"></param>
  18. /// <returns></returns>
  19. [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
  20. public static extern bool InternetGetCookie ( string lpszUrlName, string lbszCookieName, StringBuilder lpszCookieData, ref int lpdwSize );
  21. /// <summary>
  22. /// 异常
  23. /// </summary>
  24. /// <returns></returns>
  25. [DllImport("kernel32.dll")]
  26. public static extern Int32 GetLastError ();
  27. static void Main ( string[] args ) {
  28. System.Diagnostics.Process.Start("IEXPLORE.EXE", "http://172.16.5.63:8088/ws?uname=122");
  29. System.Threading.Thread.Sleep(1000 * 10);
  30. //System.Diagnostics.Process.Start("http://172.16.5.63:8088/ws?uname=122");
  31. if ( !InternetSetCookie("http://172.16.5.63:8088", "Test", DateTime.Now + ";expires=Sun,22-Feb-2099 00:00:00 GMT") )
  32. {
  33. Console.WriteLine("错误消息:", GetLastError().ToString());
  34. }
  35. StringBuilder cookie = new StringBuilder(new String(‘ ‘, 2048));
  36. int datasize = cookie.Length;
  37. if ( !InternetGetCookie("http://172.16.5.63:8088", "Test", cookie, ref datasize) )
  38. {
  39. Console.WriteLine(GetLastError().ToString());
  40. }
  41. Console.WriteLine(cookie.ToString(), datasize);
  42. Console.ReadLine();
  43. }
  44. }

C# 调用动态链接库,给游览器写入Cookie