/**
*
* @File: HttpURLConnection.js
* @Create: 2010-11-17
* @Author: hoodng@gmail.com
*/
j$package("js.net");
js.net.HttpURLConnection = function(){
var _xhr, _timeout = 30000;
var _check = function(){
if(typeof _xhr === "undefined")
throw new Error("The XMLHttpRequest is null");
};
this.getAllResponseHeaders = function(){
_check();
return _xhr.getAllResponseHeaders();
};
this.getResponseHeader = function(key){
_check();
return _xhr.getResponseHeader(key);
};
this.setRequestHeader = function(key, value){
_check();
_xhr.setRequestHeader(key, value);
};
this.responseText = function(){
_check();
return _xhr.responseText;
};
this.responseXML = function(){
_check();
return _xhr.responseXML;
};
this.status = function(){
_check();
return _xhr.status;
};
this.readyState = function(){
_check();
return _xhr.readyState;
};
this.statusText = function(){
_check();
return _xhr.statusText;
};
this.getTimeout = function(){
return _timeout;
};
this.setTimeout = function(timeout){
_timeout = timeout;
};
this.close = function(){
if(this.__timer__ != undefined){
clearTimeout(this.__timer__);
this.__timer__ = undefined;
}
_xhr = undefined;
};
var _makeQueryString = function(params){
if(params == null) return null;
var buf = new js.lang.StringBuffer();
for(var p in params){
buf.append(p).append("=").append(params[p]).append("&");
}
buf.append("_=").append(new Date().getTime());
return buf.toString();
};
var _onreadystatechange = function(xhr, thi$, callback, errback){
if(xhr.readyState == 4){
if(this.__timer__ != undefined){
clearTimeout(this.__timer__);
this.__timer__ = undefined;
}
if(xhr.status == 200 || xhr.status == 304){
callback.call(thi$, this);
}else{
if(typeof errback == "function"){
errback.call(thi$, this);
}
}
}
};
var _ontimeout = function(xhr, thi$, tmoutback){
xhr.abort();
xhr = undefined;
if(typeof tmoutback == "function"){
tmoutback.call(thi$);
}
};
/**
* @param url
* @param params, VO of query string
* @param thi$, callback scope
* @param callback, success callback
* @param errback, error callback
* @param tmoutback, timeout callback
* If did not specify thi$, callback and errback, this method is
* synchronized
*/
this.post = function(url, params, thi$, callback, errback, tmoutback){
_check();
var query = _makeQueryString(params), async = false;
if(typeof callback === "function"){
async = true;
_xhr.onreadystatechange = _onreadystatechange.$bind(
this, false, _xhr, thi$, callback, errback, tmoutback);
}
this.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
if(async && typeof tmoutback == "function"){
js.lang.Thread.$yield(this, _ontimeout, _timeout, _xhr, thi$, tmoutback);
}
try{
_xhr.open("POST", url, async);
_xhr.send(query);
} catch (x) {
this.exception = x;
if(typeof errback == "function"){
errback.call(thi$, this);
}
this.close();
}
};
/**
* @see this.post(url, params, thi$, callback, errback, tmoutback)
*/
this.get = function(url, params, thi$, callback, errback, tmoutback){
_check();
var query = _makeQueryString(params), async = false;
if(query != null){
url += ("?" + query);
}
if(typeof callback === "function"){
async = true;
_xhr.onreadystatechange = _onreadystatechange.$bind(
this, false, _xhr, thi$, callback, errback, tmoutback);
}
try{
_xhr.open("GET", url, async);
_xhr.send(null);
} catch (x) {
this.exception = x;
if(typeof errback == "function"){
errback.call(thi$, this);
}
this.close();
}
};
_xhr = (typeof window.XMLHttpRequest != "undefined") ?
new XMLHttpRequest() : function(){
var _ = js.net.HttpURLConnection;
if(_.PROGID != undefined){
return new ActiveXObject(_.PROGID);
}else{
for (var i = 0; i < _.PROGIDS.length; i++) {
_.PROGID = _.PROGIDS[i];
try {
return new ActiveXObject(_.PROGID);
} catch (ex) {
// Nothing to do
}
}
return undefined;
}
}();
}.$extend(js.lang.Object);
js.net.HttpURLConnection.PROGID = undefined;
js.net.HttpURLConnection.PROGIDS = ["MSXML2.XMLHTTP.6.0",
"MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
|