javascript調用webservice實現base64加密

來源:互聯網
上載者:User

發表時間: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, '&#38;');
    value = value.replace(/</g, '&#x003E;');    //&gt;
    value = value.replace(/>/g, '&lt;');
    value = value.replace(//g, '&#x00AE;');
    value = value.replace(//g, '&#x2122;');
    return value;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.