POST http://localhost:51348/TestService.svc/SendCredentials

POST http://localhost:51348/TestService.svc/SendCredentials 400 (Bad Request)

本文关键字:svc SendCredentials TestService localhost http POST 51348      更新时间:2023-09-26

我已经被这个问题困了将近两天了,我似乎没有找到任何解决方案。我在我的机器上托管了一个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变量,我将非常感激。

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

将_publicKey转换为字符串,然后使用

Convert.ToBase64String(byte[] inArray)

在服务端做相反的

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