Restful wcf服务错误'无法使用根名称反序列化XML主体

时间:2012-02-14 12:23:03

标签: c# .net wcf rest

我为手机应用写了一封RESFful服务。 我不确定我做错了什么?我试图用多种内容类型设置测试它,但没有运气。 来自手机应用程序的数据将以下列格式编码。 PHNhbWxwOlJlc3BvbnNlIHhtbG5zOnNhbWxwPSJ1cm46b2FzaXM6bmFtZXM6dGM6U0FNTDoyLjA6cHJvdG9jb2wiPjxzYW1scDpTdGF0dXM + PHNhbWxwOlN0YXR1c0NvZGU + aGVyVmFsdWU + PC94ZW5jOkNpcGhlckRhdGE + PC94ZW5jOkVuY3J5cHRlZERhdGE + PC9zYW1sOkVuY3J5cHRlZEFzc2VydGlvbj48L3NhbWxwOlJlc3BvbnNlPg ==“;

这是界面中的定义:

[OperationContract]
    [WebInvoke]
    String GetUserInfo(String authenticateRequest);

我收到错误:使用以下测试代码。

'无法使用根名称'Binary'和根命名空间''(对于操作'GetMobileCheckCapture'和契约('IMobileCC','http://tempuri.org/'))使用DataContractSerializer

这就是我尝试测试服务的方式: 字符串encryptedSAML =

PHNhbWxwOlJlc3BvbnNlIHhtbG5zOnNhbWxwPSJ1cm46b2FzaXM6bmFtZXM6dGM6U0FNTDoyLjA6cHJvdG9jb2wiPjxzYW1scDpTdGF0dXM + PHNhbWxwOlN0YXR1c0NvZGU + aGVyVmFsdWU + PC94ZW5jOkNpcGhlckRhdGE + PC94ZW5jOkVuY3J5cHRlZERhdGE + PC9zYW1sOkVuY3J5cHRlZEFzc2VydGlvbj48L3NhbWxwOlJlc3BvbnNlPg ==“;

 HttpWebRequest req = WebRequest.Create("http://localhost/Services/Mservice.svc/GetUserInfo") as HttpWebRequest;
            req.KeepAlive = false;
            req.Method = "POST";
            req.ContentType = "text/xml; encoding='utf-8'";
              ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] bDataToPass = encoding.GetBytes(encryptedSAML);
            req.ContentLength = bDataToPass.Length;

        using (Stream dataStream = req.GetRequestStream())
        {
            dataStream.Write(bDataToPass, 0, bDataToPass.Length);
        }

        try
        {
            using (WebResponse webresponse = req.GetResponse())
            {
                StreamReader reader = null;
                string responses = "";

                string StatusDescription = ((HttpWebResponse)webresponse).StatusDescription;

                if (((HttpWebResponse)webresponse).StatusCode != HttpStatusCode.OK)
                {
                    // Console.Write();
                }
                reader = new StreamReader(webresponse.GetResponseStream());
                responses = reader.ReadToEnd();
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.LoadXml(responses.Replace("&", "&"));
                response = xmldoc;
            }
        }
        catch (WebException e)
        {
            using (WebResponse response2 = e.Response)
            {
                HttpWebResponse httpResponse = (HttpWebResponse)response2;
                Console.WriteLine("Error code: {0}", httpResponse.StatusCode);
                using (Stream data = response2.GetResponseStream())
                {
                    string text = new StreamReader(data).ReadToEnd();
                    Console.WriteLine(text);
                }
            }
        } 

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用UTF-8编码而不是ASCII?

 HttpWebRequest req = WebRequest.Create("http://localhost/Services/Mservice.svc/GetUserInfo") as HttpWebRequest;
            req.KeepAlive = false;
            req.Method = "POST";
            req.ContentType = "text/xml; encoding='utf-8'";
              UTF8Encoding encoding = new UTF8Encoding();
            byte[] bDataToPass = encoding.GetBytes(encryptedSAML);
            req.ContentLength = bDataToPass.Length;