Answered by:
HTTP Request with Content type text/xml in wcf service

Question
-
User649217265 posted
We have created a WCF service and we used the POST method, as a request format with text/xml.We want to change the content-type of request as text/xml, but when we are changing the content type to text/xml we are getting an error '400 Bad Request'
My Interface method,
[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Xml, RequestFormat=WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, Method = "POST")]
string Payment_Notification(Stream fileContents);End Point in Web.Config File,
<system.serviceModel>
<services>
<service name="WcfRestBased.Service" behaviorConfiguration="myServiceBehavior">
<endpoint name="webHttpBinding" address="" binding="webHttpBinding" contract="IService" behaviorConfiguration="web"/>
<endpoint name="mexHttpBinding" address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services><behaviors>
<serviceBehaviors>
<behavior name="myServiceBehavior">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true" />
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false" />
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
<serviceThrottling maxConcurrentCalls="500" maxConcurrentSessions="500" maxConcurrentInstances="500" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp automaticFormatSelectionEnabled="false" />
</behavior>
</endpointBehaviors>
</behaviors>Thursday, April 18, 2013 12:23 PM
Answers
-
User-742633084 posted
Hi tapan10,Try not to explicitly specify request message format for your service operation (via attributes), just leave it default. Then , post arbitray data (with proper content-type header set in client-side webrequest code) to see if it works. You can follow the following the approach mentioned in the following blog article:
#WCF "Raw" programming model (Web) - receiving arbitrary data
http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, April 25, 2013 2:32 AM
All replies
-
User-1662538993 posted
Can you post the code for how you are calling the service with post method. How you are doing the request?
You can use fiddler to test that it is working or not-
You can use fiddler to send the request and check.
Thursday, April 18, 2013 2:15 PM -
User649217265 posted
Here is my code, we are consuming using asp.net
protected void GetData()
{
try
{
string message = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><PaymentNotificationRequest><Payments><Payment><PaymentLogId>32436</PaymentLogId><CustReference>0079</CustReference><Amount>20000</Amount><PaymentMethod>card</PaymentMethod><PaymentReference>FBN|ATM|2000111|99999</PaymentReference><TerminalId>1390999</TerminalId><ChannelName>ATM</ChannelName><Location>Abuja MegaPlaza</Location><PaymentDate>2010-05-01</PaymentDate><InstitutionId>006</InstitutionId><InstitutionName>Mtn Nigeria Limited</InstitutionName><BranchName>Ahmedu Bello way Victoria Island</BranchName><BankName>Oceanic Bank</BankName><CustomerName>John Bello</CustomerName><OtherCustomerInfo>NIgerian</OtherCustomerInfo><ReceiptNo>7843</ReceiptNo><CollectionsAccount>090990333823</CollectionsAccount><BankCode>FBN</BankCode><CustomerAddress>9 Moloko Close Santos Layout</CustomerAddress><CustomerPhoneNumber>08033729051</CustomerPhoneNumber><DepositorName>Ope Adeoye</DepositorName><DepositSlipNumber>3434</DepositSlipNumber><PaymentCurrency>566</PaymentCurrency><PaymentItems><PaymentItem><ItemName></ItemName><ItemCode></ItemCode><ItemAmount></ItemAmount></PaymentItem><PaymentItem><ItemName></ItemName><ItemCode></ItemCode><ItemAmount></ItemAmount></PaymentItem></PaymentItems></Payment></Payments></PaymentNotificationRequest>";
byte[] reqData = Encoding.UTF8.GetBytes(message);
HttpWebRequest req = null;
HttpWebResponse res = null;
string url = "http://localhost:51147/WCFRestBased/Service.svc/Payment_Notification";
req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "text/xml;charset=utf-8";
req.Accept = "application/soap+xml,text/xml,*/*";
req.Timeout = 30000;
req.ContentLength = reqData.Length;
Stream postStream = req.GetRequestStream();
postStream.Write(reqData, 0, reqData.Length);
postStream.Close();
res = (HttpWebResponse)req.GetResponse(); // Getting error in this line
}
catch (Exception ex)
{
}
}Thursday, April 18, 2013 2:29 PM -
User-742633084 posted
Hi tapan10,Try not to explicitly specify request message format for your service operation (via attributes), just leave it default. Then , post arbitray data (with proper content-type header set in client-side webrequest code) to see if it works. You can follow the following the approach mentioned in the following blog article:
#WCF "Raw" programming model (Web) - receiving arbitrary data
http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, April 25, 2013 2:32 AM