当前位置: 移动技术网 > IT编程>开发语言>Asp > SmartHTTP 简易HttpRequest类(ASP)

SmartHTTP 简易HttpRequest类(ASP)

2017年12月08日  | 移动技术网IT编程  | 我要评论
最简单的调用方法:
response.write smarthttp("http://www.baidu.com/").send().gettext()

复杂调用
set myhttp = smarthttp("http://www.baidu.com/s","get")
myhttp.dataset.append "wd","smarthttp"
myhttp.send()
response.write myhttp.gettext("gbk")

复制代码 代码如下:

<script language="jscript" runat="server">
function smarthttp(url,method,data){
return new _smarthttp(url,method,data);
}

function _smarthttp(url,method,data){
if(typeof method=="undefined") method="get";
if(typeof data=="undefined") data="";
method = method.touppercase();
method = method!="post" ? "get" : "post";

this.method = method;
this.url=url;
this.data=data;
this.charset="gb2312";
this.http=null;
this.headers=[];
this.status=0;
this.readystate=0;
this.content=null;
this.msg="";
this.dataset={
charset:"gb2312",
data:[],
append:function(key,value,noencode){
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)});
},
remove:function(key){
if(this.data.length<=0) return 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:function(key){
if(this.data.length<=0) return false;
for(var i=0;i<this.data.length;i++){
if(this.data[i].key==key){
return true;
}
}
return false;
},
clear:function(){
this.dataset.data=[];
}
};
}

_smarthttp.prototype.init=function(){
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=function(headstr){
if(headstr.indexof(":")>=0) this.headers.push(headstr);
};
_smarthttp.prototype.send=function(){
this.init();
var _http = this.getobj();
if(_http==null){return "";}
try{_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(key,value);
}
}
_http.send(this.data);
this.readystate = _http.readystate;
if(_http.readystate==4){
this.status = _http.status;
this.http = _http;
this.content = _http.responsebody;
}
return this;
}

_smarthttp.prototype.getbinary=function(){
return this.content;
};

_smarthttp.prototype.gettext=function(charset){
try{
return this.b2s(this.content,charset ? charset : this.charset);
}catch(ex){
this.msg = ex.description;
return "";
}
};

_smarthttp.prototype.getjson=function(charset){
try{
var _json=null;
eval("_json=(" + this.gettext(charset) + ");");
return _json;
}catch(ex){
this.msg = ex.description;
return null;
}
};

_smarthttp.prototype.getxml=function(charset){
try{
var _dom = new activexobject("msxml2.domdocument");
_dom.loadxml(this.gettext(charset).replace("&","&"));
return _dom;
}catch(ex){
this.msg = ex.description;
return null;
}
};
_smarthttp.prototype.getobj = function (){
var b=null;
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++){
try{
b= new activexobject(httplist[i]);
(function(o){
_smarthttp.prototype.getobj = function(){return new activexobject(o)};
})(httplist[i]);
return b;
}catch(ex){
//eval("this.msg = ex.description;");
}
}
return b;
};

_smarthttp.prototype.getrnd = function (){return math.random().tostring().substr(2);};

_smarthttp.prototype.b2s = function(bytsource, cset){
var objstream;
var byts;
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();
objstream = null;
return byts;
};
_smarthttp.prototype.urlencode=function(str){ return encodeuricomponent(str);};
_smarthttp.prototype.urldecode=function(str){ return decodeuricomponent(str);};
</script>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网