码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2017-03-23 10:41:12      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:net   returns   url   ted   time   readline   []   etc   charset   

给游览器写入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

标签:net   returns   url   ted   time   readline   []   etc   charset   

原文地址:http://www.cnblogs.com/likeli/p/6603792.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!