SSLHttpClient.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. namespace HPSocketCS
  7. {
  8. public class HttpsClient : SSLHttpClient
  9. {
  10. public HttpsClient()
  11. : base()
  12. {
  13. }
  14. /// <summary>
  15. ///
  16. /// </summary>
  17. /// <param name="verifyModel">验证模式</param>
  18. /// <param name="pemCertFile">证书文件</param>
  19. /// <param name="pemKeyFile">私钥文件</param>
  20. /// <param name="keyPasswod">私钥密码(没有密码则为空)</param>
  21. /// <param name="caPemCertFileOrPath">CA 证书文件或目录(单向验证或客户端可选)</param>
  22. public HttpsClient(SSLVerifyMode verifyModel, string pemCertFile, string pemKeyFile, string keyPasswod, string caPemCertFileOrPath)
  23. : base(verifyModel, pemCertFile, pemKeyFile, keyPasswod, caPemCertFileOrPath)
  24. {
  25. }
  26. }
  27. public class SSLHttpClient : HttpClient
  28. {
  29. /// <summary>
  30. /// 验证模式
  31. /// </summary>
  32. public SSLVerifyMode VerifyMode { get; set; }
  33. /// <summary>
  34. /// 证书文件(客户端可选)
  35. /// </summary>
  36. public string PemCertFile { get; set; }
  37. /// <summary>
  38. /// 私钥文件(客户端可选)
  39. /// </summary>
  40. public string PemKeyFile { get; set; }
  41. /// <summary>
  42. /// 私钥密码(没有密码则为空)
  43. /// </summary>
  44. public string KeyPasswod { get; set; }
  45. /// <summary>
  46. /// CA 证书文件或目录(单向验证或客户端可选)
  47. /// </summary>
  48. public string CAPemCertFileOrPath { get; set; }
  49. public SSLHttpClient()
  50. {
  51. }
  52. /// <summary>
  53. ///
  54. /// </summary>
  55. /// <param name="verifyModel">验证模式</param>
  56. /// <param name="pemCertFile">证书文件(客户端可选)</param>
  57. /// <param name="pemKeyFile">私钥文件(客户端可选)</param>
  58. /// <param name="keyPasswod">私钥密码(没有密码则为空)</param>
  59. /// <param name="caPemCertFileOrPath">CA 证书文件或目录(单向验证或客户端可选)</param>
  60. public SSLHttpClient(SSLVerifyMode verifyModel, string pemCertFile, string pemKeyFile, string keyPasswod, string caPemCertFileOrPath)
  61. {
  62. this.VerifyMode = verifyModel;
  63. this.PemCertFile = pemCertFile;
  64. this.PemKeyFile = pemKeyFile;
  65. this.KeyPasswod = keyPasswod;
  66. this.CAPemCertFileOrPath = caPemCertFileOrPath;
  67. }
  68. /// <summary>
  69. /// 初始化SSL环境
  70. /// </summary>
  71. /// <returns></returns>
  72. public virtual bool Initialize()
  73. {
  74. if (pClient != IntPtr.Zero)
  75. {
  76. PemCertFile = string.IsNullOrWhiteSpace(PemCertFile) ? null : PemCertFile;
  77. PemKeyFile = string.IsNullOrWhiteSpace(PemKeyFile) ? null : PemKeyFile;
  78. KeyPasswod = string.IsNullOrWhiteSpace(KeyPasswod) ? null : KeyPasswod;
  79. CAPemCertFileOrPath = string.IsNullOrWhiteSpace(CAPemCertFileOrPath) ? null : CAPemCertFileOrPath;
  80. return SSLSdk.HP_SSLClient_SetupSSLContext(pClient, VerifyMode, PemCertFile, PemKeyFile, KeyPasswod, CAPemCertFileOrPath);
  81. }
  82. return false;
  83. }
  84. /// <summary>
  85. /// 反初始化SSL环境
  86. /// </summary>
  87. public virtual void Uninitialize()
  88. {
  89. if (pClient != IntPtr.Zero)
  90. {
  91. SSLSdk.HP_SSLClient_CleanupSSLContext(pClient);
  92. }
  93. }
  94. protected override bool CreateListener()
  95. {
  96. if (IsCreate == true || pListener != IntPtr.Zero || pClient != IntPtr.Zero)
  97. {
  98. return false;
  99. }
  100. pListener = HttpSdk.Create_HP_HttpClientListener();
  101. if (pListener == IntPtr.Zero)
  102. {
  103. return false;
  104. }
  105. pClient = SSLHttpSdk.Create_HP_HttpsClient(pListener);
  106. if (pClient == IntPtr.Zero)
  107. {
  108. return false;
  109. }
  110. IsCreate = true;
  111. return true;
  112. }
  113. public override void Destroy()
  114. {
  115. Stop();
  116. if (pClient != IntPtr.Zero)
  117. {
  118. SSLHttpSdk.Destroy_HP_HttpsClient(pClient);
  119. pClient = IntPtr.Zero;
  120. }
  121. if (pListener != IntPtr.Zero)
  122. {
  123. HttpSdk.Destroy_HP_HttpClientListener(pListener);
  124. pListener = IntPtr.Zero;
  125. }
  126. IsCreate = false;
  127. }
  128. }
  129. }