The simplest call method:
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 codeThe Code is 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 (expires, 10000, expires, 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>