Smarthttp Simple HttpRequest Class (ASP) _ Application Skills

Source: Internet
Author: User
Tags eval gettext
Simplest method of invocation:
Response.Write smarthttp ("http://www.baidu.com/"). Send (). GetText ()

Complex call
Set myhttp = Smarthttp ("http://www.baidu.com/s", "get")
Myhttp.dataset.append "WD", "Smarthttp"
Myhttp.send ()
Response.Write Myhttp.gettext ("GBK")

Copy Code code as follows:

<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>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.