123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- /**
- * HTTP 통신 클라이언트 클래스
- *
- * @author kimjihwan
- */
- function HttpClient()
- {
- this.m_objHttpFactory;
- this.m_objHttpRequest;
- };
- HttpClient.prototype.status = function ()
- {
- if (4 == this.m_objHttpRequest.readyState)
- {
- return this.m_objHttpRequest.status;
- }
- else
- {
- return null;
- }
- };
- HttpClient.prototype.post = function (strURL, objData, bAsync, strContentsType, strEncoding, bMutipart, strBoundary)
- {
- return this._sendHttpRequest("POST", strURL, objData, bAsync, strContentsType, strEncoding, bMutipart, strBoundary);
- };
- HttpClient.prototype.get = function (strURL, bAsync, strContentsType, strEncoding)
- {
- return this._sendHttpRequest("GET", strURL, null, bAsync, strContentsType, strEncoding, false, null);
- };
- HttpClient.prototype.put = function (strURL, objData, bAsync, strContentsType, strEncoding)
- {
- return this._sendHttpRequest("PUT", strURL, objData, bAsync, strContentsType, strEncoding, false, null);
- };
- HttpClient.prototype.deleteMethod = function (strURL, objData, bAsync, strContentsType, strEncoding)
- {
- return this._sendHttpRequest("DELETE", strURL, objData, bAsync, strContentsType, strEncoding, false, null);
- };
- HttpClient.prototype._sendHttpRequest = function (strMethod, strURL, objData, bAsync, strContentsType, strEncoding, bMutipart, strBoundary)
- {
- if (null == bAsync)
- {
- bAsync = false;
- }
-
- if (null == strContentsType || strContentsType.isEmpty())
- {
- strContentsType = "application/x-www-form-urlencoded";
- }
-
- if (null == strEncoding || strEncoding.isEmpty())
- {
- strEncoding = "UTF-8";
- }
-
- this.m_objHttpRequest = this._makeHttpRequest();
-
- if (bAsync)
- {
- this.m_objHttpRequest.onreadystatechange = HttpClient.handleASyncHttpResponse;
- }
-
- this.m_objHttpRequest.open(strMethod, strURL, bAsync);
-
- if (bMutipart)
- {
- this.m_objHttpRequest.setRequestHeader("Content-Type", strContentsType + "; boundary=" + strBoundary);
- }
- else
- {
- this.m_objHttpRequest.setRequestHeader("Content-Type", strContentsType + ";charset=UTF-8");
- }
-
- this.m_objHttpRequest.send(objData);
-
- if (!bAsync)
- {
- return this._handleSyncHttpResponse(strURL);
- }
- };
- /**
- * 동기 모드일때 답신을 처리한다.
- * @return
- */
- HttpClient.prototype._handleSyncHttpResponse = function (strURL)
- {
- if (4 == this.m_objHttpRequest.readyState)
- {
- if (200 == this.m_objHttpRequest.status)
- {
- if("text/html" == this.m_objHttpRequest.getResponseHeader("Content-Type"))
- {
- // TODO redirect
- }
- else
- {
- if (null != window.console)
- {
- if (!is_smartphone)
- {
- if (null != window.console)
- {
- console.log(" Connect URL : " + strURL);
- console.log(" Connect STATUS : " + this.m_objHttpRequest.status);
- console.log(" Connect STATUS TEXT : " + this.m_objHttpRequest.statusText);
- }
- }
- else
- {
- if (null != window.console)
- {
- console.log(this.m_objHttpRequest.status + "," + this.m_objHttpRequest.statusText + "," + strURL);
- }
- }
- }
- return this.m_objHttpRequest.responseText;
- }
- }
- else
- {
- if (!is_smartphone)
- {
- if (null != window.console)
- {
- console.log(" Connect URL : " + strURL);
- console.log(" Connect STATUS : " + this.m_objHttpRequest.status);
- console.log(" Connect STATUS TEXT : " + this.m_objHttpRequest.statusText);
- }
- }
- else
- {
- if (null != window.console)
- {
- console.log(this.m_objHttpRequest.status + "," + this.m_objHttpRequest.statusText + "," + strURL);
- }
- }
- return null;
- }
- }
- };
- HttpClient.prototype._makeHttpRequest = function ()
- {
- if (null != this.m_objHttpFactory)
- {
- return this.m_objHttpFactory();
- }
-
- for (var i=0; i<HttpClient._HttpFactories.length; i++)
- {
- try
- {
- var objfactory = HttpClient._HttpFactories[i];
- var objRequest = objfactory();
-
- if (null != objRequest)
- {
- this.m_objHttpFactory = objfactory;
- return objRequest;
- }
- }
- catch (e)
- {
- continue;
- }
- }
- // 여기까지 왔다면 HttpRequest 생성에 실패한 것이다.
- HttpClient._HttpFactories = function ()
- {
- throw new Error("HttpRequest not supported");
- };
- HttpClient._HttpFactories();
- };
-
- HttpClient._HttpFactories =
- [
- function () { return new XMLHttpRequest(); },
- function () { return new ActiveXObject("Msxml2.XMLHTTP"); },
- function () { return new ActiveXObject("Microsoft.XMLHTTP"); }
- ];
- /**
- * 비동기 모드일때 답신을 처리한다.
- * @return
- */
- HttpClient.handleASyncHttpResponse = function ()
- {
- if (4 == this.readyState)
- {
- if (200 == this.status)
- {
- var strResult = this.responseText;
-
- if (null != strResult || !strResult.isEmpty())
- {
- // trim을 하지 않으면 Mozilla 계열의 Browser에서 Dom을 이상하게 만든다.
- strResult = strResult.trim();
-
- // result 처리
- var xnResultDoc = XmlLib.loadXMLFromString(strResult);
- if (null != xnResultDoc)
- {
- model.updateDataForASync(XmlLib.getRootElementNode(xnResultDoc));
- }
- }
- }
- else
- {
- throw new Error(this.status);
- }
- }
- };
|