zoukankan      html  css  js  c++  java
  • javascript调用webservice实现base64加密

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

  • 相关阅读:
    KCF目标跟踪方法分析与总结
    C# 事件
    委托学习(3)
    委托学习(2)
    委托学习(1)
    本地无sqlserver服务下操作数据库 之GSQL
    unity Android 打包后读取 xml 文件
    Unity 3D 调用摄像头捕获照片 录像
    Unity 进度条3D制作(3D版)
    Unity 3D 进度条制作
  • 原文地址:https://www.cnblogs.com/yuanxiaoping_21cn_com/p/1332952.html
Copyright © 2011-2022 走看看