HttpAgent.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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 HttpAgent : TcpAgent
  9. {
  10. /// <summary>
  11. /// 【可选】开始解析
  12. /// </summary>
  13. public event HttpAgentEvent.OnMessageBeginEventHandler OnMessageBegin;
  14. /// <summary>
  15. /// 【可选】请求头通知
  16. /// </summary>
  17. public event HttpAgentEvent.OnHeaderEventHandler OnHeader;
  18. /// <summary>
  19. /// 【可选】Chunked 报文头通知
  20. /// </summary>
  21. public event HttpAgentEvent.OnChunkHeaderEventHandler OnChunkHeader;
  22. /// <summary>
  23. /// 【可选】Chunked 报文结束通知
  24. /// </summary>
  25. public event HttpAgentEvent.OnChunkCompleteEventHandler OnChunkComplete;
  26. /// <summary>
  27. /// 【可选】升级协议通知
  28. /// </summary>
  29. public event HttpAgentEvent.OnUpgradeEventHandler OnUpgrade;
  30. /// <summary>
  31. /// 【可选】状态行解析完成(仅用于 HTTP 客户端)
  32. /// </summary>
  33. public event HttpAgentEvent.OnStatusLineEventHandler OnStatusLine;
  34. /// <summary>
  35. /// 【必须】请求头完成通知
  36. /// </summary>
  37. public event HttpAgentEvent.OnHeadersCompleteEventHandler OnHeadersComplete;
  38. /// <summary>
  39. /// 【必须】请求体报文通知
  40. /// </summary>
  41. public event HttpAgentEvent.OnBodyEventHandler OnBody;
  42. /// <summary>
  43. /// 【必须】请求体报文通知
  44. /// </summary>
  45. public event HttpAgentEvent.OnPointerDataBodyEventHandler OnPointerDataBody;
  46. /// <summary>
  47. /// 【必须】完成解析通知
  48. /// </summary>
  49. public event HttpAgentEvent.OnMessageCompleteEventHandler OnMessageComplete;
  50. /// <summary>
  51. /// 【必须】解析错误通知
  52. /// </summary>
  53. public event HttpAgentEvent.OnParseErrorEventHandler OnParseError;
  54. /// <summary>
  55. /// 【必须】WebSocket数据包体通知(byte)
  56. /// </summary>
  57. public event WebSocketEvent.OnWSMessageBodyEventHandler OnWSMessageBody;
  58. /// <summary>
  59. /// 【必须】WebSocket数据包体通知(指针)
  60. /// </summary>
  61. public event WebSocketEvent.OnPointerWSMessageBodyEventHandler OnPointerWSMessageBody;
  62. /// <summary>
  63. /// 【必须】WebSocket数据完成解析通知
  64. /// </summary>
  65. public event WebSocketEvent.OnWSMessageCompleteEventHandler OnWSMessageComplete;
  66. /// <summary>
  67. /// 【必须】WebSocket数据头通知
  68. /// </summary>
  69. public event WebSocketEvent.OnWSMessageHeaderEventHandler OnWSMessageHeader;
  70. /// <summary>
  71. /// 创建socket监听&服务组件
  72. /// </summary>
  73. /// <returns></returns>
  74. protected override bool CreateListener()
  75. {
  76. if (IsCreate == true || pListener != IntPtr.Zero || pAgent != IntPtr.Zero)
  77. {
  78. return false;
  79. }
  80. pListener = HttpSdk.Create_HP_HttpAgentListener();
  81. if (pListener == IntPtr.Zero)
  82. {
  83. return false;
  84. }
  85. pAgent = HttpSdk.Create_HP_HttpAgent(pListener);
  86. if (pAgent == IntPtr.Zero)
  87. {
  88. return false;
  89. }
  90. IsCreate = true;
  91. return true;
  92. }
  93. /// <summary>
  94. /// 终止服务并释放资源
  95. /// </summary>
  96. public override void Destroy()
  97. {
  98. Stop();
  99. if (pAgent != IntPtr.Zero)
  100. {
  101. HttpSdk.Destroy_HP_HttpAgent(pAgent);
  102. pAgent = IntPtr.Zero;
  103. }
  104. if (pListener != IntPtr.Zero)
  105. {
  106. HttpSdk.Destroy_HP_HttpAgentListener(pListener);
  107. pListener = IntPtr.Zero;
  108. }
  109. IsCreate = false;
  110. }
  111. HttpSdk.OnMessageBegin _OnMessageBegin;
  112. HttpSdk.OnHeader _OnHeader;
  113. HttpSdk.OnChunkHeader _OnChunkHeader;
  114. HttpSdk.OnChunkComplete _OnChunkComplete;
  115. HttpSdk.OnUpgrade _OnUpgrade;
  116. HttpSdk.OnStatusLine _OnStatusLine;
  117. HttpSdk.OnHeadersComplete _OnHeadersComplete;
  118. HttpSdk.OnBody _OnBody;
  119. HttpSdk.OnMessageComplete _OnMessageComplete;
  120. HttpSdk.OnParseError _OnParseError;
  121. //增加callback
  122. HttpSdk.OnWSMessageBody _OnWSMessageBody;
  123. HttpSdk.OnWSMessageComplete _OnWSMessageComplete;
  124. HttpSdk.OnWSMessageHeader _OnWSMessageHeader;
  125. ///////////////////////////////////////////////////////////////////////////////////////
  126. protected override void SetCallback()
  127. {
  128. // 设置tcpagent的callback
  129. _OnPrepareConnect = new Sdk.OnPrepareConnect(SDK_OnPrepareConnect);
  130. _OnConnect = new Sdk.OnConnect(SDK_OnConnect);
  131. _OnSend = new Sdk.OnSend(SDK_OnSend);
  132. _OnReceive = new Sdk.OnReceive(SDK_OnReceive);
  133. _OnClose = new Sdk.OnClose(SDK_OnClose);
  134. _OnShutdown = new Sdk.OnShutdown(SDK_OnShutdown);
  135. _OnHandShake = new Sdk.OnHandShake(SDK_OnHandShake);
  136. // 设置 Socket 监听器回调函数
  137. HttpSdk.HP_Set_FN_HttpAgent_OnPrepareConnect(pListener, _OnPrepareConnect);
  138. HttpSdk.HP_Set_FN_HttpAgent_OnConnect(pListener, _OnConnect);
  139. HttpSdk.HP_Set_FN_HttpAgent_OnSend(pListener, _OnSend);
  140. HttpSdk.HP_Set_FN_HttpAgent_OnReceive(pListener, _OnReceive);
  141. HttpSdk.HP_Set_FN_HttpAgent_OnClose(pListener, _OnClose);
  142. HttpSdk.HP_Set_FN_HttpAgent_OnShutdown(pListener, _OnShutdown);
  143. HttpSdk.HP_Set_FN_HttpAgent_OnHandShake(pListener, _OnHandShake);
  144. // 设置自身的callback
  145. _OnMessageBegin = new HttpSdk.OnMessageBegin(SDK_OnMessageBegin);
  146. _OnHeader = new HttpSdk.OnHeader(SDK_OnHeader);
  147. _OnChunkHeader = new HttpSdk.OnChunkHeader(SDK_OnChunkHeader);
  148. _OnChunkComplete = new HttpSdk.OnChunkComplete(SDK_OnChunkComplete);
  149. _OnUpgrade = new HttpSdk.OnUpgrade(SDK_OnUpgrade);
  150. _OnStatusLine = new HttpSdk.OnStatusLine(SDK_OnStatusLine);
  151. _OnHeadersComplete = new HttpSdk.OnHeadersComplete(SDK_OnHeadersComplete);
  152. _OnBody = new HttpSdk.OnBody(SDK_OnBody);
  153. _OnMessageComplete = new HttpSdk.OnMessageComplete(SDK_OnMessageComplete);
  154. _OnParseError = new HttpSdk.OnParseError(SDK_OnParseError);
  155. //增加事件!
  156. _OnWSMessageBody = new HttpSdk.OnWSMessageBody(SDK_OnWSMessageBody);
  157. _OnWSMessageComplete = new HttpSdk.OnWSMessageComplete(SDK_OnWSMessageComplete);
  158. _OnWSMessageHeader = new HttpSdk.OnWSMessageHeader(SDK_OnWSMessageHeader);
  159. HttpSdk.HP_Set_FN_HttpAgent_OnMessageBegin(pListener, _OnMessageBegin);
  160. HttpSdk.HP_Set_FN_HttpAgent_OnStatusLine(pListener, _OnStatusLine);
  161. HttpSdk.HP_Set_FN_HttpAgent_OnHeader(pListener, _OnHeader);
  162. HttpSdk.HP_Set_FN_HttpAgent_OnHeadersComplete(pListener, _OnHeadersComplete);
  163. HttpSdk.HP_Set_FN_HttpAgent_OnBody(pListener, _OnBody);
  164. HttpSdk.HP_Set_FN_HttpAgent_OnChunkHeader(pListener, _OnChunkHeader);
  165. HttpSdk.HP_Set_FN_HttpAgent_OnChunkComplete(pListener, _OnChunkComplete);
  166. HttpSdk.HP_Set_FN_HttpAgent_OnMessageComplete(pListener, _OnMessageComplete);
  167. HttpSdk.HP_Set_FN_HttpAgent_OnUpgrade(pListener, _OnUpgrade);
  168. HttpSdk.HP_Set_FN_HttpAgent_OnParseError(pListener, _OnParseError);
  169. //增加
  170. HttpSdk.HP_Set_FN_HttpAgent_OnWSMessageBody(pListener, _OnWSMessageBody);
  171. HttpSdk.HP_Set_FN_HttpAgent_OnWSMessageComplete(pListener, _OnWSMessageComplete);
  172. HttpSdk.HP_Set_FN_HttpAgent_OnWSMessageHeader(pListener, _OnWSMessageHeader);
  173. }
  174. //包解析完成事件
  175. private HandleResult SDK_OnWSMessageHeader(IntPtr pSender, IntPtr connId, bool bFinal, byte iReserved, byte iOperationCode, byte[] lpszMask, ulong ullBodyLen)
  176. {
  177. if (OnWSMessageHeader != null) {
  178. OnWSMessageHeader(connId, bFinal, iReserved, iOperationCode, lpszMask, ullBodyLen);
  179. }
  180. return HandleResult.Ok;
  181. }
  182. //头解析完成事件
  183. private HandleResult SDK_OnWSMessageComplete(IntPtr pSender, IntPtr connId)
  184. {
  185. if (OnWSMessageComplete != null){
  186. return OnWSMessageComplete(connId);
  187. }
  188. return HandleResult.Ok;
  189. }
  190. //包体事件!
  191. private HandleResult SDK_OnWSMessageBody(IntPtr pSender, IntPtr connId, IntPtr pData, int iLength)
  192. {
  193. if (OnWSMessageBody != null)
  194. {
  195. byte[] bytes = new byte[iLength];
  196. Marshal.Copy(pData, bytes, 0, iLength);
  197. return OnWSMessageBody(connId, bytes);
  198. }
  199. else if(OnPointerWSMessageBody!=null)
  200. {
  201. return OnPointerWSMessageBody(connId, pData, iLength);
  202. }
  203. return HandleResult.Ok;
  204. }
  205. protected HttpParseResult SDK_OnParseError(IntPtr pSender, IntPtr connId, int iErrorCode, string lpszErrorDesc)
  206. {
  207. if (OnParseError != null)
  208. {
  209. return OnParseError(connId, iErrorCode, lpszErrorDesc);
  210. }
  211. return HttpParseResult.Ok;
  212. }
  213. protected HttpParseResult SDK_OnMessageComplete(IntPtr pSender, IntPtr connId)
  214. {
  215. if (OnMessageComplete != null)
  216. {
  217. return OnMessageComplete(connId);
  218. }
  219. return HttpParseResult.Ok;
  220. }
  221. protected HttpParseResult SDK_OnBody(IntPtr pSender, IntPtr connId, IntPtr pData, int iLength)
  222. {
  223. if (OnPointerDataBody != null)
  224. {
  225. return OnPointerDataBody(connId, pData, iLength);
  226. }
  227. else if (OnBody != null)
  228. {
  229. byte[] bytes = new byte[iLength];
  230. Marshal.Copy(pData, bytes, 0, iLength);
  231. return OnBody(connId, bytes);
  232. }
  233. return HttpParseResult.Ok;
  234. }
  235. private HttpParseResultEx SDK_OnHeadersComplete(IntPtr pSender, IntPtr connId)
  236. {
  237. if (OnHeadersComplete != null)
  238. {
  239. return OnHeadersComplete(connId);
  240. }
  241. return HttpParseResultEx.Ok;
  242. }
  243. protected HttpParseResult SDK_OnStatusLine(IntPtr pSender, IntPtr connId, ushort usStatusCode, string lpszDesc)
  244. {
  245. if (OnStatusLine != null)
  246. {
  247. return OnStatusLine(connId, usStatusCode, lpszDesc);
  248. }
  249. return HttpParseResult.Ok;
  250. }
  251. protected HttpParseResult SDK_OnUpgrade(IntPtr pSender, IntPtr connId, HttpUpgradeType enUpgradeType)
  252. {
  253. if (OnUpgrade != null)
  254. {
  255. return OnUpgrade(connId, enUpgradeType);
  256. }
  257. return HttpParseResult.Ok;
  258. }
  259. protected HttpParseResult SDK_OnChunkComplete(IntPtr pSender, IntPtr connId)
  260. {
  261. if (OnChunkComplete != null)
  262. {
  263. return OnChunkComplete(connId);
  264. }
  265. return HttpParseResult.Ok;
  266. }
  267. protected HttpParseResult SDK_OnChunkHeader(IntPtr pSender, IntPtr connId, int iLength)
  268. {
  269. if (OnChunkHeader != null)
  270. {
  271. return OnChunkHeader(connId, iLength);
  272. }
  273. return HttpParseResult.Ok;
  274. }
  275. protected HttpParseResult SDK_OnHeader(IntPtr pSender, IntPtr connId, string lpszName, string lpszValue)
  276. {
  277. if (OnHeader != null)
  278. {
  279. return OnHeader(connId, lpszName, lpszValue);
  280. }
  281. return HttpParseResult.Ok;
  282. }
  283. protected HttpParseResult SDK_OnMessageBegin(IntPtr pSender, IntPtr connId)
  284. {
  285. if (OnMessageBegin != null)
  286. {
  287. return OnMessageBegin(connId);
  288. }
  289. return HttpParseResult.Ok;
  290. }
  291. /**************************************************************************/
  292. /***************************** Agent 操作方法 ******************************/
  293. /// <summary>
  294. /// 发送请求
  295. /// </summary>
  296. /// <param name="connId"></param>
  297. /// <param name="method">请求方法</param>
  298. /// <param name="path">请求路径</param>
  299. /// <param name="headers">请求头</param>
  300. /// <param name="body">请求体</param>
  301. /// <returns></returns>
  302. public bool SendRequest(IntPtr connId, HttpMethod method, string path, THeader[] headers, byte[] body, int bodyLength)
  303. {
  304. int headersLength = headers == null ? 0 : headers.Length;
  305. return HttpSdk.HP_HttpAgent_SendRequest(pAgent, connId, method.ToString(), path, headers, headersLength, body, bodyLength);
  306. }
  307. /// <summary>
  308. /// 名称:发送本地文件
  309. /// 描述:向指定连接发送 4096 KB 以下的小文件
  310. /// </summary>
  311. /// <param name="connId"></param>
  312. /// <param name="fileName"></param>
  313. /// <param name="method"></param>
  314. /// <param name="path"></param>
  315. /// <param name="headers"></param>
  316. /// <returns></returns>
  317. public bool SendLocalFile(IntPtr connId, string fileName, HttpMethod method, string path, THeader[] headers)
  318. {
  319. int headersLength = headers == null ? 0 : headers.Length;
  320. return HttpSdk.HP_HttpAgent_SendLocalFile(pAgent, connId, fileName, method.ToString(), path, headers, headersLength);
  321. }
  322. /// <summary>
  323. /// 发送 POST 请求
  324. /// </summary>
  325. /// <param name="connId"></param>
  326. /// <param name="path"></param>
  327. /// <param name="headers"></param>
  328. /// <param name="body"></param>
  329. /// <returns></returns>
  330. public bool SendPost(IntPtr connId, string path, THeader[] headers, string body)
  331. {
  332. int bodyLength = body == null ? 0 : body.Length;
  333. int headersLength = headers == null ? 0 : headers.Length;
  334. return HttpSdk.HP_HttpAgent_SendPost(pAgent, connId, path, headers, headersLength, body, bodyLength);
  335. }
  336. /// <summary>
  337. /// 发送 PUT 请求
  338. /// </summary>
  339. /// <param name="connId"></param>
  340. /// <param name="path"></param>
  341. /// <param name="headers"></param>
  342. /// <param name="body"></param>
  343. /// <returns></returns>
  344. public bool SendPut(IntPtr connId, string path, THeader[] headers, string body)
  345. {
  346. int bodyLength = body == null ? 0 : body.Length;
  347. int headersLength = headers == null ? 0 : headers.Length;
  348. return HttpSdk.HP_HttpAgent_SendPut(pAgent, connId, path, headers, headersLength, body, bodyLength);
  349. }
  350. /// <summary>
  351. /// 发送 PATCH 请求
  352. /// </summary>
  353. /// <param name="connId"></param>
  354. /// <param name="path"></param>
  355. /// <param name="headers"></param>
  356. /// <param name="body"></param>
  357. /// <returns></returns>
  358. public bool SendPatch(IntPtr connId, string path, THeader[] headers, string body)
  359. {
  360. int bodyLength = body == null ? 0 : body.Length;
  361. int headersLength = headers == null ? 0 : headers.Length;
  362. return HttpSdk.HP_HttpAgent_SendPatch(pAgent, connId, path, headers, headersLength, body, bodyLength);
  363. }
  364. /// <summary>
  365. /// 发送 GET 请求
  366. /// </summary>
  367. /// <param name="connId"></param>
  368. /// <param name="path"></param>
  369. /// <param name="headers"></param>
  370. /// <returns></returns>
  371. public bool SendGet(IntPtr connId, string path, THeader[] headers)
  372. {
  373. int headersLength = headers == null ? 0 : headers.Length;
  374. return HttpSdk.HP_HttpAgent_SendGet(pAgent, connId, path, headers, headersLength);
  375. }
  376. /// <summary>
  377. /// 发送 DELETE 请求
  378. /// </summary>
  379. /// <param name="connId"></param>
  380. /// <param name="path"></param>
  381. /// <param name="headers"></param>
  382. /// <returns></returns>
  383. public bool SendDelete(IntPtr connId, string path, THeader[] headers)
  384. {
  385. return HttpSdk.HP_HttpAgent_SendDelete(pAgent, connId, path, headers, headers.Length);
  386. }
  387. /// <summary>
  388. /// 发送 HEAD 请求
  389. /// </summary>
  390. /// <param name="connId"></param>
  391. /// <param name="path"></param>
  392. /// <param name="headers"></param>
  393. /// <returns></returns>
  394. public bool SendHead(IntPtr connId, string path, THeader[] headers)
  395. {
  396. int headersLength = headers == null ? 0 : headers.Length;
  397. return HttpSdk.HP_HttpAgent_SendHead(pAgent, connId, path, headers, headersLength);
  398. }
  399. /// <summary>
  400. /// 发送 TRACE 请求
  401. /// </summary>
  402. /// <param name="connId"></param>
  403. /// <param name="path"></param>
  404. /// <param name="headers"></param>
  405. /// <returns></returns>
  406. public bool SendTrace(IntPtr connId, string path, THeader[] headers)
  407. {
  408. int headersLength = headers == null ? 0 : headers.Length;
  409. return HttpSdk.HP_HttpAgent_SendTrace(pAgent, connId, path, headers, headersLength);
  410. }
  411. /// <summary>
  412. /// 发送 OPTIONS 请求
  413. /// </summary>
  414. /// <param name="connId"></param>
  415. /// <param name="path"></param>
  416. /// <param name="headers"></param>
  417. /// <returns></returns>
  418. public bool SendOptions(IntPtr connId, string path, THeader[] headers)
  419. {
  420. int headersLength = headers == null ? 0 : headers.Length;
  421. return HttpSdk.HP_HttpAgent_SendOptions(pAgent, connId, path, headers, headersLength);
  422. }
  423. /// <summary>
  424. /// 发送 WSMessage 请求
  425. /// </summary>
  426. /// <param name="dwConnID"></param>
  427. /// <param name="bFinal"></param>
  428. /// <param name="iReserved"></param>
  429. /// <param name="iOperationCode"></param>
  430. /// <param name="lpszMask"></param>
  431. /// <param name="Data"></param>
  432. /// <param name="ullBodyLen"></param>
  433. /// <returns></returns>
  434. public bool SendWSMessage(IntPtr dwConnID, bool bFinal, byte iReserved, byte iOperationCode, byte[] lpszMask, byte[] pData, ulong ullBodyLen) {
  435. return HttpSdk.HP_HttpAgent_SendWSMessage(pAgent, dwConnID, bFinal, iReserved, iOperationCode, lpszMask, pData, pData.Length, ullBodyLen);
  436. }
  437. /// <summary>
  438. /// 发送 CONNECT 请求
  439. /// </summary>
  440. /// <param name="connId"></param>
  441. /// <param name="path"></param>
  442. /// <param name="headers"></param>
  443. /// <returns></returns>
  444. public bool SendConnect(IntPtr connId, string path, THeader[] headers)
  445. {
  446. int headersLength = headers == null ? 0 : headers.Length;
  447. return HttpSdk.HP_HttpAgent_SendConnect(pAgent, connId, path, headers, headersLength);
  448. }
  449. /// <summary>
  450. /// 启动 HTTP 通信, 当通信组件设置为非自动启动 HTTP 通信时,需要调用本方法启动 HTTP 通信
  451. /// </summary>
  452. /// <param name="connId"></param>
  453. /// <returns></returns>
  454. public bool StartHttp(IntPtr connId)
  455. {
  456. return HttpSdk.HP_HttpAgent_StartHttp(pAgent, connId);
  457. }
  458. /******************************************************************************/
  459. /***************************** Agent 属性访问方法 ******************************/
  460. /// <summary>
  461. /// 获取或设置是否使用 Cookie
  462. /// </summary>
  463. public bool UseCookie
  464. {
  465. get
  466. {
  467. return HttpSdk.HP_HttpAgent_IsUseCookie(pAgent);
  468. }
  469. set
  470. {
  471. HttpSdk.HP_HttpAgent_SetUseCookie(pAgent, value);
  472. }
  473. }
  474. /// <summary>
  475. /// 获取 HTTP 状态码
  476. /// </summary>
  477. /// <param name="connId"></param>
  478. /// <returns></returns>
  479. public HttpStatusCode GetStatusCode(IntPtr connId)
  480. {
  481. // 有没有可能获取到未定义的状态码???
  482. return (HttpStatusCode)HttpSdk.HP_HttpAgent_GetStatusCode(pAgent, connId);
  483. }
  484. /// <summary>
  485. /// 获取或设置本地协议版本
  486. /// </summary>
  487. public HttpVersion LocalVersion
  488. {
  489. get
  490. {
  491. return HttpSdk.HP_HttpAgent_GetLocalVersion(pAgent);
  492. }
  493. set
  494. {
  495. HttpSdk.HP_HttpAgent_SetLocalVersion(pAgent, value);
  496. }
  497. }
  498. /// <summary>
  499. /// 检查是否升级协议
  500. /// </summary>
  501. /// <param name="connId"></param>
  502. /// <returns></returns>
  503. public bool IsUpgrade(IntPtr connId)
  504. {
  505. return HttpSdk.HP_HttpAgent_IsUpgrade(pAgent, connId);
  506. }
  507. /// <summary>
  508. /// 检查是否有 Keep-Alive 标识
  509. /// </summary>
  510. /// <param name="connId"></param>
  511. /// <returns></returns>
  512. public bool IsKeepAlive(IntPtr connId)
  513. {
  514. return HttpSdk.HP_HttpAgent_IsKeepAlive(pAgent, connId);
  515. }
  516. /// <summary>
  517. /// 获取协议版本
  518. /// </summary>
  519. /// <param name="connId"></param>
  520. /// <returns></returns>
  521. public HttpVersion GetVersion(IntPtr connId)
  522. {
  523. return (HttpVersion)HttpSdk.HP_HttpAgent_GetVersion(pAgent, connId);
  524. }
  525. /// <summary>
  526. /// 获取内容长度
  527. /// </summary>
  528. /// <param name="connId"></param>
  529. /// <returns></returns>
  530. public long GetContentLength(IntPtr connId)
  531. {
  532. return HttpSdk.HP_HttpAgent_GetContentLength(pAgent, connId);
  533. }
  534. /// <summary>
  535. /// 获取内容类型
  536. /// </summary>
  537. /// <param name="connId"></param>
  538. /// <returns></returns>
  539. public string GetContentType(IntPtr connId)
  540. {
  541. IntPtr ptr = HttpSdk.HP_HttpAgent_GetContentType(pAgent, connId);
  542. return Common.PtrToAnsiString(ptr);
  543. }
  544. /// <summary>
  545. /// 获取内容类型
  546. /// </summary>
  547. /// <returns></returns>
  548. public string GetContentEncoding(IntPtr connId)
  549. {
  550. IntPtr ptr = HttpSdk.HP_HttpAgent_GetContentEncoding(pAgent, connId);
  551. return Common.PtrToAnsiString(ptr);
  552. }
  553. /// <summary>
  554. /// 获取传输编码
  555. /// </summary>
  556. /// <returns></returns>
  557. public string GetTransferEncoding(IntPtr connId)
  558. {
  559. IntPtr ptr = HttpSdk.HP_HttpAgent_GetTransferEncoding(pAgent, connId);
  560. return Common.PtrToAnsiString(ptr);
  561. }
  562. /// <summary>
  563. /// 获取协议升级类型
  564. /// </summary>
  565. /// <param name="connId"></param>
  566. /// <returns></returns>
  567. public HttpUpgradeType GetUpgradeType(IntPtr connId)
  568. {
  569. return HttpSdk.HP_HttpAgent_GetUpgradeType(pAgent, connId);
  570. }
  571. /// <summary>
  572. /// 获取解析错误代码
  573. /// </summary>
  574. /// <param name="connId"></param>
  575. /// <returns></returns>
  576. public ErrorInfo GetParseErrorInfo(IntPtr connId)
  577. {
  578. IntPtr ptr = IntPtr.Zero;
  579. var error = new ErrorInfo();
  580. error.ErrorCode = HttpSdk.HP_HttpAgent_GetParseErrorCode(pAgent, connId, ref ptr);
  581. if (ptr != IntPtr.Zero)
  582. {
  583. try
  584. {
  585. error.ErrorMsg = Common.PtrToAnsiString(ptr); ;
  586. }
  587. catch (Exception)
  588. {
  589. }
  590. }
  591. return error;
  592. }
  593. /// <summary>
  594. /// 获取某个请求头(单值)
  595. /// </summary>
  596. /// <param name="connId"></param>
  597. /// <param name="key"></param>
  598. /// <returns></returns>
  599. public string GetHeader(IntPtr connId, string key)
  600. {
  601. IntPtr ptr = IntPtr.Zero;
  602. if (HttpSdk.HP_HttpAgent_GetHeader(pAgent, connId, key, ref ptr))
  603. {
  604. return Common.PtrToAnsiString(ptr);
  605. }
  606. return "";
  607. }
  608. /// <summary>
  609. /// 获取某个请求头(多值)
  610. /// </summary>
  611. /// <param name="connId"></param>
  612. /// <param name="key"></param>
  613. /// <returns></returns>
  614. public List<string> GetHeaders(IntPtr connId, string key)
  615. {
  616. var list = new List<string>();
  617. try
  618. {
  619. IntPtr[] arr = null;
  620. uint count = 0;
  621. HttpSdk.HP_HttpAgent_GetHeaders(pAgent, connId, key, arr, ref count);
  622. if (count > 0)
  623. {
  624. arr = new IntPtr[count];
  625. if (HttpSdk.HP_HttpAgent_GetHeaders(pAgent, connId, key, arr, ref count) && count > 0)
  626. {
  627. foreach (var item in arr)
  628. {
  629. list.Add(Common.PtrToAnsiString(item));
  630. }
  631. }
  632. }
  633. }
  634. catch (Exception)
  635. {
  636. }
  637. return list;
  638. }
  639. /// <summary>
  640. /// 获取所有请求头
  641. /// </summary>
  642. /// <param name="connId"></param>
  643. /// <returns></returns>
  644. public List<THeader> GetAllHeaders(IntPtr connId)
  645. {
  646. var list = new List<THeader>();
  647. try
  648. {
  649. uint count = 0;
  650. IntPtr arrPtr = IntPtr.Zero;
  651. HttpSdk.HP_HttpAgent_GetAllHeaders(pAgent, connId, arrPtr, ref count);
  652. if (count > 0)
  653. {
  654. var ptrArr = new THeaderForIntPtr[count];
  655. arrPtr = Marshal.UnsafeAddrOfPinnedArrayElement(ptrArr, 0);
  656. if (HttpSdk.HP_HttpAgent_GetAllHeaders(pAgent, connId, arrPtr, ref count))
  657. {
  658. foreach (var item in ptrArr)
  659. {
  660. list.Add(new THeader()
  661. {
  662. Name = Common.PtrToAnsiString(item.Name),
  663. Value = Common.PtrToAnsiString(item.Value),
  664. });
  665. }
  666. }
  667. }
  668. }
  669. catch (Exception)
  670. {
  671. }
  672. return list;
  673. }
  674. /// <summary>
  675. /// 获取所有请求头名称
  676. /// </summary>
  677. /// <param name="connId"></param>
  678. /// <returns></returns>
  679. public List<string> GetAllHeaderNames(IntPtr connId)
  680. {
  681. var list = new List<string>();
  682. try
  683. {
  684. IntPtr[] arr = null;
  685. uint count = 0;
  686. HttpSdk.HP_HttpAgent_GetAllHeaderNames(pAgent, connId, arr, ref count);
  687. if (count > 0)
  688. {
  689. arr = new IntPtr[count];
  690. if (HttpSdk.HP_HttpAgent_GetAllHeaderNames(pAgent, connId, arr, ref count) && count > 0)
  691. {
  692. foreach (var item in arr)
  693. {
  694. list.Add(Common.PtrToAnsiString(item));
  695. }
  696. }
  697. }
  698. }
  699. catch (Exception)
  700. {
  701. }
  702. return list;
  703. }
  704. /// <summary>
  705. /// 获取Cookie
  706. /// </summary>
  707. /// <param name="connId"></param>
  708. /// <param name="key"></param>
  709. /// <returns></returns>
  710. public string GetCookie(IntPtr connId, string key)
  711. {
  712. IntPtr ptr = IntPtr.Zero;
  713. if (HttpSdk.HP_HttpAgent_GetCookie(pAgent, connId, key, ref ptr))
  714. {
  715. return Common.PtrToAnsiString(ptr);
  716. }
  717. return "";
  718. }
  719. /// <summary>
  720. /// 获取所有 Cookie
  721. /// </summary>
  722. /// <param name="connId"></param>
  723. /// <param name="lpCookies"></param>
  724. /// <param name="pdwCount"></param>
  725. /// <returns></returns>
  726. public List<TCookie> GetAllCookies(IntPtr connId)
  727. {
  728. var list = new List<TCookie>();
  729. try
  730. {
  731. uint count = 0;
  732. IntPtr arrPtr = IntPtr.Zero;
  733. HttpSdk.HP_HttpAgent_GetAllCookies(pAgent, connId, arrPtr, ref count);
  734. if (count > 0)
  735. {
  736. var ptrArr = new THeaderForIntPtr[count];
  737. arrPtr = Marshal.UnsafeAddrOfPinnedArrayElement(ptrArr, 0);
  738. if (HttpSdk.HP_HttpAgent_GetAllCookies(pAgent, connId, arrPtr, ref count))
  739. {
  740. foreach (var item in ptrArr)
  741. {
  742. list.Add(new TCookie()
  743. {
  744. Name = Common.PtrToAnsiString(item.Name),
  745. Value = Common.PtrToAnsiString(item.Value)
  746. });
  747. }
  748. }
  749. }
  750. }
  751. catch (Exception)
  752. {
  753. }
  754. return list;
  755. }
  756. /// <summary>
  757. /// 获取或设置 HTTP 启动方式,默认为true
  758. /// </summary>
  759. public bool HttpAutoStart
  760. {
  761. get
  762. {
  763. return HttpSdk.HP_HttpAgent_IsHttpAutoStart(pAgent);
  764. }
  765. set
  766. {
  767. HttpSdk.HP_HttpAgent_SetHttpAutoStart(pAgent, value);
  768. }
  769. }
  770. }
  771. }