response.write SmartHttp("http://www.51frw.cn/").send().gettext()
复杂调用
set myhttp = SmartHttp("http://www.51frw.cn/s","GET")
myhttp.dataset.append "wd","smarthttp"
myhttp.send()
response.write myhttp.gettext("gbk")
代码如下:
<BR /><script language="jscript" runat="server"> <BR />function SmartHttp(url,method,data){ <BR />return new _SmartHttp(url,method,data); <BR />} <br /><br />function _SmartHttp(url,method,data){ <BR />if(typeof method=="undefined") method="GET"; <BR />if(typeof data=="undefined") data=""; <BR />method = method.toUpperCase(); <BR />method = method!="POST" ? "GET" : "POST"; <br /><br />this.method = method; <BR />this.url=url; <BR />this.data=data; <BR />this.charset="gb2312"; <BR />this.http=null; <BR />this.headers=[]; <BR />this.status=0; <BR />this.readyState=0; <BR />this.content=null; <BR />this.msg=""; <BR />this.dataset={ <BR />charset:"gb2312", <BR />data:[], <BR />append:function(key,value,noencode){ <BR />var fn=null; <BR />if(this.charset.toLowerCase()=="utf-8"){fn = encodeURIComponent;}else{fn = escape;} <BR />if(noencode==true){fn=function(_str){return _str;}} <BR />this.data.push({"key":fn(key),"value":fn(value)}); <BR />}, <BR />remove:function(key){ <BR />if(this.data.length<=0) return false; <BR />var _data=[]; <BR />for(var i=0;i<this.data.length;i++){ <BR />if(this.data[i].key!=key){ <BR />_data.push(this.data[i]); <BR />} <BR />} <BR />this.data = _data; <BR />}, <BR />isexists:function(key){ <BR />if(this.data.length<=0) return false; <BR />for(var i=0;i<this.data.length;i++){ <BR />if(this.data[i].key==key){ <BR />return true; <BR />} <BR />} <BR />return false; <BR />}, <BR />clear:function(){ <BR />this.dataset.data=[]; <BR />} <BR />}; <BR />} <br /><br />_SmartHttp.prototype.init=function(){ <BR />var datasetstr=""; <BR />if(this.dataset.data.length>0){ <BR />for(var i=0;i<this.dataset.data.length;i++){ <BR />datasetstr += this.dataset.data[i].key + "=" + this.dataset.data[i].value + "&"; <BR />} <BR />} <BR />if(datasetstr!="") datasetstr = datasetstr.substr(0,datasetstr.length-1); <BR />if(this.data==""){this.data = datasetstr;}else{if(datasetstr!="")this.data+= "&" + datasetstr;} <BR />if(this.data=="")this.data=null; <BR />this.url += ((this.url.indexOf("?")<0) ? "?" : "&") + "jornd=" + this.getrnd(); <BR />if(this.method=="GET" && this.data!=null) this.url += "&" + this.data; <BR />if(this.method=="POST") this.headers.push("Content-Type:application/x-www-form-urlencoded"); <BR />if(!this.charset || this.charset=="") this.charset = "gb2312"; <BR />}; <br /><br />_SmartHttp.prototype.header=function(headstr){ <BR />if(headstr.indexOf(":")>=0) this.headers.push(headstr); <BR />}; <BR />_SmartHttp.prototype.send=function(){ <BR />this.init(); <BR />var _http = this.getobj(); <BR />if(_http==null){return "";} <BR />try{_http.setTimeouts(10000,10000,10000,30000);}catch(ex){} <BR />_http.open(this.method,this.url,false); <BR />if(this.headers.length>0){ <BR />for(var i=0;i<this.headers.length;i++){ <BR />var Sindex = this.headers[i].indexOf(":"); <BR />var key = this.headers[i].substr(0,Sindex); <BR />var value = this.headers[i].substr(Sindex+1); <BR />_http.setRequestHeader(key,value); <BR />} <BR />} <BR />_http.send(this.data); <BR />this.readyState = _http.readyState; <BR />if(_http.readyState==4){ <BR />this.status = _http.status; <BR />this.http = _http; <BR />this.content = _http.responseBody; <BR />} <BR />return this; <BR />} <br /><br />_SmartHttp.prototype.getbinary=function(){ <BR />return this.content; <BR />}; <br /><br />_SmartHttp.prototype.gettext=function(charset){ <BR />try{ <BR />return this.b2s(this.content,charset ? charset : this.charset); <BR />}catch(ex){ <BR />this.msg = ex.description; <BR />return ""; <BR />} <BR />}; <br /><br />_SmartHttp.prototype.getjson=function(charset){ <BR />try{ <BR />var _json=null; <BR />eval("_json=(" + this.gettext(charset) + ");"); <BR />return _json; <BR />}catch(ex){ <BR />this.msg = ex.description; <BR />return null; <BR />} <BR />}; <br /><br />_SmartHttp.prototype.getxml=function(charset){ <BR />try{ <BR />var _dom = new ActiveXObject("MSXML2.DOMDocument"); <BR />_dom.loadXML(this.gettext(charset).replace("&","&")); <BR />return _dom; <BR />}catch(ex){ <BR />this.msg = ex.description; <BR />return null; <BR />} <BR />}; <BR />_SmartHttp.prototype.getobj = function (){ <BR />var b=null; <BR />var httplist = ["MSXML2.serverXMLHttp.3.0","MSXML2.serverXMLHttp","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp","Microsoft.XMLHttp"]; <BR />for(var i = 0;i<=httplist.length -1;i++){ <BR />try{ <BR />b= new ActiveXObject(httplist[i]); <BR />(function(o){ <BR />_SmartHttp.prototype.getobj = function(){return new ActiveXObject(o)}; <BR />})(httplist[i]); <BR />return b; <BR />}catch(ex){ <BR />//eval("this.msg = ex.description;"); <BR />} <BR />} <BR />return b; <BR />}; <br /><br />_SmartHttp.prototype.getrnd = function (){return Math.random().toString().substr(2);}; <br /><br />_SmartHttp.prototype.b2s = function(bytSource, Cset){ <BR />var Objstream; <BR />var byts; <BR />Objstream =Server.CreateObject("ADODB.Stream"); <BR />Objstream.Type = 1; <BR />Objstream.Mode = 3; <BR />Objstream.Open(); <BR />Objstream.Write(bytSource); <BR />Objstream.Position = 0; <BR />Objstream.Type = 2; <BR />Objstream.CharSet = Cset; <BR />byts = Objstream.ReadText(); <BR />Objstream.Close(); <BR />Objstream = null; <BR />return byts; <BR />}; <BR />_SmartHttp.prototype.urlencode=function(str){ return encodeURIComponent(str);}; <BR />_SmartHttp.prototype.urldecode=function(str){ return decodeURIComponent(str);}; <BR /></script> <BR />
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!