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

Posted Li.Goper

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

在与 Rust 中的本机库链接时将符号公开给动态链接器

python中调用 C#动态链接库问题记录

django学习之- Cookie

cookie是啥?有啥用?cookie详解,一篇文章彻底搞懂cookie

C#总结动态加载C++动态链接库

C#调用C++语言编写的动态链接库