SmartHTTP 简易HttpRequest类(ASP),需要的朋友可以参考下。最简单的调用方法:
respuesta.escribir SmartHttp(http://www.baidu.com/).send().gettext()
复杂调用
establecer myhttp = SmartHttp(http://www.baidu.com/s,GET)
myhttp.dataset.append wd,smarthttp
mihttp.enviar()
respuesta.escribir mihttp.gettext(gbk)
复制代码代码如下:
<lenguaje de script=jscript runat=servidor>
función SmartHttp(url,método,datos){
devolver nuevo _SmartHttp(url,método,datos);
}
función _SmartHttp(url,método,datos){
if(tipo de método==indefinido) método=GET;
if(tipo de datos==indefinido) datos=;
método = método.toUpperCase();
método = método! = POST? OBTENER: PUBLICAR;
este.método = método;
this.url=url;
this.datos=datos;
this.charset=gb2312;
this.http=null;
this.headers=[];
este.estado=0;
this.readyState=0;
this.content=null;
este.msg=;
este.conjunto de datos={
juego de caracteres: gb2312,
datos:[],
agregar: función (clave, valor, sin código) {
varfn=nulo;
if(this.charset.toLowerCase()==utf-8){fn = encodeURIComponent;}else{fn = escape;}
if(noencode==true){fn=function(_str){return _str;}}
this.data.push({clave:fn(clave),valor:fn(valor)});
},
eliminar: función (tecla) {
if(this.data.length<=0) devuelve falso;
var_data=[];
for(var i=0;i<this.data.length;i++){
if(this.data[i].clave!=clave){
_data.push(this.data[i]);
}
}
this.datos = _datos;
},
existe: función (tecla) {
if(this.data.length<=0) devuelve falso;
for(var i=0;i<this.data.length;i++){
if(this.data[i].key==clave){
devolver verdadero;
}
}
devolver falso;
},
claro:función(){
this.dataset.data=[];
}
};
}
_SmartHttp.prototype.init=función(){
var datasetstr=;
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=función(headstr){
if(headstr.indexOf(:)>=0) this.headers.push(headstr);
};
_SmartHttp.prototype.send=función(){
this.init();
var _http = this.getobj();
si(_http==null){retorno;}
prueba{_http.setTimeouts(10000,10000,10000,30000);}catch(ex){}
_http.open(este.método,esta.url,falso);
if(this.headers.length>0){
for(var i=0;i<this.headers.length;i++){
var Sindex = this.headers[i].indexOf(:);
var clave = this.headers[i].substr(0,Sindex);
valor var = this.headers[i].substr(Sindex+1);
_http.setRequestHeader(clave,valor);
}
}
_http.send(esto.datos);
this.readyState = _http.readyState;
si(_http.readyState==4){
this.status = _http.status;
this.http = _http;
this.content = _http.responseBody;
}
devolver esto;
}
_SmartHttp.prototype.getbinary=función(){
devolver este contenido;
};
_SmartHttp.prototype.gettext=función(juego de caracteres){
intentar{
devolver this.b2s(this.content,charset? charset: this.charset);
}captura(ex){
this.msg = ex.descripción;
devolver ;
}
};
_SmartHttp.prototype.getjson=función(juego de caracteres){
intentar{
var _json=null;
eval(_json=( + this.gettext(juego de caracteres) + ););
devolver _json;
}captura(ex){
this.msg = ex.descripción;
devolver nulo;
}
};
_SmartHttp.prototype.getxml=función(juego de caracteres){
intentar{
var _dom = nuevo ActiveXObject(MSXML2.DOMDocument);
_dom.loadXML(this.gettext(juego de caracteres).replace(&,&));
devolver _dom;
}captura(ex){
this.msg = ex.descripción;
devolver nulo;
}
};
_SmartHttp.prototype.getobj = función (){
var b=nulo;
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++){
intentar{
b= nuevo ActiveXObject(listahttp[i]);
(función(o){
_SmartHttp.prototype.getobj = function(){devolver nuevo ActiveXObject(o)};
})(listahttp[i]);
volver b;
}captura(ex){
//eval(this.msg = ex.descripción;);
}
}
volver b;
};
_SmartHttp.prototype.getrnd = función (){return Math.random().toString().substr(2);};
_SmartHttp.prototype.b2s = función(bytSource, Cset){
varObjstream;
varbytes;
Objstream =Server.CreateObject(ADODB.Stream);
Objstream.Tipo = 1;
Objstream.Modo = 3;
Objstream.Open();
Objstream.Write(bytSource);
Objstream.Posición = 0;
Objstream.Tipo = 2;
Objstream.CharSet = Cset;
byts = Objstream.ReadText();
Objstream.Close();
Objstream = nulo;
bytes de retorno;
};
_SmartHttp.prototype.urlencode=function(str){ return encodeURIComponent(str);};
_SmartHttp.prototype.urldecode=function(str){ return decodeURIComponent(str);};
</script>