HttpClient.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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 HttpClient : TcpClient
  9. {
  10. /// <summary>
  11. /// 【可选】开始解析
  12. /// </summary>
  13. public event HttpClientEvent.OnMessageBeginEventHandler OnMessageBegin;
  14. /// <summary>
  15. /// 【可选】请求头通知
  16. /// </summary>
  17. public event HttpClientEvent.OnHeaderEventHandler OnHeader;
  18. /// <summary>
  19. /// 【可选】Chunked 报文头通知
  20. /// </summary>
  21. public event HttpClientEvent.OnChunkHeaderEventHandler OnChunkHeader;
  22. /// <summary>
  23. /// 【可选】Chunked 报文结束通知
  24. /// </summary>
  25. public event HttpClientEvent.OnChunkCompleteEventHandler OnChunkComplete;
  26. /// <summary>
  27. /// 【可选】升级协议通知
  28. /// </summary>
  29. public event HttpClientEvent.OnUpgradeEventHandler OnUpgrade;
  30. /// <summary>
  31. /// 【可选】状态行解析完成(仅用于 HTTP 客户端)
  32. /// </summary>
  33. public event HttpClientEvent.OnStatusLineEventHandler OnStatusLine;
  34. /// <summary>
  35. /// 【必须】请求头完成通知
  36. /// </summary>
  37. public event HttpClientEvent.OnHeadersCompleteEventHandler OnHeadersComplete;
  38. /// <summary>
  39. /// 【必须】请求体报文通知
  40. /// </summary>
  41. public event HttpClientEvent.OnBodyEventHandler OnBody;
  42. /// <summary>
  43. /// 【必须】请求体报文通知
  44. /// </summary>
  45. public event HttpClientEvent.OnPointerDataBodyEventHandler OnPointerDataBody;
  46. /// <summary>
  47. /// 【必须】完成解析通知
  48. /// </summary>
  49. public event HttpClientEvent.OnMessageCompleteEventHandler OnMessageComplete;
  50. /// <summary>
  51. /// 【必须】解析错误通知
  52. /// </summary>
  53. public event HttpClientEvent.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 || pClient != IntPtr.Zero)
  77. {
  78. return false;
  79. }
  80. pListener = HttpSdk.Create_HP_HttpClientListener();
  81. if (pListener == IntPtr.Zero)
  82. {
  83. return false;
  84. }
  85. pClient = HttpSdk.Create_HP_HttpClient(pListener);
  86. if (pClient == 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 (pClient != IntPtr.Zero)
  100. {
  101. HttpSdk.Destroy_HP_HttpClient(pClient);
  102. pClient = IntPtr.Zero;
  103. }
  104. if (pListener != IntPtr.Zero)
  105. {
  106. HttpSdk.Destroy_HP_HttpClientListener(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. // 设置tcpClient的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. _OnHandShake = new Sdk.OnHandShake(SDK_OnHandShake);
  135. // 设置 Socket 监听器回调函数
  136. HttpSdk.HP_Set_FN_HttpClient_OnPrepareConnect(pListener, _OnPrepareConnect);
  137. HttpSdk.HP_Set_FN_HttpClient_OnConnect(pListener, _OnConnect);
  138. HttpSdk.HP_Set_FN_HttpClient_OnSend(pListener, _OnSend);
  139. HttpSdk.HP_Set_FN_HttpClient_OnReceive(pListener, _OnReceive);
  140. HttpSdk.HP_Set_FN_HttpClient_OnClose(pListener, _OnClose);
  141. HttpSdk.HP_Set_FN_HttpClient_OnHandShake(pListener, _OnHandShake);
  142. // 设置自身的callback
  143. _OnMessageBegin = new HttpSdk.OnMessageBegin(SDK_OnMessageBegin);
  144. _OnHeader = new HttpSdk.OnHeader(SDK_OnHeader);
  145. _OnChunkHeader = new HttpSdk.OnChunkHeader(SDK_OnChunkHeader);
  146. _OnChunkComplete = new HttpSdk.OnChunkComplete(SDK_OnChunkComplete);
  147. _OnUpgrade = new HttpSdk.OnUpgrade(SDK_OnUpgrade);
  148. _OnStatusLine = new HttpSdk.OnStatusLine(SDK_OnStatusLine);
  149. _OnHeadersComplete = new HttpSdk.OnHeadersComplete(SDK_OnHeadersComplete);
  150. _OnBody = new HttpSdk.OnBody(SDK_OnBody);
  151. _OnMessageComplete = new HttpSdk.OnMessageComplete(SDK_OnMessageComplete);
  152. _OnParseError = new HttpSdk.OnParseError(SDK_OnParseError);
  153. //增加的
  154. _OnWSMessageBody = new HttpSdk.OnWSMessageBody(SDK_OnWSMessageBody);
  155. _OnWSMessageComplete = new HttpSdk.OnWSMessageComplete(SDK_OnWSMessageComplete);
  156. _OnWSMessageHeader = new HttpSdk.OnWSMessageHeader(SDK_OnWSMessageHeader);
  157. HttpSdk.HP_Set_FN_HttpClient_OnMessageBegin(pListener, _OnMessageBegin);
  158. HttpSdk.HP_Set_FN_HttpClient_OnStatusLine(pListener, _OnStatusLine);
  159. HttpSdk.HP_Set_FN_HttpClient_OnHeader(pListener, _OnHeader);
  160. HttpSdk.HP_Set_FN_HttpClient_OnHeadersComplete(pListener, _OnHeadersComplete);
  161. HttpSdk.HP_Set_FN_HttpClient_OnBody(pListener, _OnBody);
  162. HttpSdk.HP_Set_FN_HttpClient_OnChunkHeader(pListener, _OnChunkHeader);
  163. HttpSdk.HP_Set_FN_HttpClient_OnChunkComplete(pListener, _OnChunkComplete);
  164. HttpSdk.HP_Set_FN_HttpClient_OnMessageComplete(pListener, _OnMessageComplete);
  165. HttpSdk.HP_Set_FN_HttpClient_OnUpgrade(pListener, _OnUpgrade);
  166. HttpSdk.HP_Set_FN_HttpClient_OnParseError(pListener, _OnParseError);
  167. //设置回调
  168. HttpSdk.HP_Set_FN_HttpAgent_OnWSMessageBody(pListener, _OnWSMessageBody);
  169. HttpSdk.HP_Set_FN_HttpAgent_OnWSMessageComplete(pListener, _OnWSMessageComplete);
  170. HttpSdk.HP_Set_FN_HttpAgent_OnWSMessageHeader(pListener, _OnWSMessageHeader);
  171. }
  172. //数据包解析完成
  173. private HandleResult SDK_OnWSMessageHeader(IntPtr pSender, IntPtr connId, bool bFinal, byte iReserved, byte iOperationCode, byte[] lpszMask, ulong ullBodyLen)
  174. {
  175. if (OnWSMessageHeader != null)
  176. {
  177. OnWSMessageHeader(connId, bFinal, iReserved, iOperationCode, lpszMask, ullBodyLen);
  178. }
  179. return HandleResult.Ok;
  180. }
  181. //头解析完成事件
  182. private HandleResult SDK_OnWSMessageComplete(IntPtr pSender, IntPtr connId)
  183. {
  184. if (OnWSMessageComplete != null)
  185. {
  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(HttpMethod method, string path, THeader[] headers, byte[] body, int bodyLength)
  303. {
  304. int headersLength = headers == null ? 0 : headers.Length;
  305. return HttpSdk.HP_HttpClient_SendRequest(pClient, 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(string fileName, HttpMethod method, string path, THeader[] headers)
  318. {
  319. int headersLength = headers == null ? 0 : headers.Length;
  320. return HttpSdk.HP_HttpClient_SendLocalFile(pClient, 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(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_HttpClient_SendPost(pClient, 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(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_HttpClient_SendPut(pClient, 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(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_HttpClient_SendPatch(pClient, 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(string path, THeader[] headers)
  372. {
  373. int headersLength = headers == null ? 0 : headers.Length;
  374. return HttpSdk.HP_HttpClient_SendGet(pClient, 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(string path, THeader[] headers)
  384. {
  385. int headersLength = headers == null ? 0 : headers.Length;
  386. return HttpSdk.HP_HttpClient_SendDelete(pClient, path, headers, headersLength);
  387. }
  388. /// <summary>
  389. /// 发送 HEAD 请求
  390. /// </summary>
  391. /// <param name="connId"></param>
  392. /// <param name="path"></param>
  393. /// <param name="headers"></param>
  394. /// <returns></returns>
  395. public bool SendHead(string path, THeader[] headers)
  396. {
  397. int headersLength = headers == null ? 0 : headers.Length;
  398. return HttpSdk.HP_HttpClient_SendHead(pClient, path, headers, headersLength);
  399. }
  400. /// <summary>
  401. /// 发送 TRACE 请求
  402. /// </summary>
  403. /// <param name="connId"></param>
  404. /// <param name="path"></param>
  405. /// <param name="headers"></param>
  406. /// <returns></returns>
  407. public bool SendTrace(string path, THeader[] headers)
  408. {
  409. int headersLength = headers == null ? 0 : headers.Length;
  410. return HttpSdk.HP_HttpClient_SendTrace(pClient, path, headers, headersLength);
  411. }
  412. /// <summary>
  413. /// 发送 WSMessage 请求
  414. /// </summary>
  415. /// <param name="dwConnID"></param>
  416. /// <param name="bFinal"></param>
  417. /// <param name="iReserved"></param>
  418. /// <param name="iOperationCode"></param>
  419. /// <param name="lpszMask"></param>
  420. /// <param name="Data"></param>
  421. /// <param name="ullBodyLen"></param>
  422. /// <returns></returns>
  423. public bool SendWSMessage( bool bFinal, byte iReserved, byte iOperationCode, byte[] lpszMask, byte[] pData, ulong ullBodyLen)
  424. {
  425. return HttpSdk.HP_HttpClient_SendWSMessage(pClient, bFinal, iReserved, iOperationCode, lpszMask, pData, pData.Length, ullBodyLen);
  426. }
  427. /// <summary>
  428. /// 发送 OPTIONS 请求
  429. /// </summary>
  430. /// <param name="connId"></param>
  431. /// <param name="path"></param>
  432. /// <param name="headers"></param>
  433. /// <returns></returns>
  434. public bool SendOptions(string path, THeader[] headers)
  435. {
  436. int headersLength = headers == null ? 0 : headers.Length;
  437. return HttpSdk.HP_HttpClient_SendOptions(pClient, path, headers, headersLength);
  438. }
  439. /// <summary>
  440. /// 发送 CONNECT 请求
  441. /// </summary>
  442. /// <param name="connId"></param>
  443. /// <param name="path"></param>
  444. /// <param name="headers"></param>
  445. /// <returns></returns>
  446. public bool SendConnect(string path, THeader[] headers)
  447. {
  448. int headersLength = headers == null ? 0 : headers.Length;
  449. return HttpSdk.HP_HttpClient_SendConnect(pClient, path, headers, headersLength);
  450. }
  451. /// <summary>
  452. /// 启动 HTTP 通信, 当通信组件设置为非自动启动 HTTP 通信时,需要调用本方法启动 HTTP 通信
  453. /// </summary>
  454. /// <param name="connId"></param>
  455. /// <returns></returns>
  456. public bool StartHttp()
  457. {
  458. return HttpSdk.HP_HttpClient_StartHttp(pClient);
  459. }
  460. /******************************************************************************/
  461. /***************************** Agent 属性访问方法 ******************************/
  462. /// <summary>
  463. /// 获取或设置是否使用 Cookie
  464. /// </summary>
  465. public bool UseCookie
  466. {
  467. get
  468. {
  469. return HttpSdk.HP_HttpClient_IsUseCookie(pClient);
  470. }
  471. set
  472. {
  473. HttpSdk.HP_HttpClient_SetUseCookie(pClient, value);
  474. }
  475. }
  476. /// <summary>
  477. /// 获取 HTTP 状态码
  478. /// </summary>
  479. /// <param name="connId"></param>
  480. /// <returns></returns>
  481. public HttpStatusCode GetStatusCode(IntPtr connId)
  482. {
  483. // 有没有可能获取到未定义的状态码???
  484. return (HttpStatusCode)HttpSdk.HP_HttpClient_GetStatusCode(pClient);
  485. }
  486. /// <summary>
  487. /// 获取或设置本地协议版本
  488. /// </summary>
  489. public HttpVersion LocalVersion
  490. {
  491. get
  492. {
  493. return HttpSdk.HP_HttpClient_GetLocalVersion(pClient);
  494. }
  495. set
  496. {
  497. HttpSdk.HP_HttpClient_SetLocalVersion(pClient, value);
  498. }
  499. }
  500. /// <summary>
  501. /// 检查是否升级协议
  502. /// </summary>
  503. /// <param name="connId"></param>
  504. /// <returns></returns>
  505. public bool IsUpgrade(IntPtr connId)
  506. {
  507. return HttpSdk.HP_HttpClient_IsUpgrade(pClient);
  508. }
  509. /// <summary>
  510. /// 检查是否有 Keep-Alive 标识
  511. /// </summary>
  512. /// <param name="connId"></param>
  513. /// <returns></returns>
  514. public bool IsKeepAlive(IntPtr connId)
  515. {
  516. return HttpSdk.HP_HttpClient_IsKeepAlive(pClient);
  517. }
  518. /// <summary>
  519. /// 获取协议版本
  520. /// </summary>
  521. /// <param name="connId"></param>
  522. /// <returns></returns>
  523. public HttpVersion GetVersion(IntPtr connId)
  524. {
  525. return (HttpVersion)HttpSdk.HP_HttpClient_GetVersion(pClient);
  526. }
  527. /// <summary>
  528. /// 获取内容长度
  529. /// </summary>
  530. /// <param name="connId"></param>
  531. /// <returns></returns>
  532. public long GetContentLength(IntPtr connId)
  533. {
  534. return HttpSdk.HP_HttpClient_GetContentLength(pClient);
  535. }
  536. /// <summary>
  537. /// 获取内容类型
  538. /// </summary>
  539. /// <param name="connId"></param>
  540. /// <returns></returns>
  541. public string GetContentType(IntPtr connId)
  542. {
  543. IntPtr ptr = HttpSdk.HP_HttpClient_GetContentType(pClient);
  544. return Common.PtrToAnsiString(ptr);
  545. }
  546. /// <summary>
  547. /// 获取内容类型
  548. /// </summary>
  549. /// <returns></returns>
  550. public string GetContentEncoding(IntPtr connId)
  551. {
  552. IntPtr ptr = HttpSdk.HP_HttpClient_GetContentEncoding(pClient);
  553. return Common.PtrToAnsiString(ptr);
  554. }
  555. /// <summary>
  556. /// 获取传输编码
  557. /// </summary>
  558. /// <returns></returns>
  559. public string GetTransferEncoding(IntPtr connId)
  560. {
  561. IntPtr ptr = HttpSdk.HP_HttpClient_GetTransferEncoding(pClient);
  562. return Common.PtrToAnsiString(ptr);
  563. }
  564. /// <summary>
  565. /// 获取协议升级类型
  566. /// </summary>
  567. /// <param name="connId"></param>
  568. /// <returns></returns>
  569. public HttpUpgradeType GetUpgradeType(IntPtr connId)
  570. {
  571. return HttpSdk.HP_HttpClient_GetUpgradeType(pClient);
  572. }
  573. /// <summary>
  574. /// 获取解析错误代码
  575. /// </summary>
  576. /// <param name="connId"></param>
  577. /// <returns></returns>
  578. public ErrorInfo GetParseErrorInfo(IntPtr connId)
  579. {
  580. IntPtr ptr = IntPtr.Zero;
  581. var error = new ErrorInfo();
  582. error.ErrorCode = HttpSdk.HP_HttpClient_GetParseErrorCode(pClient, ref ptr);
  583. if (ptr != IntPtr.Zero)
  584. {
  585. try
  586. {
  587. error.ErrorMsg = Common.PtrToAnsiString(ptr); ;
  588. }
  589. catch (Exception)
  590. {
  591. }
  592. }
  593. return error;
  594. }
  595. /// <summary>
  596. /// 获取某个请求头(单值)
  597. /// </summary>
  598. /// <param name="connId"></param>
  599. /// <param name="key"></param>
  600. /// <returns></returns>
  601. public string GetHeader(string key)
  602. {
  603. IntPtr ptr = IntPtr.Zero;
  604. if (HttpSdk.HP_HttpClient_GetHeader(pClient, key, ref ptr))
  605. {
  606. return Common.PtrToAnsiString(ptr);
  607. }
  608. return "";
  609. }
  610. /// <summary>
  611. /// 获取某个请求头(多值)
  612. /// </summary>
  613. /// <param name="connId"></param>
  614. /// <param name="key"></param>
  615. /// <returns></returns>
  616. public List<string> GetHeaders(string key)
  617. {
  618. var list = new List<string>();
  619. try
  620. {
  621. IntPtr[] arr = null;
  622. uint count = 0;
  623. HttpSdk.HP_HttpClient_GetHeaders(pClient, key, arr, ref count);
  624. if (count > 0)
  625. {
  626. arr = new IntPtr[count];
  627. if (HttpSdk.HP_HttpClient_GetHeaders(pClient, key, arr, ref count) && count > 0)
  628. {
  629. foreach (var item in arr)
  630. {
  631. list.Add(Common.PtrToAnsiString(item));
  632. }
  633. }
  634. }
  635. }
  636. catch (Exception)
  637. {
  638. }
  639. return list;
  640. }
  641. /// <summary>
  642. /// 获取所有请求头
  643. /// </summary>
  644. /// <param name="connId"></param>
  645. /// <returns></returns>
  646. public List<THeader> GetAllHeaders(IntPtr connId)
  647. {
  648. var list = new List<THeader>();
  649. try
  650. {
  651. uint count = 0;
  652. IntPtr arrPtr = IntPtr.Zero;
  653. HttpSdk.HP_HttpClient_GetAllHeaders(pClient, arrPtr, ref count);
  654. if (count > 0)
  655. {
  656. var ptrArr = new THeaderForIntPtr[count];
  657. arrPtr = Marshal.UnsafeAddrOfPinnedArrayElement(ptrArr, 0);
  658. if (HttpSdk.HP_HttpClient_GetAllHeaders(pClient, arrPtr, ref count))
  659. {
  660. foreach (var item in ptrArr)
  661. {
  662. list.Add(new THeader()
  663. {
  664. Name = Common.PtrToAnsiString(item.Name),
  665. Value = Common.PtrToAnsiString(item.Value),
  666. });
  667. }
  668. }
  669. }
  670. }
  671. catch (Exception)
  672. {
  673. }
  674. return list;
  675. }
  676. /// <summary>
  677. /// 获取所有请求头名称
  678. /// </summary>
  679. /// <param name="connId"></param>
  680. /// <returns></returns>
  681. public List<string> GetAllHeaderNames(IntPtr connId)
  682. {
  683. var list = new List<string>();
  684. try
  685. {
  686. IntPtr[] arr = null;
  687. uint count = 0;
  688. HttpSdk.HP_HttpClient_GetAllHeaderNames(pClient, arr, ref count);
  689. if (count > 0)
  690. {
  691. arr = new IntPtr[count];
  692. if (HttpSdk.HP_HttpClient_GetAllHeaderNames(pClient, arr, ref count) && count > 0)
  693. {
  694. foreach (var item in arr)
  695. {
  696. list.Add(Common.PtrToAnsiString(item));
  697. }
  698. }
  699. }
  700. }
  701. catch (Exception)
  702. {
  703. }
  704. return list;
  705. }
  706. /// <summary>
  707. /// 获取Cookie
  708. /// </summary>
  709. /// <param name="connId"></param>
  710. /// <param name="key"></param>
  711. /// <returns></returns>
  712. public string GetCookie(string key)
  713. {
  714. IntPtr ptr = IntPtr.Zero;
  715. if (HttpSdk.HP_HttpClient_GetCookie(pClient, key, ref ptr))
  716. {
  717. return Common.PtrToAnsiString(ptr);
  718. }
  719. return "";
  720. }
  721. /// <summary>
  722. /// 获取所有 Cookie
  723. /// </summary>
  724. /// <param name="connId"></param>
  725. /// <param name="lpCookies"></param>
  726. /// <param name="pdwCount"></param>
  727. /// <returns></returns>
  728. public List<TCookie> GetAllCookies(IntPtr connId)
  729. {
  730. var list = new List<TCookie>();
  731. try
  732. {
  733. uint count = 0;
  734. IntPtr arrPtr = IntPtr.Zero;
  735. HttpSdk.HP_HttpClient_GetAllCookies(pClient, arrPtr, ref count);
  736. if (count > 0)
  737. {
  738. var ptrArr = new THeaderForIntPtr[count];
  739. arrPtr = Marshal.UnsafeAddrOfPinnedArrayElement(ptrArr, 0);
  740. if (HttpSdk.HP_HttpClient_GetAllCookies(pClient, arrPtr, ref count))
  741. {
  742. foreach (var item in ptrArr)
  743. {
  744. list.Add(new TCookie()
  745. {
  746. Name = Common.PtrToAnsiString(item.Name),
  747. Value = Common.PtrToAnsiString(item.Value)
  748. });
  749. }
  750. }
  751. }
  752. }
  753. catch (Exception)
  754. {
  755. }
  756. return list;
  757. }
  758. /// <summary>
  759. /// 获取或设置 HTTP 启动方式,默认为true
  760. /// </summary>
  761. public bool HttpAutoStart
  762. {
  763. get
  764. {
  765. return HttpSdk.HP_HttpClient_IsHttpAutoStart(pClient);
  766. }
  767. set
  768. {
  769. HttpSdk.HP_HttpServer_SetHttpAutoStart(pClient, value);
  770. }
  771. }
  772. }
  773. }