【问题标题】:POST http://localhost:51348/TestService.svc/SendCredentials 400 (Bad Request)POST http://localhost:51348/TestService.svc/SendCredentials 400(错误请求)
【发布时间】:2015-07-06 10:09:21
【问题描述】:

我已经被这个问题困扰了将近两天了,我似乎没有找到任何解决方案。我在我的机器上托管了一个 WCF 服务,其中包含一个方法 SendCredentials,它接受两个字符串参数。

现在我应该向我的服务发送一个公钥,它将通过它进行加密(非对称加密)并将一些信息发送回客户端。

我无法将该公钥从客户端传递给服务方法,因为它是 XML 格式的。这是我的客户端代码:

    $(document).ready(function () {
        $("#btnSend").click(function () {
            debugger;
            jQuery.support.cors = true;

            var doOaepPadding = true;
            var rsa = new System.Security.Cryptography.RSACryptoServiceProvider();
            _privateKey = rsa.ToXmlString(true);
            _publicKey = rsa.ToXmlString(false);
            var data = $("#txtName").val();
            var name = "testvalue";

            var _privateKey = rsa.ToXmlString(true);
            **var _publicKey = rsa.ToXmlString(false);**

            //<![CDATA[ and ]]>;
            $.ajax({

                type: "POST",

                url: 'http://localhost:51348/TestService.svc/SendCredentials',
               crossDomain: true,

                   data:JSON.stringify({ mac: "bac", pubKey: _publicKey }),

                contentType: "application/json",
                dataType: "json",

                success: function (result) {
                    var ans = JSON.stringify(result);

                    alert(ans);

                    //   result = new XMLSerializer().serializeToString(result.documentElement);

                },
                error: function (xhr, err) {
                    alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
                    alert("responseText: " + xhr.responseText);
                }
            });
            return false;

        });

    });

</script>

_publicKey 是我要传递但抛出上述错误的变量。任何建议我如何传递这个 XML 变量将不胜感激。

【问题讨论】:

    标签: javascript web-services wcf


    【解决方案1】:

    我建议你将 _publicKey 转换为 base64 字符串

    将您的 _publicKey 转换为字符串,然后转换为字节数组并使用

    Convert.ToBase64String(byte[] inArray)
    

    在服务端做相反的事情

    System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(endodedString), true));
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多