HttpSdk.cs 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. namespace HPSocketCS
  9. {
  10. public static class ____LinqEx
  11. {
  12. public static byte LOBYTE(this HttpVersion version)
  13. {
  14. return (byte)((ushort)version & 0xff);
  15. }
  16. public static byte HIBYTE(this HttpVersion version)
  17. {
  18. return (byte)((ushort)version >> 8);//高8位
  19. }
  20. public static string ToString(this HttpMethod method)
  21. {
  22. var fi = method.GetType().GetField(method.ToString());
  23. var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
  24. return (attributes.Length > 0) ? attributes[0].Description : method.ToString();
  25. }
  26. }
  27. public enum WSOpcode : byte
  28. {
  29. Cont = 0x0,
  30. Text = 0x1,
  31. Binary = 0x2,
  32. Close = 0x8,
  33. Ping = 0x9,
  34. Pong = 0xa,
  35. }
  36. public enum WSReserved : byte
  37. {
  38. Off = 0x0,
  39. On = 0x1
  40. }
  41. public class WSMessageState
  42. {
  43. public bool Final { get; set; }
  44. public WSReserved Reserved { get; set; }
  45. public WSOpcode OperationCode { get; set; }
  46. public byte[] Mask { get; set; }
  47. public ulong BodyLen { get; set; }
  48. public ulong BodyRemain { get; set; }
  49. }
  50. /// <summary>
  51. /// http版本号
  52. /// </summary>
  53. public enum HttpVersion
  54. {
  55. /// <summary>
  56. /// http 1.0
  57. /// </summary>
  58. V1_0 = 1,
  59. /// <summary>
  60. /// http 1.1
  61. /// </summary>
  62. v1_1 = 257,
  63. }
  64. /// <summary>
  65. /// URL 域
  66. /// HTTP 请求行中 URL 段位的域定义
  67. /// </summary>
  68. public enum HttpUrlField
  69. {
  70. Schema = 0,
  71. Host = 1,
  72. Port = 2,
  73. Path = 3,
  74. QueryString = 4,
  75. Fragment = 5,
  76. UserInfo = 6,
  77. /// <summary>
  78. /// Field Count
  79. /// </summary>
  80. Max = 7,
  81. }
  82. /// <summary>
  83. /// HTTP 解析结果标识
  84. /// 指示 HTTP 解析器是否继续执行解析操作
  85. /// </summary>
  86. public enum HttpParseResult
  87. {
  88. /// <summary>
  89. /// 终止解析,断开连接
  90. /// </summary>
  91. Error = -1,
  92. /// <summary>
  93. /// 继续解析
  94. /// </summary>
  95. Ok = 0,
  96. }
  97. /// <summary>
  98. /// HTTP 解析结果标识
  99. /// 指示 HTTP 解析器是否继续执行解析操作
  100. /// </summary>
  101. public enum HttpParseResultEx
  102. {
  103. /// <summary>
  104. /// 终止解析,断开连接
  105. /// </summary>
  106. Error = -1,
  107. /// <summary>
  108. /// 继续解析
  109. /// </summary>
  110. Ok = 0,
  111. /// <summary>
  112. /// 跳过当前请求 BODY
  113. /// </summary>
  114. SkipBody = 1,
  115. /// <summary>
  116. /// 升级协议
  117. /// </summary>
  118. Upgrade = 2,
  119. }
  120. public enum HttpUpgradeType
  121. {
  122. /// <summary>
  123. /// 没有升级
  124. /// </summary>
  125. None = 0,
  126. /// <summary>
  127. /// WebSocket
  128. /// </summary>
  129. WebSocket = 1,
  130. /// <summary>
  131. /// HTTP 隧道
  132. /// </summary>
  133. HttpTunnel = 2,
  134. /// <summary>
  135. /// 未知类型
  136. /// </summary>
  137. Unknown = -1,
  138. }
  139. public enum HttpStatusCode
  140. {
  141. Continue = 100,
  142. SwitchingProtocols = 101,
  143. Processing = 102,
  144. Ok = 200,
  145. Created = 201,
  146. Accepted = 202,
  147. NonAuthoritativeInformation = 203,
  148. NoContent = 204,
  149. ResetContent = 205,
  150. PartialContent = 206,
  151. MultiStatus = 207,
  152. MultipleChoices = 300,
  153. MovedPermanently = 301,
  154. MovedTemporarily = 302,
  155. SeeOther = 303,
  156. NotModified = 304,
  157. UseProxy = 305,
  158. SwitchProxy = 306,
  159. TemporaryRedirect = 307,
  160. BadRequest = 400,
  161. Unauthorized = 401,
  162. PaymentRequired = 402,
  163. Forbidden = 403,
  164. NotFound = 404,
  165. MethodNotAllowed = 405,
  166. NotAcceptable = 406,
  167. ProxyAuthenticationRequired = 407,
  168. RequestTimeout = 408,
  169. Conflict = 409,
  170. Gone = 410,
  171. LengthRequired = 411,
  172. PreconditionFailed = 412,
  173. RequestEntityTooLarge = 413,
  174. RequestUriTooLong = 414,
  175. UnsupportedMediaType = 415,
  176. RequestedRangeNotSatisfiable = 416,
  177. ExpectationFailed = 417,
  178. UnprocessableEntity = 422,
  179. Locked = 423,
  180. FailedDependency = 424,
  181. UnorderedCollection = 435,
  182. UpgradeRequired = 426,
  183. RetryWith = 449,
  184. InternalServerError = 500,
  185. NotImplemented = 501,
  186. BadGateway = 502,
  187. ServiceUnavailable = 503,
  188. GatewayTimeout = 504,
  189. HttpVersionNotSupported = 505,
  190. VariantAlsoNegotiates = 506,
  191. InsufficientStorage = 507,
  192. BandwidthLimitExceeded = 509,
  193. NotExtended = 510,
  194. UnparseableResponseHeaders = 600,
  195. }
  196. /// <summary>
  197. /// HttpMethod
  198. /// </summary>
  199. public enum HttpMethod
  200. {
  201. [Description("GET")]
  202. Get,
  203. [Description("POST")]
  204. Post,
  205. [Description("PUT")]
  206. Put,
  207. [Description("PATCH")]
  208. Patch,
  209. [Description("DELETE")]
  210. Delete,
  211. [Description("HEAD")]
  212. Head,
  213. [Description("TRACE")]
  214. Trace,
  215. [Description("OPTIONS")]
  216. Options,
  217. [Description("CONNECT")]
  218. Connect,
  219. }
  220. /// <summary>
  221. /// Name/Value 结构体
  222. /// 字符串名值对结构体
  223. /// </summary>
  224. public struct TNVPair
  225. {
  226. [MarshalAs(UnmanagedType.LPStr)]
  227. public string Name;
  228. [MarshalAs(UnmanagedType.LPStr)]
  229. public string Value;
  230. }
  231. public struct TCookie
  232. {
  233. [MarshalAs(UnmanagedType.LPStr)]
  234. public string Name;
  235. [MarshalAs(UnmanagedType.LPStr)]
  236. public string Value;
  237. }
  238. public struct THeader
  239. {
  240. [MarshalAs(UnmanagedType.LPStr)]
  241. public string Name;
  242. [MarshalAs(UnmanagedType.LPStr)]
  243. public string Value;
  244. }
  245. public struct THeaderForIntPtr
  246. {
  247. public IntPtr Name;
  248. public IntPtr Value;
  249. }
  250. /// <summary>
  251. /// http事件
  252. /// </summary>
  253. public class HttpEvent
  254. {
  255. public delegate HttpParseResult OnMessageBeginEventHandler(IntPtr connId);
  256. public delegate HttpParseResult OnHeaderEventHandler(IntPtr connId, string name, string value);
  257. public delegate HttpParseResultEx OnHeadersCompleteEventHandler(IntPtr connId);
  258. public delegate HttpParseResult OnPointerDataBodyEventHandler(IntPtr connId, IntPtr pData, int length);
  259. public delegate HttpParseResult OnBodyEventHandler(IntPtr connId, byte[] bytes);
  260. public delegate HttpParseResult OnChunkHeaderEventHandler(IntPtr connId, int length);
  261. public delegate HttpParseResult OnChunkCompleteEventHandler(IntPtr connId);
  262. public delegate HttpParseResult OnMessageCompleteEventHandler(IntPtr connId);
  263. public delegate HttpParseResult OnUpgradeEventHandler(IntPtr connId, HttpUpgradeType upgradeType);
  264. public delegate HttpParseResult OnParseErrorEventHandler(IntPtr connId, int errorCode, string errorDesc);
  265. }
  266. /// <summary>
  267. /// http server 事件
  268. /// </summary>
  269. public class HttpServerEvent : HttpEvent
  270. {
  271. public delegate HttpParseResult OnRequestLineEventHandler(IntPtr connId, string method, string url);
  272. }
  273. /// <summary>
  274. /// http cilent 事件
  275. /// </summary>
  276. public class HttpClientEvent : HttpEvent
  277. {
  278. public delegate HttpParseResult OnStatusLineEventHandler(IntPtr connId, ushort statusCode, string desc);
  279. }
  280. /// <summary>
  281. /// http agent 事件
  282. /// </summary>
  283. public class HttpAgentEvent : HttpClientEvent
  284. {
  285. }
  286. /// <summary>
  287. /// websocket 事件
  288. /// </summary>
  289. public class WebSocketEvent : HttpEvent
  290. {
  291. public delegate HandleResult OnWSMessageHeaderEventHandler(IntPtr connId, bool final, byte reserved, byte operationCode, byte[] mask, ulong bodyLength);
  292. public delegate HandleResult OnPointerWSMessageBodyEventHandler(IntPtr connId, IntPtr pData, int length);
  293. public delegate HandleResult OnWSMessageBodyEventHandler(IntPtr connId, byte[] data);
  294. public delegate HandleResult OnWSMessageCompleteEventHandler(IntPtr connId);
  295. }
  296. public class HttpSdk
  297. {
  298. /****************************************************/
  299. /******************* HTTP 回调函数 *******************/
  300. /* Agent & Server & Clent */
  301. public delegate HttpParseResult OnMessageBegin(IntPtr pSender, IntPtr connId);
  302. public delegate HttpParseResult OnHeader(IntPtr pSender, IntPtr connId, string lpszName, string lpszValue);
  303. public delegate HttpParseResultEx OnHeadersComplete(IntPtr pSender, IntPtr connId);
  304. public delegate HttpParseResult OnBody(IntPtr pSender, IntPtr connId, IntPtr pData, int iLength);
  305. public delegate HttpParseResult OnChunkHeader(IntPtr pSender, IntPtr connId, int iLength);
  306. public delegate HttpParseResult OnChunkComplete(IntPtr pSender, IntPtr connId);
  307. public delegate HttpParseResult OnMessageComplete(IntPtr pSender, IntPtr connId);
  308. public delegate HttpParseResult OnUpgrade(IntPtr pSender, IntPtr connId, HttpUpgradeType enUpgradeType);
  309. public delegate HttpParseResult OnParseError(IntPtr pSender, IntPtr connId, int iErrorCode, string lpszErrorDesc);
  310. /* Server */
  311. public delegate HttpParseResult OnRequestLine(IntPtr pSender, IntPtr connId, string lpszMethod, string lpszUrl);
  312. /* Agent & Client */
  313. public delegate HttpParseResult OnStatusLine(IntPtr pSender, IntPtr connId, ushort usStatusCode, string lpszDesc);
  314. /* WebSocket */
  315. public delegate HandleResult OnWSMessageHeader(IntPtr pSender, IntPtr dwConnID, bool bFinal, byte iReserved, byte iOperationCode, byte[] lpszMask, ulong ullBodyLen);
  316. public delegate HandleResult OnWSMessageBody(IntPtr pSender, IntPtr dwConnID, IntPtr pData, int length);
  317. public delegate HandleResult OnWSMessageComplete(IntPtr pSender, IntPtr dwConnID);
  318. /****************************************************/
  319. /***************** HTTP 对象创建函数 *****************/
  320. /// <summary>
  321. /// 创建 HP_HttpServer 对象
  322. /// </summary>
  323. /// <param name="pListener"></param>
  324. /// <returns></returns>
  325. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  326. public static extern IntPtr Create_HP_HttpServer(IntPtr pListener);
  327. /// <summary>
  328. /// 创建 HP_HttpAgent 对象
  329. /// </summary>
  330. /// <param name="pListener"></param>
  331. /// <returns></returns>
  332. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  333. public static extern IntPtr Create_HP_HttpAgent(IntPtr pListener);
  334. /// <summary>
  335. /// 创建 HP_HttpClient 对象
  336. /// </summary>
  337. /// <param name="pListener"></param>
  338. /// <returns></returns>
  339. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  340. public static extern IntPtr Create_HP_HttpClient(IntPtr pListener);
  341. /// <summary>
  342. /// 创建 HP_HttpSyncClient 对象
  343. /// </summary>
  344. /// <param name="pListener"></param>
  345. /// <returns></returns>
  346. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  347. public static extern IntPtr Create_HP_HttpSyncClient(IntPtr pListener);
  348. /// <summary>
  349. /// 销毁 HP_HttpServer 对象
  350. /// </summary>
  351. /// <param name="pServer"></param>
  352. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  353. public static extern void Destroy_HP_HttpServer(IntPtr pServer);
  354. /// <summary>
  355. /// 销毁 HP_HttpAgent 对象
  356. /// </summary>
  357. /// <param name="pAgent"></param>
  358. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  359. public static extern void Destroy_HP_HttpAgent(IntPtr pAgent);
  360. /// <summary>
  361. /// 销毁 HP_HttpClient 对象
  362. /// </summary>
  363. /// <param name="pClient"></param>
  364. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  365. public static extern void Destroy_HP_HttpClient(IntPtr pClient);
  366. /// <summary>
  367. /// 销毁 HP_HttpSyncClient 对象
  368. /// </summary>
  369. /// <param name="pClient"></param>
  370. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  371. public static extern void Destroy_HP_HttpSyncClient(IntPtr pClient);
  372. /// <summary>
  373. /// 创建 HP_HttpServerListener 对象
  374. /// </summary>
  375. /// <returns></returns>
  376. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  377. public static extern IntPtr Create_HP_HttpServerListener();
  378. /// <summary>
  379. /// 创建 HP_HttpAgentListener 对象
  380. /// </summary>
  381. /// <returns></returns>
  382. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  383. public static extern IntPtr Create_HP_HttpAgentListener();
  384. /// <summary>
  385. /// 创建 HP_HttpClientListener 对象
  386. /// </summary>
  387. /// <returns></returns>
  388. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  389. public static extern IntPtr Create_HP_HttpClientListener();
  390. /// <summary>
  391. /// 销毁 HP_HttpServerListener 对象
  392. /// </summary>
  393. /// <param name="pListener"></param>
  394. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  395. public static extern void Destroy_HP_HttpServerListener(IntPtr pListener);
  396. /// <summary>
  397. /// 销毁 HP_HttpAgentListener 对象
  398. /// </summary>
  399. /// <param name="pListener"></param>
  400. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  401. public static extern void Destroy_HP_HttpAgentListener(IntPtr pListener);
  402. /// <summary>
  403. /// 销毁 HP_HttpClientListener 对象
  404. /// </summary>
  405. /// <param name="pListener"></param>
  406. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  407. public static extern void Destroy_HP_HttpClientListener(IntPtr pListener);
  408. /**********************************************************************************/
  409. /*************************** HTTP Server 回调函数设置方法 **************************/
  410. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  411. public static extern void HP_Set_FN_HttpServer_OnMessageBegin(IntPtr pListener, OnMessageBegin fn);
  412. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  413. public static extern void HP_Set_FN_HttpServer_OnRequestLine(IntPtr pListener, OnRequestLine fn);
  414. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  415. public static extern void HP_Set_FN_HttpServer_OnHeader(IntPtr pListener, OnHeader fn);
  416. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  417. public static extern void HP_Set_FN_HttpServer_OnHeadersComplete(IntPtr pListener, OnHeadersComplete fn);
  418. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  419. public static extern void HP_Set_FN_HttpServer_OnBody(IntPtr pListener, OnBody fn);
  420. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  421. public static extern void HP_Set_FN_HttpServer_OnChunkHeader(IntPtr pListener, OnChunkHeader fn);
  422. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  423. public static extern void HP_Set_FN_HttpServer_OnChunkComplete(IntPtr pListener, OnChunkComplete fn);
  424. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  425. public static extern void HP_Set_FN_HttpServer_OnMessageComplete(IntPtr pListener, OnMessageComplete fn);
  426. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  427. public static extern void HP_Set_FN_HttpServer_OnUpgrade(IntPtr pListener, OnUpgrade fn);
  428. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  429. public static extern void HP_Set_FN_HttpServer_OnParseError(IntPtr pListener, OnParseError fn);
  430. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  431. public static extern void HP_Set_FN_HttpServer_OnWSMessageHeader(IntPtr pListener, OnWSMessageHeader fn);
  432. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  433. public static extern void HP_Set_FN_HttpServer_OnWSMessageBody(IntPtr pListener, OnWSMessageBody fn);
  434. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  435. public static extern void HP_Set_FN_HttpServer_OnWSMessageComplete(IntPtr pListener, OnWSMessageComplete fn);
  436. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  437. public static extern void HP_Set_FN_HttpServer_OnPrepareListen(IntPtr pListener, Sdk.OnPrepareListen fn);
  438. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  439. public static extern void HP_Set_FN_HttpServer_OnAccept(IntPtr pListener, Sdk.OnAccept fn);
  440. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  441. public static extern void HP_Set_FN_HttpServer_OnHandShake(IntPtr pListener, Sdk.OnHandShake fn);
  442. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  443. public static extern void HP_Set_FN_HttpServer_OnReceive(IntPtr pListener, Sdk.OnReceive fn);
  444. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  445. public static extern void HP_Set_FN_HttpServer_OnSend(IntPtr pListener, Sdk.OnSend fn);
  446. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  447. public static extern void HP_Set_FN_HttpServer_OnClose(IntPtr pListener, Sdk.OnClose fn);
  448. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  449. public static extern void HP_Set_FN_HttpServer_OnShutdown(IntPtr pListener, Sdk.OnShutdown fn);
  450. /**********************************************************************************/
  451. /**************************** HTTP Agent 回调函数设置方法 **************************/
  452. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  453. public static extern void HP_Set_FN_HttpAgent_OnMessageBegin(IntPtr pListener, OnMessageBegin fn);
  454. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  455. public static extern void HP_Set_FN_HttpAgent_OnStatusLine(IntPtr pListener, OnStatusLine fn);
  456. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  457. public static extern void HP_Set_FN_HttpAgent_OnHeader(IntPtr pListener, OnHeader fn);
  458. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  459. public static extern void HP_Set_FN_HttpAgent_OnHeadersComplete(IntPtr pListener, OnHeadersComplete fn);
  460. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  461. public static extern void HP_Set_FN_HttpAgent_OnBody(IntPtr pListener, OnBody fn);
  462. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  463. public static extern void HP_Set_FN_HttpAgent_OnChunkHeader(IntPtr pListener, OnChunkHeader fn);
  464. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  465. public static extern void HP_Set_FN_HttpAgent_OnChunkComplete(IntPtr pListener, OnChunkComplete fn);
  466. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  467. public static extern void HP_Set_FN_HttpAgent_OnMessageComplete(IntPtr pListener, OnMessageComplete fn);
  468. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  469. public static extern void HP_Set_FN_HttpAgent_OnUpgrade(IntPtr pListener, OnUpgrade fn);
  470. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  471. public static extern void HP_Set_FN_HttpAgent_OnParseError(IntPtr pListener, OnParseError fn);
  472. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  473. public static extern void HP_Set_FN_HttpAgent_OnWSMessageHeader(IntPtr pListener, OnWSMessageHeader fn);
  474. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  475. public static extern void HP_Set_FN_HttpAgent_OnWSMessageBody(IntPtr pListener, OnWSMessageBody fn);
  476. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  477. public static extern void HP_Set_FN_HttpAgent_OnWSMessageComplete(IntPtr pListener, OnWSMessageComplete fn);
  478. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  479. public static extern void HP_Set_FN_HttpAgent_OnPrepareConnect(IntPtr pListener, Sdk.OnPrepareConnect fn);
  480. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  481. public static extern void HP_Set_FN_HttpAgent_OnConnect(IntPtr pListener, Sdk.OnConnect fn);
  482. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  483. public static extern void HP_Set_FN_HttpAgent_OnHandShake(IntPtr pListener, Sdk.OnHandShake fn);
  484. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  485. public static extern void HP_Set_FN_HttpAgent_OnReceive(IntPtr pListener, Sdk.OnReceive fn);
  486. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  487. public static extern void HP_Set_FN_HttpAgent_OnSend(IntPtr pListener, Sdk.OnSend fn);
  488. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  489. public static extern void HP_Set_FN_HttpAgent_OnClose(IntPtr pListener, Sdk.OnClose fn);
  490. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  491. public static extern void HP_Set_FN_HttpAgent_OnShutdown(IntPtr pListener, Sdk.OnShutdown fn);
  492. /**********************************************************************************/
  493. /*************************** HTTP Client 回调函数设置方法 **************************/
  494. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  495. public static extern void HP_Set_FN_HttpClient_OnMessageBegin(IntPtr pListener, OnMessageBegin fn);
  496. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  497. public static extern void HP_Set_FN_HttpClient_OnStatusLine(IntPtr pListener, OnStatusLine fn);
  498. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  499. public static extern void HP_Set_FN_HttpClient_OnHeader(IntPtr pListener, OnHeader fn);
  500. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  501. public static extern void HP_Set_FN_HttpClient_OnHeadersComplete(IntPtr pListener, OnHeadersComplete fn);
  502. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  503. public static extern void HP_Set_FN_HttpClient_OnBody(IntPtr pListener, OnBody fn);
  504. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  505. public static extern void HP_Set_FN_HttpClient_OnChunkHeader(IntPtr pListener, OnChunkHeader fn);
  506. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  507. public static extern void HP_Set_FN_HttpClient_OnChunkComplete(IntPtr pListener, OnChunkComplete fn);
  508. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  509. public static extern void HP_Set_FN_HttpClient_OnMessageComplete(IntPtr pListener, OnMessageComplete fn);
  510. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  511. public static extern void HP_Set_FN_HttpClient_OnUpgrade(IntPtr pListener, OnUpgrade fn);
  512. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  513. public static extern void HP_Set_FN_HttpClient_OnParseError(IntPtr pListener, OnParseError fn);
  514. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  515. public static extern void HP_Set_FN_HttpClient_OnWSMessageHeader(IntPtr pListener, OnWSMessageHeader fn);
  516. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  517. public static extern void HP_Set_FN_HttpClient_OnWSMessageBody(IntPtr pListener, OnWSMessageBody fn);
  518. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  519. public static extern void HP_Set_FN_HttpClient_OnWSMessageComplete(IntPtr pListener, OnWSMessageComplete fn);
  520. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  521. public static extern void HP_Set_FN_HttpClient_OnPrepareConnect(IntPtr pListener, Sdk.OnPrepareConnect fn);
  522. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  523. public static extern void HP_Set_FN_HttpClient_OnConnect(IntPtr pListener, Sdk.OnConnect fn);
  524. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  525. public static extern void HP_Set_FN_HttpClient_OnHandShake(IntPtr pListener, Sdk.OnHandShake fn);
  526. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  527. public static extern void HP_Set_FN_HttpClient_OnReceive(IntPtr pListener, Sdk.OnReceive fn);
  528. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  529. public static extern void HP_Set_FN_HttpClient_OnSend(IntPtr pListener, Sdk.OnSend fn);
  530. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  531. public static extern void HP_Set_FN_HttpClient_OnClose(IntPtr pListener, Sdk.OnClose fn);
  532. /**************************************************************************/
  533. /***************************** Server 操作方法 *****************************/
  534. /*
  535. * 名称:回复请求
  536. * 描述:向客户端回复 HTTP 请求
  537. *
  538. * 参数: dwConnID -- 连接 ID
  539. * usStatusCode -- HTTP 状态码
  540. * lpszDesc -- HTTP 状态描述
  541. * lpHeaders -- 回复请求头
  542. * iHeaderCount -- 回复请求头数量
  543. * pData -- 回复请求体
  544. * iLength -- 回复请求体长度
  545. * 返回值: TRUE -- 成功
  546. * FALSE -- 失败
  547. */
  548. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  549. public static extern bool HP_HttpServer_SendResponse(IntPtr pServer, IntPtr dwConnID, ushort usStatusCode, string lpszDesc, THeader[] lpHeaders, int iHeaderCount, byte[] pData, int iLength);
  550. /*
  551. /*
  552. * 名称:发送本地文件
  553. * 描述:向指定连接发送 4096 KB 以下的小文件
  554. *
  555. * 参数: dwConnID -- 连接 ID
  556. * lpszFileName -- 文件路径
  557. * usStatusCode -- HTTP 状态码
  558. * lpszDesc -- HTTP 状态描述
  559. * lpHeaders -- 回复请求头
  560. * iHeaderCount -- 回复请求头数量
  561. * 返回值: TRUE -- 成功
  562. * FALSE -- 失败
  563. */
  564. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  565. public static extern bool HP_HttpServer_SendLocalFile(IntPtr pServer, IntPtr dwConnID, string lpszFileName, ushort usStatusCode, string lpszDesc, THeader[] lpHeaders, int iHeaderCount);
  566. /*
  567. * 名称:发送 WebSocket 消息
  568. * 描述:向对端端发送 WebSocket 消息
  569. *
  570. * 参数: dwConnID -- 连接 ID
  571. * bFinal -- 是否结束帧
  572. * iReserved -- RSV1/RSV2/RSV3 各 1 位
  573. * iOperationCode -- 操作码:0x0 - 0xF
  574. * lpszMask -- 掩码(nullptr 或 4 字节掩码,如果为 nullptr 则没有掩码)
  575. * pData -- 消息体数据缓冲区
  576. * iLength -- 消息体数据长度
  577. * ullBodyLen -- 消息总长度
  578. * ullBodyLen = 0 -> 消息总长度为 iLength
  579. * ullBodyLen = iLength -> 消息总长度为 ullBodyLen
  580. * ullBodyLen > iLength -> 消息总长度为 ullBodyLen,后续消息体长度为 ullBOdyLen - iLength,后续消息体通过底层方法 Send() / SendPackets() 发送
  581. * ullBodyLen < iLength -> 错误参数,发送失败
  582. * 返回值: TRUE -- 成功
  583. * FALSE -- 失败
  584. */
  585. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  586. public static extern bool HP_HttpServer_SendWSMessage(IntPtr pServer, IntPtr dwConnID, bool bFinal, WSReserved iReserved, WSOpcode iOperationCode, byte[] lpszMask, IntPtr pData, int iLength, ulong ullBodyLen);
  587. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  588. public static extern bool HP_HttpServer_SendWSMessage(IntPtr pServer, IntPtr dwConnID, bool bFinal, WSReserved iReserved, WSOpcode iOperationCode, byte[] lpszMask, byte[] pdata, int iLength, ulong ullBodyLen);
  589. /*
  590. * 名称:释放连接
  591. * 描述:把连接放入释放队列,等待某个时间(通过 SetReleaseDelay() 设置)关闭连接
  592. *
  593. * 参数: dwConnID -- 连接 ID
  594. * 返回值: TRUE -- 成功
  595. * FALSE -- 失败
  596. */
  597. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  598. public static extern bool HP_HttpServer_Release(IntPtr pServer, IntPtr dwConnID);
  599. /*
  600. * 名称:启动 HTTP 通信
  601. * 描述:当通信组件设置为非自动启动 HTTP 通信时,需要调用本方法启动 HTTP 通信
  602. *
  603. * 返回值: TRUE -- 成功
  604. * FALSE -- 失败,可通过 SYS_GetLastError() 获取失败原因
  605. */
  606. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  607. public static extern bool HP_HttpServer_StartHttp(IntPtr pServer, IntPtr dwConnID);
  608. /******************************************************************************/
  609. /***************************** Server 属性访问方法 *****************************/
  610. /* 设置连接释放延时(默认:3000 毫秒) */
  611. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  612. public static extern void HP_HttpServer_SetReleaseDelay(IntPtr pServer, uint dwReleaseDelay);
  613. /* 获取连接释放延时 */
  614. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  615. public static extern uint HP_HttpServer_GetReleaseDelay(IntPtr pServer);
  616. /* 获取请求行 URL 域掩码(URL 域参考:EnHttpUrlField) */
  617. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  618. public static extern ushort HP_HttpServer_GetUrlFieldSet(IntPtr pServer, IntPtr dwConnID);
  619. /* 获取某个 URL 域值 */
  620. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  621. public static extern IntPtr HP_HttpServer_GetUrlField(IntPtr pServer, IntPtr dwConnID, HttpUrlField enField);
  622. /* 获取请求方法 */
  623. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  624. public static extern IntPtr HP_HttpServer_GetMethod(IntPtr pServer, IntPtr dwConnID);
  625. /* 设置本地协议版本 */
  626. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  627. public static extern void HP_HttpServer_SetLocalVersion(IntPtr pServer, HttpVersion usVersion);
  628. /* 获取本地协议版本 */
  629. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  630. public static extern HttpVersion HP_HttpServer_GetLocalVersion(IntPtr pServer);
  631. /* 检查是否升级协议 */
  632. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  633. public static extern bool HP_HttpServer_IsUpgrade(IntPtr pServer, IntPtr dwConnID);
  634. /* 检查是否有 Keep-Alive 标识 */
  635. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  636. public static extern bool HP_HttpServer_IsKeepAlive(IntPtr pServer, IntPtr dwConnID);
  637. /* 获取协议版本 */
  638. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  639. public static extern ushort HP_HttpServer_GetVersion(IntPtr pServer, IntPtr dwConnID);
  640. /* 获取主机 */
  641. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  642. public static extern IntPtr HP_HttpServer_GetHost(IntPtr pServer, IntPtr dwConnID);
  643. /* 获取内容长度 */
  644. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  645. public static extern long HP_HttpServer_GetContentLength(IntPtr pServer, IntPtr dwConnID);
  646. /* 获取内容类型 */
  647. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  648. public static extern IntPtr HP_HttpServer_GetContentType(IntPtr pServer, IntPtr dwConnID);
  649. /* 获取内容编码 */
  650. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  651. public static extern IntPtr HP_HttpServer_GetContentEncoding(IntPtr pServer, IntPtr dwConnID);
  652. /* 获取传输编码 */
  653. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  654. public static extern IntPtr HP_HttpServer_GetTransferEncoding(IntPtr pServer, IntPtr dwConnID);
  655. /* 获取协议升级类型 */
  656. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  657. public static extern HttpUpgradeType HP_HttpServer_GetUpgradeType(IntPtr pServer, IntPtr dwConnID);
  658. /* 获取解析错误代码 */
  659. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  660. public static extern ushort HP_HttpServer_GetParseErrorCode(IntPtr pServer, IntPtr dwConnID, ref IntPtr lpszErrorDesc);
  661. /* 获取某个请求头(单值) */
  662. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  663. public static extern bool HP_HttpServer_GetHeader(IntPtr pServer, IntPtr dwConnID, string lpszName, ref IntPtr lpszValue);
  664. /* 获取某个请求头(多值) */
  665. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  666. public static extern bool HP_HttpServer_GetHeaders(IntPtr pServer, IntPtr dwConnID, string lpszName, IntPtr[] lpszValue, ref uint pdwCount);
  667. /* 获取所有请求头 */
  668. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  669. public static extern bool HP_HttpServer_GetAllHeaders(IntPtr pServer, IntPtr dwConnID, IntPtr lpHeaders, ref uint pdwCount);
  670. /* 获取所有请求头名称 */
  671. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  672. public static extern bool HP_HttpServer_GetAllHeaderNames(IntPtr pServer, IntPtr dwConnID, IntPtr[] lpszName, ref uint pdwCount);
  673. /* 获取 Cookie */
  674. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  675. public static extern bool HP_HttpServer_GetCookie(IntPtr pServer, IntPtr dwConnID, string lpszName, ref IntPtr lpszValue);
  676. /* 获取所有 Cookie */
  677. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  678. public static extern bool HP_HttpServer_GetAllCookies(IntPtr pServer, IntPtr dwConnID, IntPtr lpCookies, ref uint pdwCount);
  679. /* 获取当前 WebSocket 消息状态,传入 nullptr 则不获取相应字段 */
  680. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  681. public static extern bool HP_HttpServer_GetWSMessageState(IntPtr pServer, IntPtr dwConnID, ref bool lpbFinal, ref WSReserved lpiReserved, ref WSOpcode lpiOperationCode, ref IntPtr lpszMask, ref ulong lpullBodyLen, ref ulong lpullBodyRemain);
  682. /* 设置 HTTP 启动方式(默认:TRUE,自动启动) */
  683. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  684. public static extern void HP_HttpServer_SetHttpAutoStart(IntPtr pServer, bool bAutoStart);
  685. /* 获取 HTTP 启动方式 */
  686. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  687. public static extern bool HP_HttpServer_IsHttpAutoStart(IntPtr pServer);
  688. /**************************************************************************/
  689. /***************************** Agent 操作方法 ******************************/
  690. /*
  691. * 名称:发送请求
  692. * 描述:向服务端发送 HTTP 请求
  693. *
  694. * 参数: dwConnID -- 连接 ID
  695. * lpszMethod -- 请求方法
  696. * lpszPath -- 请求路径
  697. * lpHeaders -- 请求头
  698. * iHeaderCount -- 请求头数量
  699. * pBody -- 请求体
  700. * iLength -- 请求体长度
  701. * 返回值: TRUE -- 成功
  702. * FALSE -- 失败
  703. */
  704. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  705. public static extern bool HP_HttpAgent_SendRequest(IntPtr pAgent, IntPtr dwConnID, string lpszMethod, string lpszPath, THeader[] lpHeaders, int iHeaderCount, byte[] pData, int iLength);
  706. /*
  707. * 名称:发送本地文件
  708. * 描述:向指定连接发送 4096 KB 以下的小文件
  709. *
  710. * 参数: dwConnID -- 连接 ID
  711. * lpszFileName -- 文件路径
  712. * lpszMethod -- 请求方法
  713. * lpszPath -- 请求路径
  714. * lpHeaders -- 请求头
  715. * iHeaderCount -- 请求头数量
  716. * 返回值: TRUE -- 成功
  717. * FALSE -- 失败
  718. */
  719. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  720. public static extern bool HP_HttpAgent_SendLocalFile(IntPtr pAgent, IntPtr dwConnID, string lpszFileName, string lpszMethod, string lpszPath, THeader[] lpHeaders, int iHeaderCount);
  721. /* 发送 POST 请求 */
  722. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  723. public static extern bool HP_HttpAgent_SendPost(IntPtr pAgent, IntPtr dwConnID, string lpszPath, THeader[] lpHeaders, int iHeaderCount, string pBody, int iLength);
  724. /* 发送 PUT 请求 */
  725. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  726. public static extern bool HP_HttpAgent_SendPut(IntPtr pAgent, IntPtr dwConnID, string lpszPath, THeader[] lpHeaders, int iHeaderCount, string pBody, int iLength);
  727. /* 发送 PATCH 请求 */
  728. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  729. public static extern bool HP_HttpAgent_SendPatch(IntPtr pAgent, IntPtr dwConnID, string lpszPath, THeader[] lpHeaders, int iHeaderCount, string pBody, int iLength);
  730. /* 发送 GET 请求 */
  731. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  732. public static extern bool HP_HttpAgent_SendGet(IntPtr pAgent, IntPtr dwConnID, string lpszPath, THeader[] lpHeaders, int iHeaderCount);
  733. /* 发送 DELETE 请求 */
  734. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  735. public static extern bool HP_HttpAgent_SendDelete(IntPtr pAgent, IntPtr dwConnID, string lpszPath, THeader[] lpHeaders, int iHeaderCount);
  736. /* 发送 HEAD 请求 */
  737. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  738. public static extern bool HP_HttpAgent_SendHead(IntPtr pAgent, IntPtr dwConnID, string lpszPath, THeader[] lpHeaders, int iHeaderCount);
  739. /* 发送 TRACE 请求 */
  740. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  741. public static extern bool HP_HttpAgent_SendTrace(IntPtr pAgent, IntPtr dwConnID, string lpszPath, THeader[] lpHeaders, int iHeaderCount);
  742. /* 发送 OPTIONS 请求 */
  743. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  744. public static extern bool HP_HttpAgent_SendOptions(IntPtr pAgent, IntPtr dwConnID, string lpszPath, THeader[] lpHeaders, int iHeaderCount);
  745. /* 发送 CONNECT 请求 */
  746. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  747. public static extern bool HP_HttpAgent_SendConnect(IntPtr pAgent, IntPtr dwConnID, string lpszHost, THeader[] lpHeaders, int iHeaderCount);
  748. /*
  749. * 名称:发送 WebSocket 消息
  750. * 描述:向对端端发送 WebSocket 消息
  751. *
  752. * 参数: dwConnID -- 连接 ID
  753. * bFinal -- 是否结束帧
  754. * iReserved -- RSV1/RSV2/RSV3 各 1 位
  755. * iOperationCode -- 操作码:0x0 - 0xF
  756. * lpszMask -- 掩码(nullptr 或 4 字节掩码,如果为 nullptr 则没有掩码)
  757. * pData -- 消息体数据缓冲区
  758. * iLength -- 消息体数据长度
  759. * ullBodyLen -- 消息总长度
  760. * ullBodyLen = 0 -> 消息总长度为 iLength
  761. * ullBodyLen = iLength -> 消息总长度为 ullBodyLen
  762. * ullBodyLen > iLength -> 消息总长度为 ullBodyLen,后续消息体长度为 ullBOdyLen - iLength,后续消息体通过底层方法 Send() / SendPackets() 发送
  763. * ullBodyLen < iLength -> 错误参数,发送失败
  764. * 返回值: TRUE -- 成功
  765. * FALSE -- 失败
  766. */
  767. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  768. public static extern bool HP_HttpAgent_SendWSMessage(IntPtr pAgent, IntPtr dwConnID, bool bFinal, byte iReserved, byte iOperationCode, byte[] lpszMask, byte[] pData, int iLength, ulong ullBodyLen);
  769. /*
  770. * 名称:启动 HTTP 通信
  771. * 描述:当通信组件设置为非自动启动 HTTP 通信时,需要调用本方法启动 HTTP 通信
  772. *
  773. * 返回值: TRUE -- 成功
  774. * FALSE -- 失败,可通过 SYS_GetLastError() 获取失败原因
  775. */
  776. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  777. public static extern bool HP_HttpAgent_StartHttp(IntPtr pAgent, IntPtr dwConnID);
  778. /******************************************************************************/
  779. /***************************** Agent 属性访问方法 ******************************/
  780. /* 获取 HTTP 状态码 */
  781. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  782. public static extern ushort HP_HttpAgent_GetStatusCode(IntPtr pAgent, IntPtr dwConnID);
  783. /* 设置本地协议版本 */
  784. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  785. public static extern void HP_HttpAgent_SetLocalVersion(IntPtr pAgent, HttpVersion usVersion);
  786. /* 获取本地协议版本 */
  787. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  788. public static extern HttpVersion HP_HttpAgent_GetLocalVersion(IntPtr pAgent);
  789. /* 检查是否升级协议 */
  790. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  791. public static extern bool HP_HttpAgent_IsUpgrade(IntPtr pAgent, IntPtr dwConnID);
  792. /* 检查是否有 Keep-Alive 标识 */
  793. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  794. public static extern bool HP_HttpAgent_IsKeepAlive(IntPtr pAgent, IntPtr dwConnID);
  795. /* 获取协议版本 */
  796. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  797. public static extern ushort HP_HttpAgent_GetVersion(IntPtr pAgent, IntPtr dwConnID);
  798. /* 获取内容长度 */
  799. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  800. public static extern long HP_HttpAgent_GetContentLength(IntPtr pAgent, IntPtr dwConnID);
  801. /* 获取内容类型 */
  802. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  803. public static extern IntPtr HP_HttpAgent_GetContentType(IntPtr pAgent, IntPtr dwConnID);
  804. /* 获取内容编码 */
  805. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  806. public static extern IntPtr HP_HttpAgent_GetContentEncoding(IntPtr pAgent, IntPtr dwConnID);
  807. /* 获取传输编码 */
  808. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  809. public static extern IntPtr HP_HttpAgent_GetTransferEncoding(IntPtr pAgent, IntPtr dwConnID);
  810. /* 获取协议升级类型 */
  811. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  812. public static extern HttpUpgradeType HP_HttpAgent_GetUpgradeType(IntPtr pAgent, IntPtr dwConnID);
  813. /* 获取解析错误代码 */
  814. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  815. public static extern ushort HP_HttpAgent_GetParseErrorCode(IntPtr pAgent, IntPtr dwConnID, ref IntPtr lpszErrorDesc);
  816. /* 获取某个请求头(单值) */
  817. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  818. public static extern bool HP_HttpAgent_GetHeader(IntPtr pAgent, IntPtr dwConnID, string lpszName, ref IntPtr lpszValue);
  819. /* 获取某个请求头(多值) */
  820. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  821. public static extern bool HP_HttpAgent_GetHeaders(IntPtr pAgent, IntPtr dwConnID, string lpszName, IntPtr[] lpszValue, ref uint pdwCount);
  822. /* 获取所有请求头 */
  823. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  824. public static extern bool HP_HttpAgent_GetAllHeaders(IntPtr pAgent, IntPtr dwConnID, IntPtr lpHeaders, ref uint pdwCount);
  825. /* 获取所有请求头名称 */
  826. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  827. public static extern bool HP_HttpAgent_GetAllHeaderNames(IntPtr pAgent, IntPtr dwConnID, IntPtr[] lpszName, ref uint pdwCount);
  828. /* 设置是否使用 Cookie */
  829. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  830. public static extern void HP_HttpAgent_SetUseCookie(IntPtr pAgent, bool bUseCookie);
  831. /* 检查是否使用 Cookie */
  832. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  833. public static extern bool HP_HttpAgent_IsUseCookie(IntPtr pAgent);
  834. /* 获取 Cookie */
  835. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  836. public static extern bool HP_HttpAgent_GetCookie(IntPtr pAgent, IntPtr dwConnID, string lpszName, ref IntPtr lpszValue);
  837. /* 获取所有 Cookie */
  838. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  839. public static extern bool HP_HttpAgent_GetAllCookies(IntPtr pAgent, IntPtr dwConnID, IntPtr lpCookies, ref uint pdwCount);
  840. /* 获取当前 WebSocket 消息状态,传入 nullptr 则不获取相应字段 */
  841. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  842. public static extern bool HP_HttpAgent_GetWSMessageState(IntPtr pAgent, IntPtr dwConnID, ref bool lpbFinal, ref byte lpiReserved, ref byte lpiOperationCode, ref IntPtr lpszMask, ref ulong lpullBodyLen, ref ulong lpullBodyRemain);
  843. /* 设置 HTTP 启动方式(默认:TRUE,自动启动) */
  844. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  845. public static extern void HP_HttpAgent_SetHttpAutoStart(IntPtr pAgent, bool bAutoStart);
  846. /* 获取 HTTP 启动方式 */
  847. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  848. public static extern bool HP_HttpAgent_IsHttpAutoStart(IntPtr pAgent);
  849. /**************************************************************************/
  850. /***************************** Client 操作方法 *****************************/
  851. /*
  852. * 名称:发送请求
  853. * 描述:向服务端发送 HTTP 请求
  854. *
  855. * 参数: lpszMethod -- 请求方法
  856. * lpszPath -- 请求路径
  857. * lpHeaders -- 请求头
  858. * iHeaderCount -- 请求头数量
  859. * pBody -- 请求体
  860. * iLength -- 请求体长度
  861. * 返回值: TRUE -- 成功
  862. * FALSE -- 失败
  863. */
  864. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  865. public static extern bool HP_HttpClient_SendRequest(IntPtr pClient, string lpszMethod, string lpszPath, THeader[] lpHeaders, int iHeaderCount, byte[] pBody, int iLength);
  866. /*
  867. * 名称:发送本地文件
  868. * 描述:向指定连接发送 4096 KB 以下的小文件
  869. *
  870. * 参数: dwConnID -- 连接 ID
  871. * lpszFileName -- 文件路径
  872. * lpszMethod -- 请求方法
  873. * lpszPath -- 请求路径
  874. * lpHeaders -- 请求头
  875. * iHeaderCount -- 请求头数量
  876. * 返回值: TRUE -- 成功
  877. * FALSE -- 失败
  878. */
  879. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  880. public static extern bool HP_HttpClient_SendLocalFile(IntPtr pClient, string lpszFileName, string lpszMethod, string lpszPath, THeader[] lpHeaders, int iHeaderCount);
  881. /* 发送 POST 请求 */
  882. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  883. public static extern bool HP_HttpClient_SendPost(IntPtr pClient, string lpszPath, THeader[] lpHeaders, int iHeaderCount, string pBody, int iLength);
  884. /* 发送 PUT 请求 */
  885. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  886. public static extern bool HP_HttpClient_SendPut(IntPtr pClient, string lpszPath, THeader[] lpHeaders, int iHeaderCount, string pBody, int iLength);
  887. /* 发送 PATCH 请求 */
  888. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  889. public static extern bool HP_HttpClient_SendPatch(IntPtr pClient, string lpszPath, THeader[] lpHeaders, int iHeaderCount, string pBody, int iLength);
  890. /* 发送 GET 请求 */
  891. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  892. public static extern bool HP_HttpClient_SendGet(IntPtr pClient, string lpszPath, THeader[] lpHeaders, int iHeaderCount);
  893. /* 发送 DELETE 请求 */
  894. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  895. public static extern bool HP_HttpClient_SendDelete(IntPtr pClient, string lpszPath, THeader[] lpHeaders, int iHeaderCount);
  896. /* 发送 HEAD 请求 */
  897. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  898. public static extern bool HP_HttpClient_SendHead(IntPtr pClient, string lpszPath, THeader[] lpHeaders, int iHeaderCount);
  899. /* 发送 TRACE 请求 */
  900. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  901. public static extern bool HP_HttpClient_SendTrace(IntPtr pClient, string lpszPath, THeader[] lpHeaders, int iHeaderCount);
  902. /* 发送 OPTIONS 请求 */
  903. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  904. public static extern bool HP_HttpClient_SendOptions(IntPtr pClient, string lpszPath, THeader[] lpHeaders, int iHeaderCount);
  905. /* 发送 CONNECT 请求 */
  906. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  907. public static extern bool HP_HttpClient_SendConnect(IntPtr pClient, string lpszHost, THeader[] lpHeaders, int iHeaderCount);
  908. /*
  909. * 名称:发送 WebSocket 消息
  910. * 描述:向对端端发送 WebSocket 消息
  911. *
  912. * 参数: bFinal -- 是否结束帧
  913. * iReserved -- RSV1/RSV2/RSV3 各 1 位
  914. * iOperationCode -- 操作码:0x0 - 0xF
  915. * lpszMask -- 掩码(nullptr 或 4 字节掩码,如果为 nullptr 则没有掩码)
  916. * pData -- 消息体数据缓冲区
  917. * iLength -- 消息体数据长度
  918. * ullBodyLen -- 消息总长度
  919. * ullBodyLen = 0 -> 消息总长度为 iLength
  920. * ullBodyLen = iLength -> 消息总长度为 ullBodyLen
  921. * ullBodyLen > iLength -> 消息总长度为 ullBodyLen,后续消息体长度为 ullBOdyLen - iLength,后续消息体通过底层方法 Send() / SendPackets() 发送
  922. * ullBodyLen < iLength -> 错误参数,发送失败
  923. * 返回值: TRUE -- 成功
  924. * FALSE -- 失败
  925. */
  926. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  927. public static extern bool HP_HttpClient_SendWSMessage(IntPtr pClient, bool bFinal, byte iReserved, byte iOperationCode, byte[] lpszMask, byte[] pData, int iLength, ulong ullBodyLen);
  928. /*
  929. * 名称:启动 HTTP 通信
  930. * 描述:当通信组件设置为非自动启动 HTTP 通信时,需要调用本方法启动 HTTP 通信
  931. *
  932. * 返回值: TRUE -- 成功
  933. * FALSE -- 失败,可通过 SYS_GetLastError() 获取失败原因
  934. */
  935. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  936. public static extern bool HP_HttpClient_StartHttp(IntPtr pClient);
  937. /******************************************************************************/
  938. /***************************** Client 属性访问方法 *****************************/
  939. /* 获取 HTTP 状态码 */
  940. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  941. public static extern ushort HP_HttpClient_GetStatusCode(IntPtr pClient);
  942. /* 设置本地协议版本 */
  943. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  944. public static extern void HP_HttpClient_SetLocalVersion(IntPtr pClient, HttpVersion usVersion);
  945. /* 获取本地协议版本 */
  946. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  947. public static extern HttpVersion HP_HttpClient_GetLocalVersion(IntPtr pClient);
  948. /* 检查是否升级协议 */
  949. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  950. public static extern bool HP_HttpClient_IsUpgrade(IntPtr pClient);
  951. /* 检查是否有 Keep-Alive 标识 */
  952. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  953. public static extern bool HP_HttpClient_IsKeepAlive(IntPtr pClient);
  954. /* 获取协议版本 */
  955. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  956. public static extern ushort HP_HttpClient_GetVersion(IntPtr pClient);
  957. /* 获取内容长度 */
  958. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  959. public static extern long HP_HttpClient_GetContentLength(IntPtr pClient);
  960. /* 获取内容类型 */
  961. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  962. public static extern IntPtr HP_HttpClient_GetContentType(IntPtr pClient);
  963. /* 获取内容编码 */
  964. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  965. public static extern IntPtr HP_HttpClient_GetContentEncoding(IntPtr pClient);
  966. /* 获取传输编码 */
  967. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  968. public static extern IntPtr HP_HttpClient_GetTransferEncoding(IntPtr pClient);
  969. /* 获取协议升级类型 */
  970. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  971. public static extern HttpUpgradeType HP_HttpClient_GetUpgradeType(IntPtr pClient);
  972. /* 获取解析错误代码 */
  973. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  974. public static extern ushort HP_HttpClient_GetParseErrorCode(IntPtr pClient, ref IntPtr lpszErrorDesc);
  975. /* 获取某个请求头(单值) */
  976. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  977. public static extern bool HP_HttpClient_GetHeader(IntPtr pClient, string lpszName, ref IntPtr lpszValue);
  978. /* 获取某个请求头(多值) */
  979. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  980. public static extern bool HP_HttpClient_GetHeaders(IntPtr pClient, string lpszName, IntPtr[] lpszValue, ref uint pdwCount);
  981. /* 获取所有请求头 */
  982. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  983. public static extern bool HP_HttpClient_GetAllHeaders(IntPtr pClient, IntPtr lpHeaders, ref uint pdwCount);
  984. /* 获取所有请求头名称 */
  985. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  986. public static extern bool HP_HttpClient_GetAllHeaderNames(IntPtr pClient, IntPtr[] lpszName, ref uint pdwCount);
  987. /* 设置是否使用 Cookie */
  988. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  989. public static extern void HP_HttpClient_SetUseCookie(IntPtr pClient, bool bUseCookie);
  990. /* 检查是否使用 Cookie */
  991. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  992. public static extern bool HP_HttpClient_IsUseCookie(IntPtr pClient);
  993. /* 获取 Cookie */
  994. /* 获取 Cookie */
  995. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  996. public static extern bool HP_HttpClient_GetCookie(IntPtr pClient, string lpszName, ref IntPtr lpszValue);
  997. /* 获取所有 Cookie */
  998. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  999. public static extern bool HP_HttpClient_GetAllCookies(IntPtr pClient, IntPtr lpCookies, ref uint pdwCount);
  1000. /* 获取当前 WebSocket 消息状态,传入 nullptr 则不获取相应字段 */
  1001. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  1002. public static extern bool HP_HttpClient_GetWSMessageState(IntPtr pClient, ref bool lpbFinal, ref byte lpiReserved, ref byte lpiOperationCode, ref IntPtr lpszMask, ref ulong lpullBodyLen, ref ulong lpullBodyRemain);
  1003. /* 设置 HTTP 启动方式(默认:TRUE,自动启动) */
  1004. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  1005. public static extern void HP_HttpClient_SetHttpAutoStart(IntPtr pClient, bool bAutoStart);
  1006. /* 获取 HTTP 启动方式 */
  1007. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  1008. public static extern bool HP_HttpClient_IsHttpAutoStart(IntPtr pClient);
  1009. /**************************************************************************/
  1010. /************************ HTTP Sync Client 操作方法 ************************/
  1011. /// <summary>
  1012. /// 名称:发送 URL 请求
  1013. /// 描述:向服务端发送 HTTP URL 请求
  1014. /// </summary>
  1015. /// <param name="pClient"></param>
  1016. /// <param name="lpszMethod">请求方法</param>
  1017. /// <param name="lpszUrl">请求 URL</param>
  1018. /// <param name="lpHeaders">请求头</param>
  1019. /// <param name="iHeaderCount">请求头数量</param>
  1020. /// <param name="pBody">请求体</param>
  1021. /// <param name="iLength">请求体长度</param>
  1022. /// <param name="bForceReconnect">强制重新连接(默认:FALSE,当请求 URL 的主机和端口与现有连接一致时,重用现有连接)</param>
  1023. /// <returns></returns>
  1024. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  1025. public static extern bool HP_HttpSyncClient_OpenUrl(IntPtr pClient, string lpszMethod, string lpszUrl, THeader[] lpHeaders, int iHeaderCount, string pData, int iLength, bool bForceReconnect);
  1026. /// <summary>
  1027. /// 名称:清除请求结果
  1028. /// 描述:清除上一次请求的响应头和响应体等结果信息(该方法会在每次发送请求前自动调用)
  1029. /// </summary>
  1030. /// <param name="pClient"></param>
  1031. /// <returns></returns>
  1032. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  1033. public static extern bool HP_HttpSyncClient_CleanupRequestResult(IntPtr pClient);
  1034. /******************************************************************************/
  1035. /************************ HTTP Sync Client 属性访问方法 ************************/
  1036. /// <summary>
  1037. /// 设置连接超时(毫秒,0:系统默认超时,默认:5000)
  1038. /// </summary>
  1039. /// <param name="pClient"></param>
  1040. /// <param name="dwConnectTimeout"></param>
  1041. /// <returns></returns>
  1042. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  1043. public static extern void HP_HttpSyncClient_SetConnectTimeout(IntPtr pClient, uint dwConnectTimeout);
  1044. /// <summary>
  1045. /// 设置请求超时(毫秒,0:无限等待,默认:10000)
  1046. /// </summary>
  1047. /// <param name="pClient"></param>
  1048. /// <param name="dwConnectTimeout"></param>
  1049. /// <returns></returns>
  1050. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  1051. public static extern void HP_HttpSyncClient_SetRequestTimeout(IntPtr pClient, uint dwRequestTimeout);
  1052. /// <summary>
  1053. /// 获取连接超时
  1054. /// </summary>
  1055. /// <param name="pClient"></param>
  1056. /// <param name="dwConnectTimeout"></param>
  1057. /// <returns></returns>
  1058. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  1059. public static extern uint HP_HttpSyncClient_GetConnectTimeout(IntPtr pClient);
  1060. /// <summary>
  1061. /// 获取请求超时
  1062. /// </summary>
  1063. /// <param name="pClient"></param>
  1064. /// <param name="dwConnectTimeout"></param>
  1065. /// <returns></returns>
  1066. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  1067. public static extern uint HP_HttpSyncClient_GetRequestTimeout(IntPtr pClient);
  1068. /// <summary>
  1069. /// 获取响应体
  1070. /// </summary>
  1071. /// <param name="pClient"></param>
  1072. /// <param name="dwConnectTimeout"></param>
  1073. /// <returns></returns>
  1074. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  1075. public static extern uint HP_HttpSyncClient_GetResponseBody(IntPtr pClient, ref IntPtr lpszBody, ref int iLength);
  1076. /**************************************************************************/
  1077. /*************************** HTTP Cookie 管理方法 **************************/
  1078. /* 从文件加载 Cookie */
  1079. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  1080. public static extern bool HP_HttpCookie_MGR_LoadFromFile(string lpszFile, bool bKeepExists /*= TRUE*/);
  1081. /* 保存 Cookie 到文件 */
  1082. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  1083. public static extern bool HP_HttpCookie_MGR_SaveToFile(string lpszFile, bool bKeepExists /*= TRUE*/);
  1084. /* 清理 Cookie */
  1085. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  1086. public static extern bool HP_HttpCookie_MGR_ClearCookies(string lpszDomain /*= nullptr*/, string lpszPath /*= nullptr*/);
  1087. /* 清理过期 Cookie */
  1088. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  1089. public static extern bool HP_HttpCookie_MGR_RemoveExpiredCookies(string lpszDomain /*= nullptr*/, string lpszPath /*= nullptr*/);
  1090. /* 设置 Cookie */
  1091. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  1092. public static extern bool HP_HttpCookie_MGR_SetCookie(string lpszName, string lpszValue, string lpszDomain, string lpszPath, int iMaxAge /*= -1*/, bool bHttpOnly /*= FALSE*/, bool bSecure /*= FALSE*/, int enSameSite /*= 0*/, bool bOnlyUpdateValueIfExists /*= TRUE*/);
  1093. /* 删除 Cookie */
  1094. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  1095. public static extern bool HP_HttpCookie_MGR_DeleteCookie(string lpszDomain, string lpszPath, string lpszName);
  1096. /* 设置是否允许第三方 Cookie */
  1097. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  1098. public static extern void HP_HttpCookie_MGR_SetEnableThirdPartyCookie(bool bEnableThirdPartyCookie /*= TRUE*/);
  1099. /* 检查是否允许第三方 Cookie */
  1100. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  1101. public static extern bool HP_HttpCookie_MGR_IsEnableThirdPartyCookie();
  1102. /* Cookie expires 字符串转换为整数 */
  1103. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  1104. public static extern bool HP_HttpCookie_HLP_ParseExpires(string lpszExpires, ref ulong ptmExpires);
  1105. /* 整数转换为 Cookie expires 字符串 */
  1106. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  1107. public static extern bool HP_HttpCookie_HLP_MakeExpiresStr([MarshalAs(UnmanagedType.LPStr)] StringBuilder lpszBuff, ref int piBuffLen, ulong tmExpires);
  1108. /* 生成 Cookie 字符串 */
  1109. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  1110. public static extern bool HP_HttpCookie_HLP_ToString([MarshalAs(UnmanagedType.LPStr)] StringBuilder lpszBuff, ref int piBuffLen, string lpszName, string lpszValue, string lpszDomain, string lpszPath, int iMaxAge /*= -1*/, bool bHttpOnly /*= FALSE*/, bool bSecure /*= FALSE*/, int enSameSite /*= 0*/);
  1111. /* 获取当前 UTC 时间 */
  1112. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  1113. public static extern ulong HP_HttpCookie_HLP_CurrentUTCTime();
  1114. /* Max-Age -> expires */
  1115. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  1116. public static extern ulong HP_HttpCookie_HLP_MaxAgeToExpires(int iMaxAge);
  1117. /* expires -> Max-Age */
  1118. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  1119. public static extern int HP_HttpCookie_HLP_ExpiresToMaxAge(ulong tmExpires);
  1120. }
  1121. }