Code for Ajax cross-origin proxy access to network resources

Source: Internet
Author: User

Smart. asp Copy codeThe Code is as follows: <script language = "jscript" runat = "server">
/*
Call method in VBS
Dim myhttp
Set myhttp = SmartHttp (url, method, data); // all three parameters are optional.
Attribute:
Url: String, the requested URL
Method: String, REQUEST method
Data: String, request data
Charset: String, the encoding of the data returned by the request URL
Status: Int, status code returned by the request
ReadyState: Int, the current communication status of the same HTTP request, 1, 2, 3, 4
Dataset: Object: the requested data. If it is added, the data will be appended to the data attribute.
Dataset attributes:
Charset: String, encoding of sent data
Dataset method:
Append (key, value, noencode): add data
Remove (key): remove a data item
Isexists (key): determines whether a data item exists.
Clear: clear all data items
Method:
Header (headstr): sets the request header. Separate the items and values:
Timeout (t1, t2, t3, t4): sets the timeout time.
Send (): send a request
Getbinary: Get the binary data returned by the server
Gettext (charset): Get the text of the specified Encoding
Getjson (charset): obtains the json data of the specified encoding.
Getheader (key): Get the Response Header returned by the server
Getxml (charset): Get the xml data of the specified Encoding
*/
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. timeout = [hour, 10000, hour, 10000];
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 );
Return this;
};

_ SmartHttp. prototype. timeout = function (){
If (arguments. length> 4) {return this ;}
For (var I = 0; I <arguments. length; I ++ ){
If (! IsNaN (arguments [I]) {
This. timeout [I] = parseInt (arguments [I]);
}
}
Return this;
};

_ SmartHttp. prototype. send = function (){
This. init ();
Var _ http = this. getobj ();
If (_ http = null) {return this ;}
Try {
_ Http. setTimeouts (this. timeout [0], this. timeout [1], this. timeout [2], this. timeout [3]);
} 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 = parseInt (_ 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. getheader = function (key ){
If (key ){
If (key. toUpperCase () = "SET-COOKIE "){
Key = key. replace ("-","\-");
Var headers = this. http. getAllResponseHeaders ();
Var regexp = new RegExp ("\ n" + key + "\ :(. + ?) \ R "," ig ");
Var resstr = "";
While (res = regexp.exe c (headers ))! = Null ){
Var val = res [1]. trim ();
Resstr = resstr + val. substr (0, val. indexOf (";") + ";"
}
If (resstr! = ""){
Resstr = resstr. substr (0, resstr. lastIndexOf (";"));
}
Return resstr;
} Else {
Return this. http. getResponseHeader (key );
}
} Else {return this. http. getAllResponseHeaders ();}
};

_ SmartHttp. prototype. getxml = function (charset ){
Try {
Var _ dom = new ActiveXObject ("MSXML2.DOMDocument ");
_ Dom. loadXML (this. gettext (charset ));
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) {// ef bb bf, c0 fd
Var Objstream, c1, c2, c3;
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 );};
String. prototype. trim = function () {return this. replace (/(^ (\ s +) | (\ s +) $)/igm ,"");};
</Script>

Code:Copy codeThe Code is as follows: <! -- # Include file = "smart. asp" -->
<%
Response. charset = "UTF-8"
Dim url, method, data, charset
Url = Request. Form ("targeturl ")
Method = Request. Form ("method ")
Data = Request. Form ("data ")
Charset = Request. Form ("charset ")
If charset = "" then charset = "GB2312"
Response. Write SmartHttp (url, method, data). send (). gettext (charset)
Set myhttp = nothing
%>

Package and download code

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.