I am writing a sample WCF Restful Service. Here am Trying to pass an object to POST Method in the service. Here goes the code
CLIENT SIDE CODE:
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.
SERVICE CODE:
[OperationContract]
[WebInvoke(UriTemplate = "/Survey", Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped)]
void InsertData(Survey SurveyItem);
Can any one help me... Thanks in advance....