我正在编写一个示例 WCF Restful 服务。这是尝试将对象传递给服务中的 POST 方法。这是代码
客户端代码:
HttpWebRequest req = null;
HttpWebResponse res = null;
string serviceurl = "localhost:63004/MySampleService.svc/Survey";
string XmlText;
req = (HttpWebRequest)WebRequest.Create(serviceurl);
req.Method = "POST";
req.ContentType = "application/xml; charset=utf-8";
req.Timeout = 30000;
req.Headers.Add(serviceurl);
var xmlDoc = new XmlDocument { XmlResolver = null };
xmlDoc.Load(Server.MapPath("Sample.xml"));
string sXml = xmlDoc.InnerXml;
req.ContentLength = sXml.Length;
var sw = new StreamWriter(req.GetRequestStream());
sw.Write(sXml);
sw.Close();
res = (HttpWebResponse)req.GetResponse(); //GETTING THE BAD REQUEST(400) ERROR here.
服务代码:
[OperationContract]
[WebInvoke(UriTemplate = "/Survey", Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped)]
void InsertData(Survey SurveyItem);
任何人都可以帮助我......提前谢谢......