HttpClient.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**
  2. * HTTP 통신 클라이언트 클래스
  3. *
  4. * @author kimjihwan
  5. */
  6. function HttpClient()
  7. {
  8. this.m_objHttpFactory;
  9. this.m_objHttpRequest;
  10. };
  11. HttpClient.prototype.status = function ()
  12. {
  13. if (4 == this.m_objHttpRequest.readyState)
  14. {
  15. return this.m_objHttpRequest.status;
  16. }
  17. else
  18. {
  19. return null;
  20. }
  21. };
  22. HttpClient.prototype.post = function (strURL, objData, bAsync, strContentsType, strEncoding, bMutipart, strBoundary)
  23. {
  24. return this._sendHttpRequest("POST", strURL, objData, bAsync, strContentsType, strEncoding, bMutipart, strBoundary);
  25. };
  26. HttpClient.prototype.get = function (strURL, bAsync, strContentsType, strEncoding)
  27. {
  28. return this._sendHttpRequest("GET", strURL, null, bAsync, strContentsType, strEncoding, false, null);
  29. };
  30. HttpClient.prototype.put = function (strURL, objData, bAsync, strContentsType, strEncoding)
  31. {
  32. return this._sendHttpRequest("PUT", strURL, objData, bAsync, strContentsType, strEncoding, false, null);
  33. };
  34. HttpClient.prototype.deleteMethod = function (strURL, objData, bAsync, strContentsType, strEncoding)
  35. {
  36. return this._sendHttpRequest("DELETE", strURL, objData, bAsync, strContentsType, strEncoding, false, null);
  37. };
  38. HttpClient.prototype._sendHttpRequest = function (strMethod, strURL, objData, bAsync, strContentsType, strEncoding, bMutipart, strBoundary)
  39. {
  40. if (null == bAsync)
  41. {
  42. bAsync = false;
  43. }
  44. if (null == strContentsType || strContentsType.isEmpty())
  45. {
  46. strContentsType = "application/x-www-form-urlencoded";
  47. }
  48. if (null == strEncoding || strEncoding.isEmpty())
  49. {
  50. strEncoding = "UTF-8";
  51. }
  52. this.m_objHttpRequest = this._makeHttpRequest();
  53. if (bAsync)
  54. {
  55. this.m_objHttpRequest.onreadystatechange = HttpClient.handleASyncHttpResponse;
  56. }
  57. this.m_objHttpRequest.open(strMethod, strURL, bAsync);
  58. if (bMutipart)
  59. {
  60. this.m_objHttpRequest.setRequestHeader("Content-Type", strContentsType + "; boundary=" + strBoundary);
  61. }
  62. else
  63. {
  64. this.m_objHttpRequest.setRequestHeader("Content-Type", strContentsType + ";charset=UTF-8");
  65. }
  66. this.m_objHttpRequest.send(objData);
  67. if (!bAsync)
  68. {
  69. return this._handleSyncHttpResponse(strURL);
  70. }
  71. };
  72. /**
  73. * 동기 모드일때 답신을 처리한다.
  74. * @return
  75. */
  76. HttpClient.prototype._handleSyncHttpResponse = function (strURL)
  77. {
  78. if (4 == this.m_objHttpRequest.readyState)
  79. {
  80. if (200 == this.m_objHttpRequest.status)
  81. {
  82. if("text/html" == this.m_objHttpRequest.getResponseHeader("Content-Type"))
  83. {
  84. // TODO redirect
  85. }
  86. else
  87. {
  88. if (null != window.console)
  89. {
  90. if (!is_smartphone)
  91. {
  92. if (null != window.console)
  93. {
  94. console.log(" Connect URL : " + strURL);
  95. console.log(" Connect STATUS : " + this.m_objHttpRequest.status);
  96. console.log(" Connect STATUS TEXT : " + this.m_objHttpRequest.statusText);
  97. }
  98. }
  99. else
  100. {
  101. if (null != window.console)
  102. {
  103. console.log(this.m_objHttpRequest.status + "," + this.m_objHttpRequest.statusText + "," + strURL);
  104. }
  105. }
  106. }
  107. return this.m_objHttpRequest.responseText;
  108. }
  109. }
  110. else
  111. {
  112. if (!is_smartphone)
  113. {
  114. if (null != window.console)
  115. {
  116. console.log(" Connect URL : " + strURL);
  117. console.log(" Connect STATUS : " + this.m_objHttpRequest.status);
  118. console.log(" Connect STATUS TEXT : " + this.m_objHttpRequest.statusText);
  119. }
  120. }
  121. else
  122. {
  123. if (null != window.console)
  124. {
  125. console.log(this.m_objHttpRequest.status + "," + this.m_objHttpRequest.statusText + "," + strURL);
  126. }
  127. }
  128. return null;
  129. }
  130. }
  131. };
  132. HttpClient.prototype._makeHttpRequest = function ()
  133. {
  134. if (null != this.m_objHttpFactory)
  135. {
  136. return this.m_objHttpFactory();
  137. }
  138. for (var i=0; i<HttpClient._HttpFactories.length; i++)
  139. {
  140. try
  141. {
  142. var objfactory = HttpClient._HttpFactories[i];
  143. var objRequest = objfactory();
  144. if (null != objRequest)
  145. {
  146. this.m_objHttpFactory = objfactory;
  147. return objRequest;
  148. }
  149. }
  150. catch (e)
  151. {
  152. continue;
  153. }
  154. }
  155. // 여기까지 왔다면 HttpRequest 생성에 실패한 것이다.
  156. HttpClient._HttpFactories = function ()
  157. {
  158. throw new Error("HttpRequest not supported");
  159. };
  160. HttpClient._HttpFactories();
  161. };
  162. HttpClient._HttpFactories =
  163. [
  164. function () { return new XMLHttpRequest(); },
  165. function () { return new ActiveXObject("Msxml2.XMLHTTP"); },
  166. function () { return new ActiveXObject("Microsoft.XMLHTTP"); }
  167. ];
  168. /**
  169. * 비동기 모드일때 답신을 처리한다.
  170. * @return
  171. */
  172. HttpClient.handleASyncHttpResponse = function ()
  173. {
  174. if (4 == this.readyState)
  175. {
  176. if (200 == this.status)
  177. {
  178. var strResult = this.responseText;
  179. if (null != strResult || !strResult.isEmpty())
  180. {
  181. // trim을 하지 않으면 Mozilla 계열의 Browser에서 Dom을 이상하게 만든다.
  182. strResult = strResult.trim();
  183. // result 처리
  184. var xnResultDoc = XmlLib.loadXMLFromString(strResult);
  185. if (null != xnResultDoc)
  186. {
  187. model.updateDataForASync(XmlLib.getRootElementNode(xnResultDoc));
  188. }
  189. }
  190. }
  191. else
  192. {
  193. throw new Error(this.status);
  194. }
  195. }
  196. };