Restful wcf服务错误';无法反序列化根名称为的XML正文
本文关键字:正文 XML 反序列化 错误 服务 wcf Restful | 更新日期: 2023-09-27 18:24:21
我为一个手机应用程序编写了一个RESFful服务。我不确定我做错了什么?我试着用多种内容类型设置来测试它,但没有成功。来自手机应用程序的数据将以以下格式编码。PHNhbWxwOlJlc3BvbnNlIHhtbG5zOnNhbWxrwPSJ1cm46b2FzaXM6bmFtZXM6dGM6U0FNTDoyLjA6cHJvdG9jb2wJxzYW1scDpTdGF0dXM+PHNhbWxwOlN0YXR1c0NvZGU+aGVyVmFsdWU+PC94ZW5jOkNpcGhlckRhdGE+PC94ZW5jOkVuY3J5cHRlZERhdGE+PC9zYW1sOkVuY3jjHRlZEFzc2VydGlvbj 48L3NhbWxwOlJlc3BvbnPlPg==";
这是界面中的定义:
[OperationContract]
[WebInvoke]
String GetUserInfo(String authenticateRequest);
我得到错误:与以下测试代码。
"无法反序列化根名称为"Binary"和根命名空间为"的XML正文(对于操作"GetMobileCheckCapture"和协定("MobileCC","http://tempuri.org/'))使用DataContractSerializer
以下是我尝试测试服务的方式:字符串加密SAML=
PHNhbWxwOlJlc3BvbnNlIHhtbG5zOnNhbWxrwPSJ1cm46b2FzaXM6bmFtZXM6dGM6U0FNTDoyLjA6cHJvdG9jb2wJxzYW1scDpTdGF0dXM+PHNhbWxwOlN0YXR1c0NvZGU+aGVyVmFsdWU+PC94ZW5jOkNpcGhlckRhdGE+PC94ZW5jOkVuY3J5cHRlZERhdGE+PC9zYW1sOkVuY3jjHRlZEFzc2VydGlvbj 48L3NhbWxwOlJlc3BvbnPlPg==";
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);
}
}
}
您尝试过使用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;