1
我一直有400個錯誤的響應的問題,並已經把它歸結到正在通過的XML sting。該工程的字符串是c#對象到XML,休息服務
<Data xmlns=\"http://www.eysnap.com/mPlayer\">
<Name>Chris</Name>
<Age>29</Age>
<Period>123</Period>
<msg>Why</msg>
</Data>
然而,當我嘗試seralize我得到它:
<?xml version="1.0" encoding="utf-16"?>
<Data xmlns:Namespace="http://www.eysnap.com/mPlayer">
<Name>Chris</Name>
<Age>29</Age>
<Period>123</Period>
<msg>Why</msg>
</Data>
我知道這是正確的格式,但我要麼需要XML輸出設置爲裸視或者我需要配置我的服務以接受正確的視圖。
客戶郵編:
StringBuilder s = new StringBuilder();
StringWriter writter = new StringWriter(s);
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration=true;
using(XmlWriter writer = XmlWriter.Create(writter,settings)){
XmlSerializer x = new XmlSerializer(d.GetType());
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("Namespace","http://www.eysnap.com/mPlayer");
x.Serialize(writter,d,ns);
}
string invalid = s.ToString();
string valid ="<Data xmlns=\"http://www.eysnap.com/mPlayer\"><Name>Chris</Name><Age>29</Age><Period>12 Years</Period><msg>Fucking hell</msg></Data>";
req.ContentLength = valid.Length;
var sw = new StreamWriter(req.GetRequestStream());
sw.Write(valid);
sw.Close();
res = (HttpWebResponse) req.GetResponse();
Stream responseStream = res.GetResponseStream();
var streamReader = new StreamReader(responseStream);
//Read the response into an xml document
var soapResonseXmlDocument = new XmlDocument();
soapResonseXmlDocument.LoadXml(streamReader.ReadToEnd());
//return only the xml representing the response details (inner request)
TextBox1.Text = soapResonseXmlDocument.InnerXml;
//Response.Write(soapResonseXMLDocument.InnerXml);
REST服務接口:
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Xml,
RequestFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "auth")]
ResponseData Auth(Data rData);
感謝
克里斯
絕對有幫助。它整理了命名空間問題,我只需要擺脫第一行編碼。 – user623448 2011-06-08 10:37:05
我得到它在一個非常討厭和雜亂的方式工作。我認爲它與編碼是UTF-16而不是UTF-8和包裝有關。我編輯了刺,並刪除了編碼,它的工作原理。 – user623448 2011-06-08 11:17:36