SmartHTTP et HttpRequest (ASP), ainsi que le système d'exploitation SmartHTTP :
réponse.write SmartHttp(http://www.baidu.com/).send().gettext()
复杂调用
définir monhttp = SmartHttp(http://www.baidu.com/s,GET)
monhttp.dataset.append wd,smarthttp
monhttp.send()
réponse.écrire monhttp.gettext(gbk)
复制代码代码如下 :
<langage de script=jscript runat=serveur>
fonction SmartHttp (url, méthode, données) {
renvoyer un nouveau _SmartHttp(url,méthode,données);
}
fonction _SmartHttp (url, méthode, données) {
if (type de méthode == non défini) méthode = GET ;
if(typeof data==indéfini) data=;
méthode = méthode.toUpperCase();
méthode = méthode!=POST ? OBTENIR : POST ;
this.method = méthode;
this.url=url;
this.data=données ;
this.charset=gb2312;
this.http=null ;
this.headers=[];
this.status=0 ;
this.readyState=0 ;
this.content=null ;
this.msg=;
this.dataset={
jeu de caractères : gb2312,
données:[],
append:function(clé,valeur,noencode){
var fn = nul ;
if(this.charset.toLowerCase()==utf-8){fn = encodeURIComponent;}else{fn = escape;}
if(noencode==true){fn=function(_str){return _str;}}
this.data.push({clé:fn(clé),valeur:fn(valeur)});
},
supprimer:fonction(clé){
if(this.data.length<=0) renvoie false ;
var _data=[];
pour(var i=0;i<this.data.length;i++){
si(this.data[i].key!=key){
_data.push(this.data[i]);
}
}
this.data = _data;
},
existe:fonction(clé){
if(this.data.length<=0) renvoie false ;
pour(var i=0;i<this.data.length;i++){
if(this.data[i].key==key){
renvoie vrai ;
}
}
renvoie faux ;
},
clair:fonction(){
this.dataset.data=[];
}
} ;
}
_SmartHttp.prototype.init=fonction(){
var datasetstr=;
si (this.dataset.data.length>0){
pour(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=fonction(headstr){
if(headstr.indexOf(:)>=0) this.headers.push(headstr);
} ;
_SmartHttp.prototype.send=fonction(){
this.init();
var _http = this.getobj();
si(_http==null){retour ;}
essayez{_http.setTimeouts(10000,10000,10000,30000);}catch(ex){}
_http.open(this.method,this.url,false);
si(this.headers.length>0){
pour(var i=0;i<this.headers.length;i++){
var Sindex = this.headers[i].indexOf(:);
var key = this.headers[i].substr(0,Sindex);
var valeur = this.headers[i].substr(Sindex+1);
_http.setRequestHeader(clé,valeur);
}
}
_http.send(this.data);
this.readyState = _http.readyState;
si(_http.readyState==4){
this.status = _http.status;
ceci.http = _http;
this.content = _http.responseBody;
}
rends ceci ;
}
_SmartHttp.prototype.getbinary=fonction(){
renvoie this.content ;
} ;
_SmartHttp.prototype.gettext=fonction(jeu de caractères){
essayer{
return this.b2s(this.content,charset ? charset : this.charset);
}attraper(ex){
this.msg = ex.description;
retour ;
}
} ;
_SmartHttp.prototype.getjson=fonction(jeu de caractères){
essayer{
var _json=null ;
eval(_json=( + this.gettext(charset) + ););
retourner _json ;
}attraper(ex){
this.msg = ex.description;
renvoie null ;
}
} ;
_SmartHttp.prototype.getxml=fonction(jeu de caractères){
essayer{
var _dom = new ActiveXObject(MSXML2.DOMDocument);
_dom.loadXML(this.gettext(charset).replace(&,&));
retourner _dom;
}attraper(ex){
this.msg = ex.description;
renvoie null ;
}
} ;
_SmartHttp.prototype.getobj = fonction (){
var b = nul ;
var httplist = [MSXML2.serverXMLHttp.3.0,MSXML2.serverXMLHttp,MSXML2.XMLHttp.3.0,MSXML2.XMLHttp,Microsoft.XMLHttp];
pour(var je = 0;i<=httplist.length -1;i++){
essayer{
b= nouvel ActiveXObject(httplist[i]);
(fonction(o){
_SmartHttp.prototype.getobj = function(){return new ActiveXObject(o)};
})(listehttp[i]);
retourner b ;
}attraper(ex){
//eval(this.msg = ex.description;);
}
}
retourner b ;
} ;
_SmartHttp.prototype.getrnd = function (){return Math.random().toString().substr(2);};
_SmartHttp.prototype.b2s = fonction (bytSource, Cset){
varObjstream ;
var octets ;
Objstream =Serveur.CreateObject(ADODB.Stream);
Objstream.Type = 1 ;
Objstream.Mode = 3 ;
Objstream.Open();
Objstream.Write(bytSource);
Objstream.Position = 0 ;
Objstream.Type = 2 ;
Objstream.CharSet = Cset ;
octets = Objstream.ReadText();
Objstream.Close();
Objstream = nul ;
renvoyer des octets ;
} ;
_SmartHttp.prototype.urlencode=function(str){ return encodeURIComponent(str);};
_SmartHttp.prototype.urldecode=function(str){ return decodeURIComponent(str);};
</script>