0

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....

1 Answers1

0

You have not provided details about configuration in web.config or app.config for wcf service. But I found one thing wrong in your webinvoke attribute.

Whenever You are posting request like above (Direct xml as string), you have to set bodystyle parameter to Bare as shown in below code snippet.

[OperationContract]
[WebInvoke(UriTemplate = "/Survey", Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
void InsertData(Survey SurveyItem);
Jenish Rabadiya
  • 6,708
  • 6
  • 33
  • 62