📄 httpclient.js
字号:
// Description: js.net.http.HttpClient 类
// Author: Changhua.Wan
// Version: 2004.01.26.01
_package("js.net.http");
_import("js.activex.MsxmlFactory");
_import("js.net.http.Request");
_import("js.net.http.Response");
_import("js.net.http.HttpException");
function js.net.http.HttpClient() {
this.url = "";
this.method = "GET";
this.async = false;
// * 默认的Request和Response,可重定义
//* Request实现接口 getContentType,toString
//* Response实现接口 parse
this.request = new js.net.http.Request();
this.response = new js.net.http.Response();
this.xmlHttp = MsxmlFactory.getXmlHttp();
this.onCompleted = null;
this.parsed = false; // for 兼容早版本xmlhttp
var _self = this;
this.xmlHttp.onReadyStateChange = function() {
if (_self.xmlHttp.readyState == 4) {
if (!_self.parsed) {
_self.response.parse(_self.xmlHttp);
_self.parsed = true;
}
if (typeof(_self.onCompleted) != "function") return;
_self.onCompleted(_self.response);
}
}
}
var _p = js.net.http.HttpClient._extends("js.lang.Object");
var _c = js.net.http.HttpClient;
_p.open = function() {
try {
this.xmlHttp.open(this.method, this.url, this.async);
this.xmlHttp.setRequestHeader('CONTENT-TYPE', this.request.getContentType());
if (this.method.toLowerCase() == 'get')
this.xmlHttp.send();
else
this.xmlHttp.send(this.request.toData());
if (!this.async) {
if (this.xmlHttp.status == 200 || this.xmlHttp.status == 0) {
// for 早版本xmlhttp
if (!this.parsed) {
this.response.parse(this.xmlHttp);
this.parsed = true;
}
return this.response;
} else {
_throw (new js.net.http.HttpException(" "
+ this.className
+ " "
+ this.method
+ " "
+ this.url
+ "请求失败 [HTTP-STATUS:"
+ this.xmlHttp.status
+ "]"
));
}
}
} catch(ex) {
_throw (new js.net.http.HttpException(" "
+ this.className
+ " open 异常:"
+ ex.description
));
}
};
_p.doPost = function() {
this.method = "POST";
return this.open();
};
_p.doGet = function() {
this.method = "GET";
return this.open();
};
_p.doHead = function() {
this.method = "HEAD";
return this.open();
};
_p.abort = function() {
this.xmlHttp.Abort();
};
static:{ // 静态方法
_c.ifModifiedSince = function(_url, _date) {
var xmlHttp = MsxmlFactory.getXmlHttp();
xmlHttp.open("HEAD", _url, false);
xmlHttp.setRequestHeader('If-Modified-Since', _date.toGMTString());
xmlHttp.send();
if (xmlHttp.status == 200 || xmlHttp.status == 0) return true;
if (xmlHttp.status == 304) return false;
_throw (new js.net.http.HttpException(
"js.net.http.HttpClient.ifModifiedSince()"
+ this.url
+ " 请求失败 [HTTP-STATUS:"
+ xmlHttp.status
+ "]"
));
};
_c.exist = function(_url) {
var xmlHttp = MsxmlFactory.getXmlHttp();
xmlHttp.open("HEAD", _url, false);
xmlHttp.send();
return ( xmlHttp.status == 200 || xmlHttp.status == 0 );
};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -