Ajax cross-domain proxy access to network resources implementation code _ Application Tips

Source: Internet
Author: User
Tags eval gettext trim
Smart.asp
Copy Code code as follows:

<script language= "JScript" runat= "Server" >
/*
Calling methods inside the VBS
Dim myhttp
Set myhttp = Smarthttp (Url,method,data); Three parameters are optional
Property:
url:string, URL address of the request
Method:string, the method of the request
Data:string, requested data
Charset:string, the requested URL returns the encoding of the data
Status:int, request the returned status code
Readystate:int, current communication status with HTTP request, 1, 2, 3, 4
Dataset:object, the requested data is appended to the Data property if it is added
DataSet Properties:
charset:string, encoding of data sent
DataSet method:
Append (Key,value,noencode): Adding data
Remove (Key): Remove a data item
Isexists (Key): Determine if a data item exists
Clear: Clears all data items
Method:
Header (HEADSTR): Set request headers, between items and values by: separating
Timeout (T1,T2,T3,T4): Setting timeout time
Send (): Sending request
Getbinary: Gets the binary data returned by the server
GetText (CharSet): Gets the specified encoded text
Getjson (CharSet): Gets the specified encoded JSON data
GetHeader (Key): Gets the response headers returned by the server
Getxml (CharSet): Gets the specified encoded XML data
*/
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=[10000,10000,10000,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.exec (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>

Use code:
Copy Code code 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
%>

Code package download
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.