HttpServer.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. namespace HPSocketCS
  7. {
  8. public class HttpServer : TcpServer
  9. {
  10. /// <summary>
  11. /// 【可选】开始解析
  12. /// </summary>
  13. public event HttpServerEvent.OnMessageBeginEventHandler OnMessageBegin;
  14. /// <summary>
  15. /// 【可选】请求头通知
  16. /// </summary>
  17. public event HttpServerEvent.OnHeaderEventHandler OnHeader;
  18. /// <summary>
  19. /// 【可选】Chunked 报文头通知
  20. /// </summary>
  21. public event HttpServerEvent.OnChunkHeaderEventHandler OnChunkHeader;
  22. /// <summary>
  23. /// 【可选】Chunked 报文结束通知
  24. /// </summary>
  25. public event HttpServerEvent.OnChunkCompleteEventHandler OnChunkComplete;
  26. /// <summary>
  27. /// 【可选】升级协议通知
  28. /// </summary>
  29. public event HttpServerEvent.OnUpgradeEventHandler OnUpgrade;
  30. /// <summary>
  31. /// 【可选】请求行解析完成(仅用于 HTTP 服务端)
  32. /// </summary>
  33. public event HttpServerEvent.OnRequestLineEventHandler OnRequestLine;
  34. /// <summary>
  35. /// 【必须】请求头完成通知
  36. /// </summary>
  37. public event HttpServerEvent.OnHeadersCompleteEventHandler OnHeadersComplete;
  38. /// <summary>
  39. /// 【必须】请求体报文通知
  40. /// </summary>
  41. public event HttpServerEvent.OnBodyEventHandler OnBody;
  42. /// <summary>
  43. /// 【必须】请求体报文通知
  44. /// </summary>
  45. public event HttpServerEvent.OnPointerDataBodyEventHandler OnPointerDataBody;
  46. /// <summary>
  47. /// 【必须】完成解析通知
  48. /// </summary>
  49. public event HttpServerEvent.OnMessageCompleteEventHandler OnMessageComplete;
  50. /// <summary>
  51. /// 【必须】解析错误通知
  52. /// </summary>
  53. public event HttpServerEvent.OnParseErrorEventHandler OnParseError;
  54. /// <summary>
  55. /// 创建socket监听&服务组件
  56. /// </summary>
  57. /// <returns></returns>
  58. protected override bool CreateListener()
  59. {
  60. if (IsCreate == true || pListener != IntPtr.Zero || pServer != IntPtr.Zero)
  61. {
  62. return false;
  63. }
  64. pListener = HttpSdk.Create_HP_HttpServerListener();
  65. if (pListener == IntPtr.Zero)
  66. {
  67. return false;
  68. }
  69. pServer = HttpSdk.Create_HP_HttpServer(pListener);
  70. if (pServer == IntPtr.Zero)
  71. {
  72. return false;
  73. }
  74. IsCreate = true;
  75. return true;
  76. }
  77. /// <summary>
  78. /// 终止服务并释放资源
  79. /// </summary>
  80. public override void Destroy()
  81. {
  82. Stop();
  83. if (pServer != IntPtr.Zero)
  84. {
  85. HttpSdk.Destroy_HP_HttpServer(pServer);
  86. pServer = IntPtr.Zero;
  87. }
  88. if (pListener != IntPtr.Zero)
  89. {
  90. HttpSdk.Destroy_HP_HttpServerListener(pListener);
  91. pListener = IntPtr.Zero;
  92. }
  93. IsCreate = false;
  94. }
  95. HttpSdk.OnMessageBegin _OnMessageBegin;
  96. HttpSdk.OnHeader _OnHeader;
  97. HttpSdk.OnChunkHeader _OnChunkHeader;
  98. HttpSdk.OnChunkComplete _OnChunkComplete;
  99. HttpSdk.OnUpgrade _OnUpgrade;
  100. HttpSdk.OnRequestLine _OnRequestLine;
  101. HttpSdk.OnHeadersComplete _OnHeadersComplete;
  102. HttpSdk.OnBody _OnBody;
  103. HttpSdk.OnMessageComplete _OnMessageComplete;
  104. HttpSdk.OnParseError _OnParseError;
  105. protected override void SetCallback()
  106. {
  107. // 设置tcpserver的callback
  108. _OnPrepareListen = new Sdk.OnPrepareListen(SDK_OnPrepareListen);
  109. _OnAccept = new Sdk.OnAccept(SDK_OnAccept);
  110. _OnSend = new Sdk.OnSend(SDK_OnSend);
  111. _OnReceive = new Sdk.OnReceive(SDK_OnReceive);
  112. _OnClose = new Sdk.OnClose(SDK_OnClose);
  113. _OnShutdown = new Sdk.OnShutdown(SDK_OnShutdown);
  114. _OnHandShake = new Sdk.OnHandShake(SDK_OnHandShake);
  115. HttpSdk.HP_Set_FN_HttpServer_OnPrepareListen(pListener, _OnPrepareListen);
  116. HttpSdk.HP_Set_FN_HttpServer_OnAccept(pListener, _OnAccept);
  117. HttpSdk.HP_Set_FN_HttpServer_OnSend(pListener, _OnSend);
  118. HttpSdk.HP_Set_FN_HttpServer_OnReceive(pListener, _OnReceive);
  119. HttpSdk.HP_Set_FN_HttpServer_OnClose(pListener, _OnClose);
  120. HttpSdk.HP_Set_FN_HttpServer_OnShutdown(pListener, _OnShutdown);
  121. HttpSdk.HP_Set_FN_HttpServer_OnHandShake(pListener, _OnHandShake);
  122. // 设置自身的callback
  123. _OnMessageBegin = new HttpSdk.OnMessageBegin(SDK_OnMessageBegin);
  124. _OnHeader = new HttpSdk.OnHeader(SDK_OnHeader);
  125. _OnChunkHeader = new HttpSdk.OnChunkHeader(SDK_OnChunkHeader);
  126. _OnChunkComplete = new HttpSdk.OnChunkComplete(SDK_OnChunkComplete);
  127. _OnUpgrade = new HttpSdk.OnUpgrade(SDK_OnUpgrade);
  128. _OnRequestLine = new HttpSdk.OnRequestLine(SDK_OnRequestLine);
  129. _OnHeadersComplete = new HttpSdk.OnHeadersComplete(SDK_OnHeadersComplete);
  130. _OnBody = new HttpSdk.OnBody(SDK_OnBody);
  131. _OnMessageComplete = new HttpSdk.OnMessageComplete(SDK_OnMessageComplete);
  132. _OnParseError = new HttpSdk.OnParseError(SDK_OnParseError);
  133. HttpSdk.HP_Set_FN_HttpServer_OnMessageBegin(pListener, _OnMessageBegin);
  134. HttpSdk.HP_Set_FN_HttpServer_OnRequestLine(pListener, _OnRequestLine);
  135. HttpSdk.HP_Set_FN_HttpServer_OnHeader(pListener, _OnHeader);
  136. HttpSdk.HP_Set_FN_HttpServer_OnHeadersComplete(pListener, _OnHeadersComplete);
  137. HttpSdk.HP_Set_FN_HttpServer_OnBody(pListener, _OnBody);
  138. HttpSdk.HP_Set_FN_HttpServer_OnChunkHeader(pListener, _OnChunkHeader);
  139. HttpSdk.HP_Set_FN_HttpServer_OnChunkComplete(pListener, _OnChunkComplete);
  140. HttpSdk.HP_Set_FN_HttpServer_OnMessageComplete(pListener, _OnMessageComplete);
  141. HttpSdk.HP_Set_FN_HttpServer_OnUpgrade(pListener, _OnUpgrade);
  142. HttpSdk.HP_Set_FN_HttpServer_OnParseError(pListener, _OnParseError);
  143. }
  144. protected virtual HttpParseResult SDK_OnParseError(IntPtr pSender, IntPtr connId, int iErrorCode, string lpszErrorDesc)
  145. {
  146. if (OnParseError != null)
  147. {
  148. return OnParseError(connId, iErrorCode, lpszErrorDesc);
  149. }
  150. return HttpParseResult.Ok;
  151. }
  152. protected virtual HttpParseResult SDK_OnMessageComplete(IntPtr pSender, IntPtr connId)
  153. {
  154. if (OnMessageComplete != null)
  155. {
  156. return OnMessageComplete(connId);
  157. }
  158. return HttpParseResult.Ok;
  159. }
  160. protected virtual HttpParseResult SDK_OnBody(IntPtr pSender, IntPtr connId, IntPtr pData, int iLength)
  161. {
  162. if (OnPointerDataBody != null)
  163. {
  164. return OnPointerDataBody(connId, pData, iLength);
  165. }
  166. else if (OnBody != null)
  167. {
  168. byte[] bytes = new byte[iLength];
  169. Marshal.Copy(pData, bytes, 0, iLength);
  170. return OnBody(connId, bytes);
  171. }
  172. return HttpParseResult.Ok;
  173. }
  174. protected virtual HttpParseResultEx SDK_OnHeadersComplete(IntPtr pSender, IntPtr connId)
  175. {
  176. if (OnHeadersComplete != null)
  177. {
  178. return OnHeadersComplete(connId);
  179. }
  180. return HttpParseResultEx.Ok;
  181. }
  182. protected virtual HttpParseResult SDK_OnRequestLine(IntPtr pSender, IntPtr connId, string lpszMethod, string lpszUrl)
  183. {
  184. if (OnRequestLine != null)
  185. {
  186. return OnRequestLine(connId, lpszMethod, lpszUrl);
  187. }
  188. return HttpParseResult.Ok;
  189. }
  190. protected virtual HttpParseResult SDK_OnUpgrade(IntPtr pSender, IntPtr connId, HttpUpgradeType enUpgradeType)
  191. {
  192. if (OnUpgrade != null)
  193. {
  194. return OnUpgrade(connId, enUpgradeType);
  195. }
  196. return HttpParseResult.Ok;
  197. }
  198. protected virtual HttpParseResult SDK_OnChunkComplete(IntPtr pSender, IntPtr connId)
  199. {
  200. if (OnChunkComplete != null)
  201. {
  202. return OnChunkComplete(connId);
  203. }
  204. return HttpParseResult.Ok;
  205. }
  206. protected virtual HttpParseResult SDK_OnChunkHeader(IntPtr pSender, IntPtr connId, int iLength)
  207. {
  208. if (OnChunkHeader != null)
  209. {
  210. return OnChunkHeader(connId, iLength);
  211. }
  212. return HttpParseResult.Ok;
  213. }
  214. protected virtual HttpParseResult SDK_OnHeader(IntPtr pSender, IntPtr connId, string lpszName, string lpszValue)
  215. {
  216. if (OnHeader != null)
  217. {
  218. return OnHeader(connId, lpszName, lpszValue);
  219. }
  220. return HttpParseResult.Ok;
  221. }
  222. protected virtual HttpParseResult SDK_OnMessageBegin(IntPtr pSender, IntPtr connId)
  223. {
  224. if (OnMessageBegin != null)
  225. {
  226. return OnMessageBegin(connId);
  227. }
  228. return HttpParseResult.Ok;
  229. }
  230. /**************************************************************************/
  231. /***************************** Server 操作方法 *****************************/
  232. /// <summary>
  233. /// 名称:回复请求
  234. /// 描述:向客户端回复 HTTP 请求
  235. /// </summary>
  236. /// <param name="connId">连接 ID</param>
  237. /// <param name="httpStatusCode">HTTP 状态码</param>
  238. /// <param name="desc">HTTP 状态描述</param>
  239. /// <param name="headers">回复请求头</param>
  240. /// <param name="body">回复请求体</param>
  241. /// <returns></returns>
  242. public bool SendResponse(IntPtr connId, HttpStatusCode httpStatusCode, string desc, THeader[] headers, byte[] body, int bodyLength)
  243. {
  244. int headersLength = headers == null ? 0 : headers.Length;
  245. return HttpSdk.HP_HttpServer_SendResponse(pServer, connId, (ushort)httpStatusCode, desc, headers, headersLength, body, bodyLength);
  246. }
  247. /// <summary>
  248. /// 名称:发送本地文件
  249. /// 描述:向指定连接发送 4096 KB 以下的小文件
  250. /// </summary>
  251. /// <param name="connId"></param>
  252. /// <param name="fileName"></param>
  253. /// <param name="httpStatusCode"></param>
  254. /// <param name="desc"></param>
  255. /// <param name="headers"></param>
  256. /// <returns></returns>
  257. public bool SendLocalFile(IntPtr connId, string fileName, HttpStatusCode httpStatusCode, string desc, THeader[] headers)
  258. {
  259. int headersLength = headers == null ? 0 : headers.Length;
  260. return HttpSdk.HP_HttpServer_SendLocalFile(pServer, connId, fileName, (ushort)httpStatusCode, desc, headers, headersLength);
  261. }
  262. /// <summary>
  263. /// 名称:释放连接
  264. /// 描述:把连接放入释放队列,等待某个时间(通过 SetReleaseDelay() 设置)关闭连接
  265. /// </summary>
  266. /// <param name="connId">连接 ID</param>
  267. /// <returns></returns>
  268. public bool Release(IntPtr connId)
  269. {
  270. return HttpSdk.HP_HttpServer_Release(pServer, connId);
  271. }
  272. /// <summary>
  273. /// 启动 HTTP 通信, 当通信组件设置为非自动启动 HTTP 通信时,需要调用本方法启动 HTTP 通信
  274. /// </summary>
  275. /// <param name="connId"></param>
  276. /// <returns></returns>
  277. public bool StartHttp(IntPtr connId)
  278. {
  279. return HttpSdk.HP_HttpServer_StartHttp(pServer, connId);
  280. }
  281. /******************************************************************************/
  282. /***************************** Server 属性访问方法 *****************************/
  283. /// <summary>
  284. /// 获取或设置连接释放延时(默认:3000 毫秒)
  285. /// </summary>
  286. public uint ReleaseDelay
  287. {
  288. get
  289. {
  290. return HttpSdk.HP_HttpServer_GetReleaseDelay(pServer);
  291. }
  292. set
  293. {
  294. HttpSdk.HP_HttpServer_SetReleaseDelay(pServer, value);
  295. }
  296. }
  297. /// <summary>
  298. /// 获取请求行 URL 域掩码(URL 域参考:HttpUrlField)
  299. /// </summary>
  300. /// <param name="connId"></param>
  301. /// <returns></returns>
  302. public HttpUrlField GetUrlFieldSet(IntPtr connId)
  303. {
  304. return (HttpUrlField)HttpSdk.HP_HttpServer_GetUrlFieldSet(pServer, connId);
  305. }
  306. /// <summary>
  307. /// 获取某个 URL 域值
  308. /// </summary>
  309. /// <param name="connId"></param>
  310. /// <param name="enField"></param>
  311. /// <returns></returns>
  312. public string GetUrlField(IntPtr connId, HttpUrlField enField)
  313. {
  314. IntPtr ptr = HttpSdk.HP_HttpServer_GetUrlField(pServer, connId, enField);
  315. return Common.PtrToAnsiString(ptr);
  316. }
  317. /// <summary>
  318. /// 获取请求方法
  319. /// </summary>
  320. /// <param name="connId"></param>
  321. /// <returns></returns>
  322. public string GetMethod(IntPtr connId)
  323. {
  324. IntPtr ptr = HttpSdk.HP_HttpServer_GetMethod(pServer, connId);
  325. return Common.PtrToAnsiString(ptr);
  326. }
  327. /// <summary>
  328. /// 获取或设置本地协议版本
  329. /// </summary>
  330. public HttpVersion LocalVersion
  331. {
  332. get
  333. {
  334. return HttpSdk.HP_HttpServer_GetLocalVersion(pServer);
  335. }
  336. set
  337. {
  338. HttpSdk.HP_HttpServer_SetLocalVersion(pServer, value);
  339. }
  340. }
  341. /// <summary>
  342. /// 检查是否升级协议
  343. /// </summary>
  344. /// <param name="connId"></param>
  345. /// <returns></returns>
  346. public bool IsUpgrade(IntPtr connId)
  347. {
  348. return HttpSdk.HP_HttpServer_IsUpgrade(pServer, connId);
  349. }
  350. /// <summary>
  351. /// 检查是否有 Keep-Alive 标识
  352. /// </summary>
  353. /// <param name="connId"></param>
  354. /// <returns></returns>
  355. public bool IsKeepAlive(IntPtr connId)
  356. {
  357. return HttpSdk.HP_HttpServer_IsKeepAlive(pServer, connId);
  358. }
  359. /// <summary>
  360. /// 获取协议版本
  361. /// </summary>
  362. /// <param name="connId"></param>
  363. /// <returns></returns>
  364. public HttpVersion GetVersion(IntPtr connId)
  365. {
  366. return (HttpVersion)HttpSdk.HP_HttpServer_GetVersion(pServer, connId);
  367. }
  368. /// <summary>
  369. /// 获取主机
  370. /// </summary>
  371. /// <param name="connId"></param>
  372. /// <returns></returns>
  373. public string GetHost(IntPtr connId)
  374. {
  375. IntPtr ptr = HttpSdk.HP_HttpServer_GetHost(pServer, connId);
  376. return Common.PtrToAnsiString(ptr);
  377. }
  378. /// <summary>
  379. /// 获取内容长度
  380. /// </summary>
  381. /// <param name="connId"></param>
  382. /// <returns></returns>
  383. public long GetContentLength(IntPtr connId)
  384. {
  385. return HttpSdk.HP_HttpServer_GetContentLength(pServer, connId);
  386. }
  387. /// <summary>
  388. /// 获取内容类型
  389. /// </summary>
  390. /// <param name="connId"></param>
  391. /// <returns></returns>
  392. public string GetContentType(IntPtr connId)
  393. {
  394. IntPtr ptr = HttpSdk.HP_HttpServer_GetContentType(pServer, connId);
  395. return Common.PtrToAnsiString(ptr);
  396. }
  397. /// <summary>
  398. /// 获取内容类型
  399. /// </summary>
  400. /// <returns></returns>
  401. public string GetContentEncoding(IntPtr connId)
  402. {
  403. IntPtr ptr = HttpSdk.HP_HttpServer_GetContentEncoding(pServer, connId);
  404. return Common.PtrToAnsiString(ptr);
  405. }
  406. /// <summary>
  407. /// 获取传输编码
  408. /// </summary>
  409. /// <returns></returns>
  410. public string GetTransferEncoding(IntPtr connId)
  411. {
  412. IntPtr ptr = HttpSdk.HP_HttpServer_GetTransferEncoding(pServer, connId);
  413. return Common.PtrToAnsiString(ptr);
  414. }
  415. /// <summary>
  416. /// 获取协议升级类型
  417. /// </summary>
  418. /// <param name="connId"></param>
  419. /// <returns></returns>
  420. public HttpUpgradeType GetUpgradeType(IntPtr connId)
  421. {
  422. return HttpSdk.HP_HttpServer_GetUpgradeType(pServer, connId);
  423. }
  424. /// <summary>
  425. /// 获取解析错误代码
  426. /// </summary>
  427. /// <param name="connId"></param>
  428. /// <returns></returns>
  429. public ErrorInfo GetParseErrorInfo(IntPtr connId)
  430. {
  431. IntPtr ptr = IntPtr.Zero;
  432. var error = new ErrorInfo();
  433. error.ErrorCode = HttpSdk.HP_HttpServer_GetParseErrorCode(pServer, connId, ref ptr);
  434. if (ptr != IntPtr.Zero)
  435. {
  436. try
  437. {
  438. error.ErrorMsg = Common.PtrToAnsiString(ptr); ;
  439. }
  440. catch (Exception)
  441. {
  442. }
  443. }
  444. return error;
  445. }
  446. /// <summary>
  447. /// 获取某个请求头(单值)
  448. /// </summary>
  449. /// <param name="connId"></param>
  450. /// <param name="key"></param>
  451. /// <returns></returns>
  452. public string GetHeader(IntPtr connId, string key)
  453. {
  454. IntPtr ptr = IntPtr.Zero;
  455. if (HttpSdk.HP_HttpServer_GetHeader(pServer, connId, key, ref ptr))
  456. {
  457. return Common.PtrToAnsiString(ptr);
  458. }
  459. return "";
  460. }
  461. /// <summary>
  462. /// 获取某个请求头(多值)
  463. /// </summary>
  464. /// <param name="connId"></param>
  465. /// <param name="key"></param>
  466. /// <returns></returns>
  467. public List<string> GetHeaders(IntPtr connId, string key)
  468. {
  469. var list = new List<string>();
  470. try
  471. {
  472. IntPtr[] arr = null;
  473. uint count = 0;
  474. HttpSdk.HP_HttpServer_GetHeaders(pServer, connId, key, arr, ref count);
  475. if (count > 0)
  476. {
  477. arr = new IntPtr[count];
  478. if (HttpSdk.HP_HttpServer_GetHeaders(pServer, connId, key, arr, ref count) && count > 0)
  479. {
  480. foreach (var item in arr)
  481. {
  482. list.Add(Common.PtrToAnsiString(item));
  483. }
  484. }
  485. }
  486. }
  487. catch (Exception)
  488. {
  489. }
  490. return list;
  491. }
  492. /// <summary>
  493. /// 获取所有请求头
  494. /// </summary>
  495. /// <param name="connId"></param>
  496. /// <returns></returns>
  497. public List<THeader> GetAllHeaders(IntPtr connId)
  498. {
  499. var list = new List<THeader>();
  500. try
  501. {
  502. uint count = 0;
  503. IntPtr arrPtr = IntPtr.Zero;
  504. HttpSdk.HP_HttpServer_GetAllHeaders(pServer, connId, arrPtr, ref count);
  505. if (count > 0)
  506. {
  507. var ptrArr = new THeaderForIntPtr[count];
  508. arrPtr = Marshal.UnsafeAddrOfPinnedArrayElement(ptrArr, 0);
  509. if (HttpSdk.HP_HttpServer_GetAllHeaders(pServer, connId, arrPtr, ref count))
  510. {
  511. foreach (var item in ptrArr)
  512. {
  513. list.Add(new THeader()
  514. {
  515. Name = Common.PtrToAnsiString(item.Name),
  516. Value = Common.PtrToAnsiString(item.Value),
  517. });
  518. }
  519. }
  520. }
  521. }
  522. catch (Exception)
  523. {
  524. }
  525. return list;
  526. }
  527. /// <summary>
  528. /// 获取所有请求头名称
  529. /// </summary>
  530. /// <param name="connId"></param>
  531. /// <returns></returns>
  532. public List<string> GetAllHeaderNames(IntPtr connId)
  533. {
  534. var list = new List<string>();
  535. try
  536. {
  537. IntPtr[] arr = null;
  538. uint count = 0;
  539. HttpSdk.HP_HttpServer_GetAllHeaderNames(pServer, connId, arr, ref count);
  540. if (count > 0)
  541. {
  542. arr = new IntPtr[count];
  543. if (HttpSdk.HP_HttpServer_GetAllHeaderNames(pServer, connId, arr, ref count) && count > 0)
  544. {
  545. foreach (var item in arr)
  546. {
  547. list.Add(Common.PtrToAnsiString(item));
  548. }
  549. }
  550. }
  551. }
  552. catch (Exception)
  553. {
  554. }
  555. return list;
  556. }
  557. /// <summary>
  558. /// 获取Cookie
  559. /// </summary>
  560. /// <param name="connId"></param>
  561. /// <param name="key"></param>
  562. /// <returns></returns>
  563. public string GetCookie(IntPtr connId, string key)
  564. {
  565. IntPtr ptr = IntPtr.Zero;
  566. if (HttpSdk.HP_HttpServer_GetCookie(pServer, connId, key, ref ptr))
  567. {
  568. return Common.PtrToAnsiString(ptr);
  569. }
  570. return "";
  571. }
  572. /// <summary>
  573. /// 获取所有 Cookie
  574. /// </summary>
  575. /// <param name="connId"></param>
  576. /// <param name="lpCookies"></param>
  577. /// <param name="pdwCount"></param>
  578. /// <returns></returns>
  579. public List<TCookie> GetAllCookies(IntPtr connId)
  580. {
  581. var list = new List<TCookie>();
  582. try
  583. {
  584. uint count = 0;
  585. IntPtr arrPtr = IntPtr.Zero;
  586. HttpSdk.HP_HttpServer_GetAllCookies(pServer, connId, arrPtr, ref count);
  587. if (count > 0)
  588. {
  589. var ptrArr = new THeaderForIntPtr[count];
  590. arrPtr = Marshal.UnsafeAddrOfPinnedArrayElement(ptrArr, 0);
  591. if (HttpSdk.HP_HttpServer_GetAllCookies(pServer, connId, arrPtr, ref count))
  592. {
  593. foreach (var item in ptrArr)
  594. {
  595. list.Add(new TCookie()
  596. {
  597. Name = Common.PtrToAnsiString(item.Name),
  598. Value = Common.PtrToAnsiString(item.Value)
  599. });
  600. }
  601. }
  602. }
  603. }
  604. catch (Exception)
  605. {
  606. }
  607. return list;
  608. }
  609. /// <summary>
  610. /// 获取或设置 HTTP 启动方式,默认为true
  611. /// </summary>
  612. public bool HttpAutoStart
  613. {
  614. get
  615. {
  616. return HttpSdk.HP_HttpServer_IsHttpAutoStart(pServer);
  617. }
  618. set
  619. {
  620. HttpSdk.HP_HttpServer_SetHttpAutoStart(pServer, value);
  621. }
  622. }
  623. }
  624. }