SmartHTTP 簡單的HttpRequest類別(ASP),需要的朋友可以參考下。
response.write SmartHttp(http://www.baidu.com/).send().gettext()
複雜調用
設定 myhttp = SmartHttp(http://www.baidu.com/s,GET)
myhttp.dataset.append wd,smarthttp
myhttp.send()
response.write myhttp.gettext(gbk)
複製程式碼如下:
<腳本語言=jscript runat=伺服器>
函數 SmartHttp(url,方法,資料){
返回新的 _SmartHttp(url,方法,資料);
}
函數 _SmartHttp(url,方法,資料){
if(方法類型==未定義) 方法=GET;
if(資料型別==未定義) 資料=;
方法 = method.toUpperCase();
方法 = 方法!取得:發布;
this.method = 方法;
這個.url=url;
this.data=數據;
this.charset=gb2312;
this.http=null;
this.headers=[];
這個.狀態=0;
this.readyState=0;
this.content=null;
這個.msg=;
這個.資料集={
字元集:gb2312,
數據:[],
附加:函數(鍵,值,無編碼){
var fn=null;
if(this.charset.toLowerCase()==utf-8){fn =encodeURIComponent;}else{fn = escape;}
if(noencode==true){fn=function(_str){return _str;}}
this.data.push({key:fn(key),value:fn(value)});
},
刪除:函數(鍵){
if(this.data.length<=0) 回傳 false;
var _data=[];
for(var i=0;i<this.data.length;i++){
if(this.data[i].key!=key){
_data.push(this.data[i]);
}
}
this.data = _data;
},
isexists:函數(鍵){
if(this.data.length<=0) 回傳 false;
for(var i=0;i<this.data.length;i++){
if(this.data[i].key==key){
返回真;
}
}
返回假;
},
清除:函數(){
this.dataset.data=[];
}
};
}
_SmartHttp.prototype.init=函數(){
var 資料集str=;
if(this.dataset.data.length>0){
for(var i=0;i<this.dataset.data.length;i++){
datasetstr += this.dataset.data[i].key + = + this.dataset.data[i].value + &;
}
}
if(datasetstr!=) datasetstr = datasetstr.substr(0,datasetstr.length-1);
if(this.data==){this.data = datasetstr;}else{if(datasetstr!=)this.data+= & + datasetstr;}
if(this.data==)this.data=null;
this.url += ((this.url.indexOf(?)<0) ? ? : &) + jornd= + this.getrnd();
if(this.method==GET && this.data!=null) this.url += & + this.data;
if(this.method==POST) this.headers.push(Content-Type:application/x-www-form-urlencoded);
if(!this.charset || this.charset==) this.charset = gb2312;
};
_SmartHttp.prototype.header=函數(headstr){
if(headstr.indexOf(:)>=0) this.headers.push(headstr);
};
_SmartHttp.prototype.send=函數(){
this.init();
var _http = this.getobj();
if(_http==null){返回;}
試試{_http.setTimeouts(10000,10000,10000,30000);}catch(ex){}
_http.open(this.method,this.url,false);
if(this.headers.length>0){
for(var i=0;i<this.headers.length;i++){
var Sindex = this.headers[i].indexOf(:);
var key = this.headers[i].substr(0,Sindex);
var value = this.headers[i].substr(Sindex+1);
_http.setRequestHeader(鍵,值);
}
}
_http.send(this.data);
this.readyState = _http.readyState;
if(_http.readyState==4){
this.status = _http.status;
this.http = _http;
this.content = _http.responseBody;
}
返回這個;
}
_SmartHttp.prototype.getbinary=函數(){
返回此內容;
};
_SmartHttp.prototype.gettext=函數(字元集){
嘗試{
回傳 this.b2s(this.content,charset ? charset : this.charset);
}抓住(前){
this.msg = ex.描述;
返回 ;
}
};
_SmartHttp.prototype.getjson=函數(字元集){
嘗試{
var_json=null;
eval(_json=( + this.gettext(charset) + ););
返回_json;
}抓住(前){
this.msg = ex.描述;
返回空值;
}
};
_SmartHttp.prototype.getxml=函數(字元集){
嘗試{
var _dom = new ActiveXObject(MSXML2.DOMDocument);
_dom.loadXML(this.gettext(charset).replace(&,&));
返回_dom;
}抓住(前){
this.msg = ex.描述;
返回空值;
}
};
_SmartHttp.prototype.getobj = 函數(){
var b=空;
var httplist = [MSXML2.serverXMLHttp.3.0,MSXML2.serverXMLHttp,MSXML2.XMLHttp.3.0,MSXML2.XMLHttp,Microsoft.XMLHttp];
for(var i = 0;i<=httplist.length -1;i++){
嘗試{
b= new ActiveXObject(httplist[i]);
(函數(o){
_SmartHttp.prototype.getobj = function(){傳回新的ActiveXObject(o)};
})(httplist[i]);
返回b;
}抓住(前){
//eval(this.msg = ex.description;);
}
}
返回b;
};
_SmartHttp.prototype.getrnd = function (){return Math.random().toString().substr(2);};
_SmartHttp.prototype.b2s = 函數(bytSource, Cset){
var 物件流;
var 位元組;
Objstream =Server.CreateObject(ADODB.Stream);
Objstream.Type = 1;
Objstream.Mode = 3;
Objstream.Open();
Objstream.Write(bytSource);
Objstream.Position = 0;
Objstream.Type = 2;
Objstream.CharSet = Cset;
byts = Objstream.ReadText();
Objstream.Close();
物件流=空;
返回位元組;
};
_SmartHttp.prototype.urlencode=function(str){ returnencodeURIComponent(str);};
_SmartHttp.prototype.urldecode=函數(str){ 返回decodeURIComponent(str);};
</腳本>