HttpSession.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  1. #include "HttpSession.h"
  2. #include "Poco/Net/MediaType.h"
  3. #include "Poco/Exception.h"
  4. #include "Poco/Net/MultipartWriter.h"
  5. #include "Poco/Net/NameValueCollection.h"
  6. #include "Poco/Net/HTTPCookie.h"
  7. #include "ErrorCode.h"
  8. #include <fstream>
  9. #include "Poco/Buffer.h"
  10. #include "Poco/Timespan.h"
  11. #include "Poco/AutoPtr.h"
  12. #include "Poco/Util/IniFileConfiguration.h"
  13. #include "PhmsLogger.h"
  14. #include "Util.h"
  15. #include "GlobalTerminalLogoString.h"
  16. #include <sstream>
  17. using Poco::Exception;
  18. using Poco::TimeoutException;
  19. using Poco::Net::MultipartWriter;
  20. using Poco::Net::MediaType;
  21. using Poco::Net::MessageHeader;
  22. using Poco::Buffer;
  23. using Poco::Timespan;
  24. using Poco::Net::NameValueCollection;
  25. using Poco::Net::HTTPCookie;
  26. using Poco::AutoPtr;
  27. using Poco::Util::IniFileConfiguration;
  28. using std::ifstream;
  29. HTTPCookie g_cookieGuanXinSessionId;
  30. CHttpSession::CHttpSession(void):m_bSsl(false)
  31. {
  32. m_pSession = new HTTPClientSession();
  33. m_pContext = NULL;
  34. }
  35. CHttpSession::CHttpSession(bool bSsl):m_bSsl(bSsl)
  36. {
  37. if(!m_bSsl)
  38. {
  39. m_pSession = new HTTPClientSession();
  40. m_pContext = NULL;
  41. }
  42. else
  43. {
  44. //不知道为什么调用Poco::Net::initializeSSL()不起作用,无参数的HTTPSClientSession构造函数报错,必须先创建一个Context
  45. //然后调用传入Context::Ptr类型的HTTPSClientSession构造函数,以后有时间在研究
  46. //不必考虑ssl初始化函数重入,Poco::Crypto::OpenSSLInitializer::initialize()函数做了互斥
  47. m_pContext = new Context(Context::CLIENT_USE, "", "", "", Context::VERIFY_NONE, 9, false);
  48. m_pSession = new HTTPSClientSession(m_pContext);
  49. }
  50. }
  51. CHttpSession::CHttpSession(CNetConfig netConfig):m_bSsl(netConfig.GetSsl())
  52. {
  53. if(!m_bSsl)
  54. {
  55. m_pSession = new HTTPClientSession();
  56. m_pContext = NULL;
  57. }
  58. else
  59. {
  60. m_pContext = new Context(Context::CLIENT_USE, "", "", "", Context::VERIFY_NONE, 9, false);
  61. m_pSession = new HTTPSClientSession(m_pContext);
  62. }
  63. }
  64. CHttpSession::~CHttpSession(void)
  65. {
  66. delete m_pSession;
  67. }
  68. //Getter
  69. bool CHttpSession::GetSsl()
  70. {
  71. return m_bSsl;
  72. }
  73. HTTPClientSession* CHttpSession::GetHttpClientSession()
  74. {
  75. return m_pSession;
  76. }
  77. int CHttpSession::ExecuteHttpSession(CNetConfig& netConfig, CPhmsRequest& phmsRequest, istream* & pInputStream)
  78. {
  79. //if(m_pSession->connected())
  80. {
  81. m_pSession->release();
  82. }
  83. int nRet = PHMS_SUCCESSFUL_RESULT;
  84. nRet = this->SetNetWork(netConfig);
  85. if(nRet != PHMS_SUCCESSFUL_RESULT)
  86. {
  87. //写日志
  88. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(nRet), __FUNCTION__, __FILE__, __LINE__);
  89. return nRet;
  90. }
  91. m_pSession->setTimeout(Timespan(1000000*60));//设置接收超时1分钟
  92. HTTPRequest httpRequest;
  93. string stringBoundary = MultipartWriter::createBoundary();
  94. this->GenerateHttpRequest(netConfig, phmsRequest, stringBoundary, httpRequest);
  95. try
  96. {
  97. ostream &requestStream = m_pSession->sendRequest(httpRequest);
  98. netConfig.SetIp(m_pSession->getIp());
  99. nRet = this->SendPhms(requestStream, stringBoundary, phmsRequest);
  100. if(nRet != PHMS_SUCCESSFUL_RESULT)
  101. {
  102. //写日志
  103. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(nRet), __FUNCTION__, __FILE__, __LINE__);
  104. return nRet;
  105. }
  106. }
  107. catch(TimeoutException &e)
  108. {
  109. //写日志
  110. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  111. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_SEND_TIMEOUT), __FUNCTION__, __FILE__, __LINE__);
  112. return COMMON_SEND_TIMEOUT;
  113. }
  114. catch(Exception& e)
  115. {
  116. //写日志
  117. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  118. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_SEND_FAIL), __FUNCTION__, __FILE__, __LINE__);
  119. return COMMON_SEND_FAIL;
  120. }
  121. HTTPResponse httpResponse;
  122. try
  123. {
  124. istream& responseStream = m_pSession->receiveResponse(httpResponse);
  125. nRet = HandleHttpResponse(httpResponse);
  126. if(nRet != PHMS_SUCCESSFUL_RESULT)
  127. {
  128. //写日志
  129. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(nRet), __FUNCTION__, __FILE__, __LINE__);
  130. CPhmsLogger::GetPhmsLogger()->WriteLog(httpResponse.getReason(), __FUNCTION__, __FILE__, __LINE__);
  131. return nRet;
  132. }
  133. pInputStream = &responseStream;
  134. }
  135. catch(TimeoutException& e)
  136. {
  137. //写日志
  138. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  139. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_RECV_TIMEOUT), __FUNCTION__, __FILE__, __LINE__);
  140. return COMMON_RECV_TIMEOUT;
  141. }
  142. catch(Exception& e)
  143. {
  144. //写日志
  145. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  146. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_RECV_FAIL), __FUNCTION__, __FILE__, __LINE__);
  147. return COMMON_RECV_FAIL;
  148. }
  149. return PHMS_SUCCESSFUL_RESULT;
  150. }
  151. int CHttpSession::ExecuteNoPhmsHttpSession(CNetConfig& netConfig, istream* & pInputStream, int bContinue, string stringLocalFilePath)
  152. {
  153. //if(m_pSession->connected())
  154. {
  155. m_pSession->release();
  156. }
  157. int nRet = PHMS_SUCCESSFUL_RESULT;
  158. nRet = this->SetNetWork(netConfig);
  159. if(nRet != PHMS_SUCCESSFUL_RESULT)
  160. {
  161. //写日志
  162. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(nRet), __FUNCTION__, __FILE__, __LINE__);
  163. return nRet;
  164. }
  165. m_pSession->setTimeout(Timespan(1000000*60));//设置接收超时1分钟
  166. HTTPRequest httpRequest;
  167. this->GenerateNoPhmsHttpRequest(netConfig, httpRequest, bContinue, stringLocalFilePath);
  168. try
  169. {
  170. ostream &requestStream = m_pSession->sendRequest(httpRequest);
  171. }
  172. catch(TimeoutException &e)
  173. {
  174. //写日志
  175. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  176. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_SEND_TIMEOUT), __FUNCTION__, __FILE__, __LINE__);
  177. return COMMON_SEND_TIMEOUT;
  178. }
  179. catch(Exception& e)
  180. {
  181. //写日志
  182. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  183. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_SEND_FAIL), __FUNCTION__, __FILE__, __LINE__);
  184. return COMMON_SEND_FAIL;
  185. }
  186. HTTPResponse httpResponse;
  187. try
  188. {
  189. istream& responseStream = m_pSession->receiveResponse(httpResponse);
  190. nRet = HandleHttpResponse(httpResponse);
  191. if(nRet != PHMS_SUCCESSFUL_RESULT)
  192. {
  193. //写日志
  194. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(nRet), __FUNCTION__, __FILE__, __LINE__);
  195. CPhmsLogger::GetPhmsLogger()->WriteLog(httpResponse.getReason(), __FUNCTION__, __FILE__, __LINE__);
  196. return nRet;
  197. }
  198. pInputStream = &responseStream;
  199. }
  200. catch(TimeoutException& e)
  201. {
  202. //写日志
  203. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  204. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_RECV_TIMEOUT), __FUNCTION__, __FILE__, __LINE__);
  205. return COMMON_RECV_TIMEOUT;
  206. }
  207. catch(Exception& e)
  208. {
  209. //写日志
  210. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  211. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_RECV_FAIL), __FUNCTION__, __FILE__, __LINE__);
  212. return COMMON_RECV_FAIL;
  213. }
  214. return PHMS_SUCCESSFUL_RESULT;
  215. }
  216. int CHttpSession::ExecuteFormHttpSession(CNetConfig& netConfig, map<string, string> mapParam, istream* & pInputStream)
  217. {
  218. //if(m_pSession->connected())
  219. {
  220. m_pSession->release();
  221. }
  222. int nRet = PHMS_SUCCESSFUL_RESULT;
  223. nRet = this->SetNetWork(netConfig);
  224. if(nRet != PHMS_SUCCESSFUL_RESULT)
  225. {
  226. //写日志
  227. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(nRet), __FUNCTION__, __FILE__, __LINE__);
  228. return nRet;
  229. }
  230. m_pSession->setTimeout(Timespan(1000000*60));//设置接收超时1分钟
  231. HTTPRequest httpRequest;
  232. string stringParam;
  233. this->GenerateFormHttpRequest(netConfig, mapParam, stringParam, httpRequest);
  234. if(netConfig.GetUrl().find("/chss/web/")!=string::npos || netConfig.GetUrl().find("/ytj/execute/")!=string::npos)
  235. {
  236. if(g_cookieGuanXinSessionId.getName().size() != 0)
  237. {
  238. //存在cookie:JSESSIONID=92ACC4FFBD1F1148C515DC3C2AA20667; Path=/auth-service
  239. NameValueCollection nameValueCollection;
  240. nameValueCollection.add(g_cookieGuanXinSessionId.getName(), g_cookieGuanXinSessionId.getValue());
  241. httpRequest.setCookies(nameValueCollection);
  242. }
  243. }
  244. try
  245. {
  246. ostream &requestStream = m_pSession->sendRequest(httpRequest);
  247. requestStream<<stringParam;
  248. }
  249. catch(TimeoutException &e)
  250. {
  251. //写日志
  252. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  253. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_SEND_TIMEOUT), __FUNCTION__, __FILE__, __LINE__);
  254. return COMMON_SEND_TIMEOUT;
  255. }
  256. catch(Exception& e)
  257. {
  258. //写日志
  259. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  260. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_SEND_FAIL), __FUNCTION__, __FILE__, __LINE__);
  261. return COMMON_SEND_FAIL;
  262. }
  263. HTTPResponse httpResponse;
  264. try
  265. {
  266. istream& responseStream = m_pSession->receiveResponse(httpResponse);
  267. nRet = HandleHttpResponse(httpResponse);
  268. if(nRet != PHMS_SUCCESSFUL_RESULT)
  269. {
  270. //写日志
  271. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(nRet), __FUNCTION__, __FILE__, __LINE__);
  272. CPhmsLogger::GetPhmsLogger()->WriteLog(httpResponse.getReason(), __FUNCTION__, __FILE__, __LINE__);
  273. return nRet;
  274. }
  275. if(netConfig.GetUrl().find("/chss/web/ytjlogin") != string::npos)
  276. {
  277. vector<HTTPCookie> vCookie;
  278. httpResponse.getCookies(vCookie);
  279. if(vCookie.size() != 0)
  280. {
  281. g_cookieGuanXinSessionId = vCookie[0];
  282. }
  283. }
  284. pInputStream = &responseStream;
  285. }
  286. catch(TimeoutException& e)
  287. {
  288. //写日志
  289. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  290. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_RECV_TIMEOUT), __FUNCTION__, __FILE__, __LINE__);
  291. return COMMON_RECV_TIMEOUT;
  292. }
  293. catch(Exception& e)
  294. {
  295. //写日志
  296. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  297. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_RECV_FAIL), __FUNCTION__, __FILE__, __LINE__);
  298. return COMMON_RECV_FAIL;
  299. }
  300. return PHMS_SUCCESSFUL_RESULT;
  301. }
  302. int CHttpSession::ExecuteFormHttpSession(CNetConfig& netConfig, map<string, string> mapParam, string stringLocalFilePath, istream* & pInputStream)
  303. {
  304. //if(m_pSession->connected())
  305. {
  306. m_pSession->release();
  307. }
  308. int nRet = PHMS_SUCCESSFUL_RESULT;
  309. nRet = this->SetNetWork(netConfig);
  310. if(nRet != PHMS_SUCCESSFUL_RESULT)
  311. {
  312. //写日志
  313. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(nRet), __FUNCTION__, __FILE__, __LINE__);
  314. return nRet;
  315. }
  316. m_pSession->setTimeout(Timespan(1000000*60));//设置接收超时1分钟
  317. HTTPRequest httpRequest;
  318. string stringBoundary = MultipartWriter::createBoundary();
  319. this->GenerateFormHttpRequest(netConfig, mapParam, stringLocalFilePath, stringBoundary, httpRequest);
  320. if(netConfig.GetUrl().find("/chss/web/")!=string::npos || netConfig.GetUrl().find("/ytj/execute/")!=string::npos)
  321. {
  322. if(g_cookieGuanXinSessionId.getName().size() != 0)
  323. {
  324. //存在cookie:JSESSIONID=92ACC4FFBD1F1148C515DC3C2AA20667; Path=/auth-service
  325. NameValueCollection nameValueCollection;
  326. nameValueCollection.add(g_cookieGuanXinSessionId.getName(), g_cookieGuanXinSessionId.getValue());
  327. httpRequest.setCookies(nameValueCollection);
  328. }
  329. }
  330. try
  331. {
  332. ostream &requestStream = m_pSession->sendRequest(httpRequest);
  333. //requestStream<<stringParam;
  334. nRet = SendPostMapAndFile(requestStream, stringBoundary, mapParam, stringLocalFilePath);
  335. if(nRet != PHMS_SUCCESSFUL_RESULT)
  336. {
  337. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(nRet), __FUNCTION__, __FILE__, __LINE__);
  338. return nRet;
  339. }
  340. }
  341. catch(TimeoutException &e)
  342. {
  343. //写日志
  344. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  345. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_SEND_TIMEOUT), __FUNCTION__, __FILE__, __LINE__);
  346. return COMMON_SEND_TIMEOUT;
  347. }
  348. catch(Exception& e)
  349. {
  350. //写日志
  351. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  352. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_SEND_FAIL), __FUNCTION__, __FILE__, __LINE__);
  353. return COMMON_SEND_FAIL;
  354. }
  355. HTTPResponse httpResponse;
  356. try
  357. {
  358. istream& responseStream = m_pSession->receiveResponse(httpResponse);
  359. nRet = HandleHttpResponse(httpResponse);
  360. if(nRet != PHMS_SUCCESSFUL_RESULT)
  361. {
  362. //写日志
  363. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(nRet), __FUNCTION__, __FILE__, __LINE__);
  364. CPhmsLogger::GetPhmsLogger()->WriteLog(httpResponse.getReason(), __FUNCTION__, __FILE__, __LINE__);
  365. return nRet;
  366. }
  367. if(netConfig.GetUrl().find("/chss/web/ytjlogin") != string::npos)
  368. {
  369. vector<HTTPCookie> vCookie;
  370. httpResponse.getCookies(vCookie);
  371. if(vCookie.size() != 0)
  372. {
  373. g_cookieGuanXinSessionId = vCookie[0];
  374. }
  375. }
  376. pInputStream = &responseStream;
  377. }
  378. catch(TimeoutException& e)
  379. {
  380. //写日志
  381. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  382. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_RECV_TIMEOUT), __FUNCTION__, __FILE__, __LINE__);
  383. return COMMON_RECV_TIMEOUT;
  384. }
  385. catch(Exception& e)
  386. {
  387. //写日志
  388. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  389. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_RECV_FAIL), __FUNCTION__, __FILE__, __LINE__);
  390. return COMMON_RECV_FAIL;
  391. }
  392. return PHMS_SUCCESSFUL_RESULT;
  393. }
  394. int CHttpSession::ExeceteCommonPostSession(CNetConfig& netConfig, map<string, string> mapParam, string stringContentType, string stringHttpBody, istream* & pInputStream)
  395. {
  396. //if(m_pSession->connected())
  397. {
  398. m_pSession->release();
  399. }
  400. int nRet = PHMS_SUCCESSFUL_RESULT;
  401. nRet = this->SetNetWork(netConfig);
  402. if(nRet != PHMS_SUCCESSFUL_RESULT)
  403. {
  404. //写日志
  405. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(nRet), __FUNCTION__, __FILE__, __LINE__);
  406. return nRet;
  407. }
  408. m_pSession->setTimeout(Timespan(1000000*60));//设置接收超时1分钟
  409. HTTPRequest httpRequest;
  410. this->GeneratePostHttpRequest(netConfig, mapParam, stringContentType, stringHttpBody, httpRequest);
  411. try
  412. {
  413. ostream &requestStream = m_pSession->sendRequest(httpRequest);
  414. requestStream<<stringHttpBody;
  415. }
  416. catch(TimeoutException &e)
  417. {
  418. //写日志
  419. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  420. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_SEND_TIMEOUT), __FUNCTION__, __FILE__, __LINE__);
  421. return COMMON_SEND_TIMEOUT;
  422. }
  423. catch(Exception& e)
  424. {
  425. //写日志
  426. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  427. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_SEND_FAIL), __FUNCTION__, __FILE__, __LINE__);
  428. return COMMON_SEND_FAIL;
  429. }
  430. HTTPResponse httpResponse;
  431. try
  432. {
  433. istream& responseStream = m_pSession->receiveResponse(httpResponse);
  434. nRet = HandleHttpResponse(httpResponse);
  435. if(nRet != PHMS_SUCCESSFUL_RESULT)
  436. {
  437. //写日志
  438. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(nRet), __FUNCTION__, __FILE__, __LINE__);
  439. CPhmsLogger::GetPhmsLogger()->WriteLog(httpResponse.getReason(), __FUNCTION__, __FILE__, __LINE__);
  440. return nRet;
  441. }
  442. pInputStream = &responseStream;
  443. }
  444. catch(TimeoutException& e)
  445. {
  446. //写日志
  447. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  448. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_RECV_TIMEOUT), __FUNCTION__, __FILE__, __LINE__);
  449. return COMMON_RECV_TIMEOUT;
  450. }
  451. catch(Exception& e)
  452. {
  453. //写日志
  454. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  455. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_RECV_FAIL), __FUNCTION__, __FILE__, __LINE__);
  456. return COMMON_RECV_FAIL;
  457. }
  458. return PHMS_SUCCESSFUL_RESULT;
  459. }
  460. int CHttpSession::ExecuteXmlHttpSession(CNetConfig& netConfig, string stringXmlContent, istream* & pInputStream)
  461. {
  462. //if(m_pSession->connected())
  463. {
  464. m_pSession->release();
  465. }
  466. int nRet = PHMS_SUCCESSFUL_RESULT;
  467. nRet = this->SetNetWork(netConfig);
  468. if(nRet != PHMS_SUCCESSFUL_RESULT)
  469. {
  470. //写日志
  471. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(nRet), __FUNCTION__, __FILE__, __LINE__);
  472. return nRet;
  473. }
  474. m_pSession->setTimeout(Timespan(1000000*60));//设置接收超时1分钟
  475. HTTPRequest httpRequest;
  476. string stringParam;
  477. this->GenerateXmlHttpRequest(netConfig, stringXmlContent, httpRequest);
  478. try
  479. {
  480. ostream &requestStream = m_pSession->sendRequest(httpRequest);
  481. requestStream<<stringXmlContent;
  482. }
  483. catch(TimeoutException &e)
  484. {
  485. //写日志
  486. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  487. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_SEND_TIMEOUT), __FUNCTION__, __FILE__, __LINE__);
  488. return COMMON_SEND_TIMEOUT;
  489. }
  490. catch(Exception& e)
  491. {
  492. //写日志
  493. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  494. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_SEND_FAIL), __FUNCTION__, __FILE__, __LINE__);
  495. return COMMON_SEND_FAIL;
  496. }
  497. HTTPResponse httpResponse;
  498. try
  499. {
  500. istream& responseStream = m_pSession->receiveResponse(httpResponse);
  501. nRet = HandleHttpResponse(httpResponse);
  502. if(nRet != PHMS_SUCCESSFUL_RESULT)
  503. {
  504. //写日志
  505. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(nRet), __FUNCTION__, __FILE__, __LINE__);
  506. CPhmsLogger::GetPhmsLogger()->WriteLog(httpResponse.getReason(), __FUNCTION__, __FILE__, __LINE__);
  507. return nRet;
  508. }
  509. pInputStream = &responseStream;
  510. }
  511. catch(TimeoutException& e)
  512. {
  513. //写日志
  514. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  515. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_RECV_TIMEOUT), __FUNCTION__, __FILE__, __LINE__);
  516. return COMMON_RECV_TIMEOUT;
  517. }
  518. catch(Exception& e)
  519. {
  520. //写日志
  521. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  522. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_RECV_FAIL), __FUNCTION__, __FILE__, __LINE__);
  523. return COMMON_RECV_FAIL;
  524. }
  525. return PHMS_SUCCESSFUL_RESULT;
  526. }
  527. int CHttpSession::ExeceteGetHttpSession(CNetConfig& netConfig, map<string, string> mapParam, istream* & pInputStream)
  528. {
  529. //if(m_pSession->connected())
  530. {
  531. m_pSession->release();
  532. }
  533. int nRet = PHMS_SUCCESSFUL_RESULT;
  534. nRet = this->SetNetWork(netConfig);
  535. if(nRet != PHMS_SUCCESSFUL_RESULT)
  536. {
  537. //写日志
  538. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(nRet), __FUNCTION__, __FILE__, __LINE__);
  539. return nRet;
  540. }
  541. m_pSession->setTimeout(Timespan(1000000*60));//设置接收超时1分钟
  542. HTTPRequest httpRequest;
  543. string stringParam;
  544. this->GenerateGetHttpRequest(netConfig, mapParam, httpRequest);
  545. try
  546. {
  547. ostream &requestStream = m_pSession->sendRequest(httpRequest);
  548. //requestStream<<stringParam;
  549. }
  550. catch(TimeoutException &e)
  551. {
  552. //写日志
  553. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  554. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_SEND_TIMEOUT), __FUNCTION__, __FILE__, __LINE__);
  555. return COMMON_SEND_TIMEOUT;
  556. }
  557. catch(Exception& e)
  558. {
  559. //写日志
  560. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  561. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_SEND_FAIL), __FUNCTION__, __FILE__, __LINE__);
  562. return COMMON_SEND_FAIL;
  563. }
  564. HTTPResponse httpResponse;
  565. try
  566. {
  567. istream& responseStream = m_pSession->receiveResponse(httpResponse);
  568. nRet = HandleHttpResponse(httpResponse);
  569. if(nRet != PHMS_SUCCESSFUL_RESULT)
  570. {
  571. //写日志
  572. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(nRet), __FUNCTION__, __FILE__, __LINE__);
  573. CPhmsLogger::GetPhmsLogger()->WriteLog(httpResponse.getReason(), __FUNCTION__, __FILE__, __LINE__);
  574. return nRet;
  575. }
  576. pInputStream = &responseStream;
  577. }
  578. catch(TimeoutException& e)
  579. {
  580. //写日志
  581. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  582. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_RECV_TIMEOUT), __FUNCTION__, __FILE__, __LINE__);
  583. return COMMON_RECV_TIMEOUT;
  584. }
  585. catch(Exception& e)
  586. {
  587. //写日志
  588. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  589. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_RECV_FAIL), __FUNCTION__, __FILE__, __LINE__);
  590. return COMMON_RECV_FAIL;
  591. }
  592. return PHMS_SUCCESSFUL_RESULT;
  593. }
  594. int CHttpSession::GetHttpExceptionInfo(int& nCode, string& stringMsgName, string& stringMsgText)
  595. {
  596. const Exception* p = m_pSession->networkException();
  597. if(p != NULL)
  598. nCode = p->code();
  599. stringMsgName = p->name();
  600. stringMsgText = p->message();
  601. return PHMS_SUCCESSFUL_RESULT;
  602. }
  603. int CHttpSession::AbortSession()
  604. {
  605. try
  606. {
  607. m_pSession->abort();
  608. }
  609. catch(Exception&)
  610. {
  611. //异常说明socket已经是无效的了,其效果等同于abort成功,所以异常捕获里无操作
  612. }
  613. return PHMS_SUCCESSFUL_RESULT;
  614. }
  615. int CHttpSession::SetNetWork(CNetConfig& netConfig)
  616. {
  617. if(m_bSsl != netConfig.GetSsl())
  618. {
  619. //写日志
  620. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_SSL_CONFLICT_ERROR), __FUNCTION__, __FILE__, __LINE__);
  621. return COMMON_SSL_CONFLICT_ERROR;
  622. }
  623. if(netConfig.ValidatePort()==false || netConfig.ValidAddrAndUrl()==false)
  624. {
  625. //写日志
  626. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_NET_CONFIG_ERROR), __FUNCTION__, __FILE__, __LINE__);
  627. return COMMON_NET_CONFIG_ERROR;
  628. }
  629. if(netConfig.GetPrpxyType() != NO_PROXY)
  630. {
  631. if(netConfig.ValidateProxyInfo() == false)
  632. {
  633. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_PROXY_INFO_ERROR), __FUNCTION__, __FILE__, __LINE__);
  634. return COMMON_PROXY_INFO_ERROR;
  635. }
  636. }
  637. m_pSession->setHost(netConfig.GetHost());
  638. m_pSession->setAddr(netConfig.GetAddr());
  639. m_pSession->setPort(netConfig.GetPort());
  640. if(netConfig.GetPrpxyType() == HTTP_PROXY)
  641. {
  642. m_pSession->setProxyType(HTTP_PROXY);
  643. m_pSession->setProxy(netConfig.GetProxyAddr(), netConfig.GetProxyPort());
  644. m_pSession->setProxyCredentials(netConfig.GetProxyUsername(), netConfig.GetProxyPassword());
  645. }
  646. if(netConfig.GetPrpxyType() == SOCK_PROXY)
  647. {
  648. m_pSession->setProxyType(SOCK_PROXY);
  649. m_pSession->setProxy(netConfig.GetProxyAddr(), netConfig.GetProxyPort());
  650. m_pSession->setProxyCredentials(netConfig.GetProxyUsername(), netConfig.GetProxyPassword());
  651. }
  652. return PHMS_SUCCESSFUL_RESULT;
  653. }
  654. int CHttpSession::GenerateHttpRequest(CNetConfig& netConfig, CPhmsRequest& phmsRequest, string stringBoundary, HTTPRequest& httpRequest)
  655. {
  656. httpRequest.setVersion(HTTPMessage::HTTP_1_1);
  657. httpRequest.setMethod(HTTPRequest::HTTP_POST);
  658. httpRequest.setURI(netConfig.GetUrl());
  659. httpRequest.setKeepAlive(false);
  660. httpRequest.set("pragma", "no-cache");
  661. httpRequest.set("User-Agent", g_stringTerminalType);
  662. if(phmsRequest.GetPhmsRequestHead().GetOperationCode() != "1002")
  663. {
  664. NameValueCollection nameValueCollection;
  665. nameValueCollection.add("PHPSESSID", phmsRequest.GetPhmsRequestHead().GetSessionId());
  666. httpRequest.setCookies(nameValueCollection);
  667. }
  668. if(phmsRequest.GetFilePath().size() == 0)
  669. {
  670. //无附加文件
  671. MediaType mediaType("application", "x-www-form-urlencoded");
  672. mediaType.setParameter("charset", "GB2312");
  673. httpRequest.setContentType(mediaType);
  674. string stringParameterName = "msg=";
  675. httpRequest.setContentLength(phmsRequest.ToString().size()+stringParameterName.size());
  676. }
  677. else
  678. {
  679. //有附件文件
  680. MediaType mediaType("multipart", "form-data");
  681. mediaType.setParameter("boundary", stringBoundary);
  682. httpRequest.setContentType(mediaType);
  683. //由于当前使用的Nginx版本,不支持客户端的chunked模式请求,所以暂时不使用chunked模式请求
  684. //Nginx服务器到版本1.3.9才支持chunked模式的请求
  685. //服务器添加了支持chunked请求的模块2012-12-25
  686. httpRequest.setChunkedTransferEncoding(true);
  687. //没办法,只有先获得文件大小,计算content-length的值
  688. // unsigned long nHttpBodyLen = 0;
  689. // unsigned long nFileLen = 0;
  690. // nFileLen = CUtil::GetFileSize(phmsRequest.GetFilePath());
  691. // MediaType mediaType1("");
  692. // MediaType mediaType2("");
  693. // MessageHeader messageHeader1;
  694. // MessageHeader messageHeader2;
  695. // mediaType1.setType("form-data");
  696. // mediaType1.setParameter("name", "msg");
  697. // mediaType2.setType("form-data");
  698. // mediaType2.setParameter("name", "file");
  699. // mediaType2.setParameter("filename", phmsRequest.GetFilePath());
  700. // messageHeader1.set("Content-Disposition", mediaType1.toString());
  701. // messageHeader2.set("Content-Disposition", mediaType2.toString());
  702. // ostringstream oStringStream1;
  703. // ostringstream oStringStream2;
  704. // messageHeader1.write(oStringStream1);
  705. // messageHeader2.write(oStringStream2);
  706. // string stringMessageHead1 = oStringStream1.str();
  707. // string stringMessageHead2 = oStringStream2.str();
  708. // nHttpBodyLen = nFileLen/*文件参数长度*/ + (stringBoundary.size()+2/*前缀--*/+2/*\r\n*/)*3/*分段长度*/+2/*最后一个stringBoundary多出来的--*/ + stringMessageHead1.size()+stringMessageHead2.size() +
  709. // phmsRequest.ToString().size()/*phms请求参数长度*/ + 2*2/*MessageHeader与内容之间的\r\n*/ + 2*2/*内容结束后的\r\n*/;
  710. // httpRequest.setContentLength(nHttpBodyLen);
  711. }
  712. return PHMS_SUCCESSFUL_RESULT;
  713. }
  714. int CHttpSession::GenerateNoPhmsHttpRequest(CNetConfig& netConfig, HTTPRequest& httpRequest, int bContinue, string stringLocalFilePath)
  715. {
  716. httpRequest.setVersion(HTTPMessage::HTTP_1_1);
  717. httpRequest.setMethod(HTTPRequest::HTTP_GET);
  718. httpRequest.setURI(netConfig.GetUrl());
  719. httpRequest.setKeepAlive(false);
  720. httpRequest.set("pragma", "no-cache");
  721. httpRequest.set("User-Agent", g_stringTerminalType);
  722. if(bContinue)
  723. {
  724. string stringLocalFileSize = CUtil::ULongToString(CUtil::GetFileSize(stringLocalFilePath));
  725. string stringRangeParam = "bytes=";
  726. stringRangeParam += stringLocalFileSize+"-";
  727. httpRequest.set("Range", stringRangeParam);
  728. }
  729. return PHMS_SUCCESSFUL_RESULT;
  730. }
  731. int CHttpSession::GenerateFormHttpRequest(CNetConfig& netConfig, map<string, string> mapParam, string& stringParam, HTTPRequest& httpRequest)
  732. {
  733. httpRequest.setVersion(HTTPMessage::HTTP_1_1);
  734. httpRequest.setMethod(HTTPRequest::HTTP_POST);
  735. httpRequest.setURI(netConfig.GetUrl());
  736. httpRequest.setKeepAlive(false);
  737. httpRequest.set("pragma", "no-cache");
  738. httpRequest.set("User-Agent", g_stringTerminalType);
  739. //无附加文件
  740. MediaType mediaType("application", "x-www-form-urlencoded");
  741. //mediaType.setParameter("charset", "UTF8");
  742. httpRequest.setContentType(mediaType);
  743. map<string, string>::const_iterator iter;
  744. for(iter=mapParam.begin(); iter!=mapParam.end();)
  745. {
  746. stringParam += iter->first;
  747. stringParam += "=";
  748. stringParam += iter->second;
  749. if(++iter != mapParam.end())
  750. {
  751. stringParam += "&";
  752. }
  753. }
  754. httpRequest.setContentLength(stringParam.size());
  755. return PHMS_SUCCESSFUL_RESULT;
  756. }
  757. int CHttpSession::GenerateFormHttpRequest(CNetConfig& netConfig, map<string, string> mapParam, string stringLocalFilePath, string stringBoundary, HTTPRequest& httpRequest)
  758. {
  759. httpRequest.setVersion(HTTPMessage::HTTP_1_1);
  760. httpRequest.setMethod(HTTPRequest::HTTP_POST);
  761. httpRequest.setURI(netConfig.GetUrl());
  762. httpRequest.setKeepAlive(false);
  763. httpRequest.set("pragma", "no-cache");
  764. httpRequest.set("User-Agent", g_stringTerminalType);
  765. unsigned long nFileLen = 0;
  766. if(stringLocalFilePath.size() != 0)
  767. {
  768. nFileLen = CUtil::GetFileSize(stringLocalFilePath);;
  769. }
  770. if(stringLocalFilePath.size()==0 || nFileLen==0)
  771. {
  772. //无附加文件
  773. string stringParam;
  774. MediaType mediaType("application", "x-www-form-urlencoded");
  775. //mediaType.setParameter("charset", "UTF8");
  776. httpRequest.setContentType(mediaType);
  777. map<string, string>::const_iterator iter;
  778. for(iter=mapParam.begin(); iter!=mapParam.end();)
  779. {
  780. stringParam += iter->first;
  781. stringParam += "=";
  782. stringParam += iter->second;
  783. if(++iter != mapParam.end())
  784. {
  785. stringParam += "&";
  786. }
  787. }
  788. httpRequest.setContentLength(stringParam.size());
  789. }
  790. else
  791. {
  792. //有附件文件
  793. MediaType mediaType("multipart", "form-data");
  794. mediaType.setParameter("boundary", stringBoundary);
  795. //mediaType.setParameter("charset", "UTF8");
  796. httpRequest.setContentType(mediaType);
  797. //由于不是所有的服务器都支持Chunked模式,所以此处选择Content-Length模式
  798. //httpRequest.setChunkedTransferEncoding(true);
  799. //没办法,只有先获得文件大小,计算content-length的值
  800. unsigned long nHttpBodyLen = 0;
  801. unsigned long nFileLen = 0;
  802. if(stringLocalFilePath.size() != 0)
  803. {
  804. nFileLen = CUtil::GetFileSize(stringLocalFilePath);
  805. }
  806. map<string, string>::iterator iter;
  807. for(iter=mapParam.begin(); iter!=mapParam.end(); iter++)
  808. {
  809. MediaType mediaType("");
  810. mediaType.setType("form-data");
  811. mediaType.setParameter("name", iter->first);
  812. MessageHeader messageHeader;
  813. messageHeader.set("Content-Disposition", mediaType.toString());
  814. ostringstream oStringStream;
  815. messageHeader.write(oStringStream);
  816. string stringMessageHead;
  817. stringMessageHead = oStringStream.str();
  818. nHttpBodyLen += stringBoundary.size()+2/*前缀--*/+2/*\r\n*/+stringMessageHead.size()+iter->second.size()+2/*MessageHeader与内容之间的\r\n*/+2/*内容结束后的\r\n*/;
  819. }
  820. MediaType mediaType2("");
  821. MessageHeader messageHeader2;
  822. mediaType2.setType("form-data");
  823. mediaType2.setParameter("name", "file");
  824. mediaType2.setParameter("filename", CUtil::GetFileNameFromPath(stringLocalFilePath));
  825. messageHeader2.set("Content-Disposition", mediaType2.toString());
  826. MediaType mediaType3("");
  827. mediaType3.setType("application");
  828. mediaType3.setSubType("zip");
  829. messageHeader2.set("Content-Type", mediaType3.toString());
  830. ostringstream oStringStream2;
  831. messageHeader2.write(oStringStream2);
  832. string stringMessageHead2 = oStringStream2.str();
  833. nHttpBodyLen += nFileLen+stringBoundary.size()+2/*前缀--*/+2/*\r\n*/+stringMessageHead2.size()+2/*MessageHeader与内容之间的\r\n*/+2/*内容结束后的\r\n*/;
  834. nHttpBodyLen += stringBoundary.size()+2/*前缀--*/+2/*\r\n*/+2/*最后一个stringBoundary多出来的--*/;
  835. //nHttpBodyLen = nFileLen/*文件参数长度*/ + (stringBoundary.size()+2/*前缀--*/+2/*\r\n*/)*3/*分段长度*/+2/*最后一个stringBoundary多出来的--*/ + stringMessageHead1.size()+stringMessageHead2.size() +
  836. //phmsRequest.ToString().size()/*phms请求参数长度*/ + 2*2/*MessageHeader与内容之间的\r\n*/ + 2*2/*内容结束后的\r\n*/;
  837. httpRequest.setContentLength(nHttpBodyLen);
  838. }
  839. return PHMS_SUCCESSFUL_RESULT;
  840. }
  841. int CHttpSession::GeneratePostHttpRequest(CNetConfig& netConfig, map<string, string> mapParam, string stringContentType, string stringHttpBody, HTTPRequest& httpRequest)
  842. {
  843. httpRequest.setVersion(HTTPMessage::HTTP_1_1);
  844. httpRequest.setMethod(HTTPRequest::HTTP_POST);
  845. httpRequest.setURI(netConfig.GetUrl());
  846. httpRequest.setKeepAlive(false);
  847. httpRequest.set("pragma", "no-cache");
  848. httpRequest.set("User-Agent", g_stringTerminalType);
  849. httpRequest.set("Content-Type", stringContentType);
  850. //无附加文件
  851. //MediaType mediaType("application", "x-www-form-urlencoded");
  852. //mediaType.setParameter("charset", "UTF8");
  853. //httpRequest.setContentType(mediaType);
  854. string stringParam;
  855. map<string, string>::const_iterator iter;
  856. for(iter=mapParam.begin(); iter!=mapParam.end();)
  857. {
  858. stringParam += iter->first;
  859. stringParam += "=";
  860. stringParam += iter->second;
  861. if(++iter != mapParam.end())
  862. {
  863. stringParam += "&";
  864. }
  865. }
  866. string stringUrl = netConfig.GetUrl();
  867. if(stringParam.size() != 0)
  868. {
  869. stringUrl += "?";
  870. stringUrl += stringParam;
  871. }
  872. httpRequest.setURI(stringUrl);
  873. httpRequest.setContentLength(stringHttpBody.size());
  874. return PHMS_SUCCESSFUL_RESULT;
  875. }
  876. int CHttpSession::GenerateXmlHttpRequest(CNetConfig& netConfig, string stringXmlContent, HTTPRequest& httpRequest)
  877. {
  878. httpRequest.setVersion(HTTPMessage::HTTP_1_1);
  879. httpRequest.setMethod(HTTPRequest::HTTP_POST);
  880. httpRequest.setURI(netConfig.GetUrl());
  881. httpRequest.setKeepAlive(false);
  882. httpRequest.set("pragma", "no-cache");
  883. httpRequest.set("User-Agent", g_stringTerminalType);
  884. //无附加文件
  885. MediaType mediaType("text", "xml");
  886. string stringWorkingDir = CUtil::GetCurrentAppDir();
  887. string stringIniFilePath = stringWorkingDir+"PhmsConfig.ini";
  888. AutoPtr<IniFileConfiguration> iniFile = new IniFileConfiguration(stringIniFilePath, "gbk");
  889. string stringEncoding = iniFile->getString("OTHER.Encoding", "GBK");
  890. mediaType.setParameter("charset", stringEncoding);
  891. httpRequest.setContentType(mediaType);
  892. httpRequest.setContentLength(stringXmlContent.size());
  893. return PHMS_SUCCESSFUL_RESULT;
  894. }
  895. int CHttpSession::GenerateGetHttpRequest(CNetConfig& netConfig, map<string, string> mapParam, HTTPRequest& httpRequest)
  896. {
  897. httpRequest.setVersion(HTTPMessage::HTTP_1_1);
  898. httpRequest.setMethod(HTTPRequest::HTTP_GET);
  899. httpRequest.setKeepAlive(false);
  900. httpRequest.set("pragma", "no-cache");
  901. httpRequest.set("User-Agent", g_stringTerminalType);
  902. string stringParam;
  903. map<string, string>::const_iterator iter;
  904. for(iter=mapParam.begin(); iter!=mapParam.end();)
  905. {
  906. stringParam += iter->first;
  907. stringParam += "=";
  908. stringParam += iter->second;
  909. if(++iter != mapParam.end())
  910. {
  911. stringParam += "&";
  912. }
  913. }
  914. string stringUrl = netConfig.GetUrl();
  915. if(stringParam.size() != 0)
  916. {
  917. stringUrl += "?";
  918. stringUrl += stringParam;
  919. }
  920. httpRequest.setURI(stringUrl);
  921. return PHMS_SUCCESSFUL_RESULT;
  922. }
  923. int CHttpSession::SendPhms(ostream& outputStream, string stringBoundary, CPhmsRequest& phmsRequest)
  924. {
  925. if(phmsRequest.GetFilePath().size() == 0)
  926. {
  927. //无附加文件
  928. try
  929. {
  930. outputStream.exceptions(ios::failbit|ios::badbit);
  931. outputStream<<"msg="<<phmsRequest.ToString();
  932. }
  933. catch(const ios::failure& error)
  934. {
  935. //写日志
  936. int nCode = 0;
  937. string stringMsgName;
  938. string stringMsgText;
  939. this->GetHttpExceptionInfo(nCode, stringMsgName, stringMsgText);
  940. CPhmsLogger::GetPhmsLogger()->WriteLog(stringMsgText, __FUNCTION__, __FILE__, __LINE__);
  941. CPhmsLogger::GetPhmsLogger()->WriteLog(error.what(), __FUNCTION__, __FILE__, __LINE__);
  942. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_OUTPUT_STREAM_FAIL), __FUNCTION__, __FILE__, __LINE__);
  943. return COMMON_OUTPUT_STREAM_FAIL;
  944. }
  945. catch(const TimeoutException& e)
  946. {
  947. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  948. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_SEND_TIMEOUT), __FUNCTION__, __FILE__, __LINE__);
  949. return COMMON_SEND_TIMEOUT;
  950. }
  951. catch(const Exception& e)
  952. {
  953. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  954. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_SEND_FAIL), __FUNCTION__, __FILE__, __LINE__);
  955. return COMMON_SEND_FAIL;
  956. }
  957. }
  958. else
  959. {
  960. //有附加文件
  961. MediaType mediaType1("");
  962. MediaType mediaType2("");
  963. MessageHeader messageHeader1;
  964. MessageHeader messageHeader2;
  965. mediaType1.setType("form-data");
  966. mediaType1.setParameter("name", "msg");
  967. mediaType2.setType("form-data");
  968. mediaType2.setParameter("name", "filename");
  969. mediaType2.setParameter("filename", phmsRequest.GetFilePath());
  970. messageHeader1.set("Content-Disposition", mediaType1.toString());
  971. messageHeader2.set("Content-Disposition", mediaType2.toString());
  972. try
  973. {
  974. MultipartWriter multipartWrite(outputStream, stringBoundary);
  975. multipartWrite.nextPart(messageHeader1);
  976. outputStream<<phmsRequest.ToString();
  977. multipartWrite.nextPart(messageHeader2);
  978. ifstream inputFileStream;
  979. inputFileStream.exceptions(ios::badbit);
  980. inputFileStream.open(phmsRequest.GetFilePath().c_str(), ios::in|ios::binary);
  981. if(inputFileStream.fail())
  982. {
  983. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_FILE_OPEN_FAIL), __FUNCTION__, __FILE__, __LINE__);
  984. return COMMON_FILE_OPEN_FAIL;
  985. }
  986. outputStream.exceptions(ios::failbit|ios::badbit);
  987. while(!inputFileStream.eof())
  988. {
  989. Buffer<char> bufFileContent(1024);
  990. memset(bufFileContent.begin(), 0, 1024);
  991. inputFileStream.read(bufFileContent.begin(), 1024);
  992. outputStream.write(bufFileContent.begin(), inputFileStream.gcount());
  993. }
  994. multipartWrite.close();
  995. inputFileStream.close();
  996. }
  997. catch(const ios::failure& error)
  998. {
  999. //写日志
  1000. if(outputStream.fail() || outputStream.bad())
  1001. {
  1002. int nCode = 0;
  1003. string stringMsgName;
  1004. string stringMsgText;
  1005. this->GetHttpExceptionInfo(nCode, stringMsgName, stringMsgText);
  1006. CPhmsLogger::GetPhmsLogger()->WriteLog(stringMsgText, __FUNCTION__, __FILE__, __LINE__);
  1007. }
  1008. CPhmsLogger::GetPhmsLogger()->WriteLog(error.what(), __FUNCTION__, __FILE__, __LINE__);
  1009. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_OUTPUT_STREAM_FAIL), __FUNCTION__, __FILE__, __LINE__);
  1010. return COMMON_OUTPUT_STREAM_FAIL;
  1011. }
  1012. catch(const TimeoutException& e)
  1013. {
  1014. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  1015. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_SEND_TIMEOUT), __FUNCTION__, __FILE__, __LINE__);
  1016. return COMMON_SEND_TIMEOUT;
  1017. }
  1018. catch(const Exception& e)
  1019. {
  1020. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  1021. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_SEND_FAIL), __FUNCTION__, __FILE__, __LINE__);
  1022. return COMMON_SEND_FAIL;
  1023. }
  1024. }
  1025. return PHMS_SUCCESSFUL_RESULT;
  1026. }
  1027. int CHttpSession::SendPostMapAndFile(ostream& outputStream, string stringBoundary, map<string, string> mapParam, string stringLocalFilePath)
  1028. {
  1029. if(stringLocalFilePath.size()==0 || CUtil::GetFileSize(stringLocalFilePath)==0)
  1030. {
  1031. //无附加文件
  1032. try
  1033. {
  1034. outputStream.exceptions(ios::failbit|ios::badbit);
  1035. map<string, string>::iterator iter;
  1036. int i=0;
  1037. for(iter=mapParam.begin(); iter!=mapParam.end(); iter++, i++)
  1038. {
  1039. outputStream<<iter->first<<"="<<iter->second;
  1040. if(i != mapParam.size()-1)
  1041. {
  1042. outputStream<<"&";
  1043. }
  1044. }
  1045. }
  1046. catch(const ios::failure& error)
  1047. {
  1048. //写日志
  1049. int nCode = 0;
  1050. string stringMsgName;
  1051. string stringMsgText;
  1052. this->GetHttpExceptionInfo(nCode, stringMsgName, stringMsgText);
  1053. CPhmsLogger::GetPhmsLogger()->WriteLog(stringMsgText, __FUNCTION__, __FILE__, __LINE__);
  1054. CPhmsLogger::GetPhmsLogger()->WriteLog(error.what(), __FUNCTION__, __FILE__, __LINE__);
  1055. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_OUTPUT_STREAM_FAIL), __FUNCTION__, __FILE__, __LINE__);
  1056. return COMMON_OUTPUT_STREAM_FAIL;
  1057. }
  1058. catch(const TimeoutException& e)
  1059. {
  1060. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  1061. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_SEND_TIMEOUT), __FUNCTION__, __FILE__, __LINE__);
  1062. return COMMON_SEND_TIMEOUT;
  1063. }
  1064. catch(const Exception& e)
  1065. {
  1066. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  1067. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_SEND_FAIL), __FUNCTION__, __FILE__, __LINE__);
  1068. return COMMON_SEND_FAIL;
  1069. }
  1070. }
  1071. else
  1072. {
  1073. //有附加文件
  1074. MultipartWriter multipartWrite(outputStream, stringBoundary);
  1075. int i=0;
  1076. map<string, string>::iterator iter;
  1077. for(iter=mapParam.begin(); iter!=mapParam.end(); iter++, i++)
  1078. {
  1079. MediaType mediaType1("");
  1080. MessageHeader messageHeader1;
  1081. mediaType1.setType("form-data");
  1082. mediaType1.setParameter("name", iter->first);
  1083. messageHeader1.set("Content-Disposition", mediaType1.toString());
  1084. try
  1085. {
  1086. multipartWrite.nextPart(messageHeader1);
  1087. outputStream<<iter->second;
  1088. }
  1089. catch(const TimeoutException& e)
  1090. {
  1091. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  1092. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_SEND_TIMEOUT), __FUNCTION__, __FILE__, __LINE__);
  1093. return COMMON_SEND_TIMEOUT;
  1094. }
  1095. catch(const Exception& e)
  1096. {
  1097. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  1098. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_SEND_FAIL), __FUNCTION__, __FILE__, __LINE__);
  1099. return COMMON_SEND_FAIL;
  1100. }
  1101. }
  1102. MediaType mediaType2("");
  1103. MediaType mediaType3("");
  1104. MessageHeader messageHeader2;
  1105. mediaType2.setType("form-data");
  1106. mediaType2.setParameter("name", "file");
  1107. mediaType2.setParameter("filename", CUtil::GetFileNameFromPath(stringLocalFilePath));
  1108. mediaType3.setType("application");
  1109. mediaType3.setSubType("zip");
  1110. messageHeader2.set("Content-Disposition", mediaType2.toString());
  1111. messageHeader2.set("Content-Type", mediaType3.toString());
  1112. try
  1113. {
  1114. multipartWrite.nextPart(messageHeader2);
  1115. ifstream inputFileStream;
  1116. inputFileStream.exceptions(ios::badbit);
  1117. inputFileStream.open(stringLocalFilePath.c_str(), ios::in|ios::binary);
  1118. if(inputFileStream.fail())
  1119. {
  1120. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_FILE_OPEN_FAIL), __FUNCTION__, __FILE__, __LINE__);
  1121. return COMMON_FILE_OPEN_FAIL;
  1122. }
  1123. outputStream.exceptions(ios::failbit|ios::badbit);
  1124. while(!inputFileStream.eof())
  1125. {
  1126. Buffer<char> bufFileContent(1024);
  1127. memset(bufFileContent.begin(), 0, 1024);
  1128. inputFileStream.read(bufFileContent.begin(), 1024);
  1129. outputStream.write(bufFileContent.begin(), inputFileStream.gcount());
  1130. }
  1131. inputFileStream.close();
  1132. }
  1133. catch(const ios::failure& error)
  1134. {
  1135. //写日志
  1136. if(outputStream.fail() || outputStream.bad())
  1137. {
  1138. int nCode = 0;
  1139. string stringMsgName;
  1140. string stringMsgText;
  1141. this->GetHttpExceptionInfo(nCode, stringMsgName, stringMsgText);
  1142. CPhmsLogger::GetPhmsLogger()->WriteLog(stringMsgText, __FUNCTION__, __FILE__, __LINE__);
  1143. }
  1144. CPhmsLogger::GetPhmsLogger()->WriteLog(error.what(), __FUNCTION__, __FILE__, __LINE__);
  1145. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_OUTPUT_STREAM_FAIL), __FUNCTION__, __FILE__, __LINE__);
  1146. return COMMON_OUTPUT_STREAM_FAIL;
  1147. }
  1148. catch(const TimeoutException& e)
  1149. {
  1150. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  1151. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_SEND_TIMEOUT), __FUNCTION__, __FILE__, __LINE__);
  1152. return COMMON_SEND_TIMEOUT;
  1153. }
  1154. catch(const Exception& e)
  1155. {
  1156. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  1157. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_SEND_FAIL), __FUNCTION__, __FILE__, __LINE__);
  1158. return COMMON_SEND_FAIL;
  1159. }
  1160. multipartWrite.close();
  1161. }
  1162. return PHMS_SUCCESSFUL_RESULT;
  1163. }
  1164. int CHttpSession::HandleHttpResponse(HTTPResponse& httpResponse)
  1165. {
  1166. switch(httpResponse.getStatus())
  1167. {
  1168. case HTTPResponse::HTTP_OK:
  1169. return PHMS_SUCCESSFUL_RESULT;
  1170. case HTTPResponse::HTTP_NOT_FOUND:
  1171. case HTTPResponse::HTTP_FORBIDDEN:
  1172. case HTTPResponse::HTTP_UNAUTHORIZED:
  1173. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_URL_NOT_FOUND), __FUNCTION__, __FILE__, __LINE__);
  1174. return COMMON_URL_NOT_FOUND;
  1175. default:
  1176. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_RECV_CONTENT_FAIL), __FUNCTION__, __FILE__, __LINE__);
  1177. return COMMON_RECV_CONTENT_FAIL;
  1178. }
  1179. return PHMS_SUCCESSFUL_RESULT;
  1180. }