发表时间:2007-4-23 17:23:00
function Base64Service() {
//Msxml2.XMLHTTP.3.0
//Msxml2.XMLHTTP.5.0
this.__xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
this.__xmlDoc = new ActiveXObject("MSXML2.DOMDocument");
this.__base64EncodeUrl = "./Base64.asmx";
this.__base64EncodeMethodName = "EncodeUrl";
this.__base64EncodeParameterName = "url";
this.__base64DecodeMethodName = "DecodeUrl";
this.__base64DecodeParameterName = "url";
}
Base64Service.prototype.Encode = function(value) {
if (this.__xmlHttp == null || this.__xmlDoc == null) {
alert('method: Encode;error: xmlHttp or xmlDoc is null!');
return '';
}
var data;
data = '<?xml version="1.0" encoding="utf-8"?>';
data = data + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.blog.com.cn/http://www.w3.org/2001/XMLSchema" xmlns:soap="http://www.blog.com.cn/http://schemas.xmlsoap.org/soap/envelope/">';
data = data + '<soap:Body>';
data = data + '<' + this.__base64EncodeMethodName + ' xmlns="http://tempuri.org/">';
data = data + '<' + this.__base64EncodeParameterName + '>' + encodeXml(value) + '</' + this.__base64EncodeParameterName + '>';
data = data + '</' + this.__base64EncodeMethodName + '>';
data = data + '</soap:Body>';
data = data + '</soap:Envelope>';
try {
this.__xmlHttp.Open("POST", this.__base64EncodeUrl, false);
this.__xmlHttp.SetRequestHeader("Content-Type","text/xml; charset=gb2312");
this.__xmlHttp.SetRequestHeader("SOAPAction","http://tempuri.org/" + this.__base64EncodeMethodName);
this.__xmlHttp.Send(data);
this.__xmlDoc.loadXML(this.__xmlHttp.responseText);
var node = this.__xmlDoc.documentElement;
return node.text;
} catch (e) {
alert('method: Encode64;error: ' + e.message);
return '';
}
}
Base64Service.prototype.Decode = function(value)
{
if (this.__xmlHttp == null || this.__xmlDoc == null) {
alert('method: Decode;error: xmlHttp or xmlDoc is null!');
return '';
}
var data;
data = '<?xml version="1.0" encoding="utf-8"?>';
data = data + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.blog.com.cn/http://www.w3.org/2001/XMLSchema" xmlns:soap="http://www.blog.com.cn/http://schemas.xmlsoap.org/soap/envelope/">';
data = data + '<soap:Body>';
data = data + '<' + this.__base64DecodeMethodName + ' xmlns="http://www.blog.com.cn/http://tempuri.org/">';
data = data + '<' + this.__base64DecodeParameterName + '>'+value + '</' + this.__base64DecodeParameterName + '>';
data = data + '</' + this.__base64DecodeMethodName + '>';
data = data + '</soap:Body>';
data = data + '</soap:Envelope>';
try {
this.__xmlHttp.Open("POST", this.__base64EncodeUrl, false);
this.__xmlHttp.SetRequestHeader("Content-Type","text/xml; charset=gb2312");
this.__xmlHttp.SetRequestHeader("SOAPAction","http://tempuri.org/" + this.__base64DecodeMethodName);
this.__xmlHttp.Send(data);
this.__xmlDoc.loadXML(this.__xmlHttp.responseText);
var node = this.__xmlDoc.documentElement;
return node.text;
} catch (e) {
alert('method: Decode64;error: ' + e.message);
return '';
}
}
//替换xml中不支持的字符
function encodeXml(value) {
value = value.replace(/&/g, '&');
value = value.replace(/</g, '>'); //>
value = value.replace(/>/g, '<');
value = value.replace(/®/g, '®');
value = value.replace(/™/g, '™');
return value;
}