javascript调用webservice实现base64加密

本文介绍了一个使用Microsoft XML HTTP对象实现的Base64编码和解码服务。该服务通过SOAP协议调用Base64.asmx Web服务来完成字符串的Base64编码和解码过程,并详细展示了如何构建SOAP请求消息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

发表时间: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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值