Problems when performing HTTP Post to WCF Service (Error 415: Cannot process the message because of content type mismatch)

nkewney
nkewney used Ask the Experts™
on
Hi Everyone,

I'm having real problems posting data to a WCF service. I would be very grateful if anybody could help me diagnose the error I'm receiving below.

The remote server returned an error: (415) Cannot process the message because the content type 'application/x-www-form-urlencoded' was not the expected type 'text/xml; charset=utf-8'..

I'm sending data to the service via a simple HTTP post as content type = "application/x-www-form-urlencoded".

My WCF interface and config is pasted below.
[ServiceContract]
public interface ITextService
{
[WebInvoke(UriTemplate = "invoke", Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
[OperationContract]
string PostData(Stream input);
}


[OperationBehavior]
public string PostData(Stream input)
{......



<?xml version="1.0"?>
<configuration>
<system.web>
<webServices>
<protocols>
<add name="HttpPost" />
</protocols>
</webServices>
<compilation debug="true"/>
<customErrors mode="Off">

</customErrors>
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's 
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="WebConfiguration"
maxBufferSize="65536"
maxReceivedMessageSize="2000000000"
transferMode="Streamed">
</binding>
</webHttpBinding>
<basicHttpBinding>
<binding name="StreamedHttp" transferMode="Streamed" maxBufferSize="2147483647" MaxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="SimFreeGateway.TextService" behaviorConfiguration="SimFreeGatewayBehavior">
<endpoint address="" binding="basicHttpBinding" contract="SimFreeGateway.ITextService" bindingConfiguration="StreamedHttp" bindingNamespace="xxx" />
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="WebBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="WebBehavior">
<serviceDebug httpHelpPageEnabled="false" httpsHelpPageEnabled="false" />
<serviceMetadata />
</behavior>
<behavior name="SimFreeGatewayBehavior">
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Carlos VillegasFull Stack .NET Developer

Commented:
Hello, I think that you need to use a WebHttpBinding endpoint instead of BasicHttpBinding, I did that some time agon, more info:

The Web Programming Model allows developers to expose Web services through HTTP requests that use "plain old XML" (POX) style messaging instead of SOAP-based messaging. For clients to communicate with a service using HTTP requests, an endpoint of the service must be configured with the WebHttpBinding that has the WebHttpBehavior attached to it. The WCF Web Programming Model also requires that the individual service operations are annotated with the WebGetAttribute or WebInvokeAttribute attributes. This defines a mapping from a URI and HTTP method to the service operation, as well as the format of the messages used to call the operation and return the results. Support in for syndication and ASP.AJAX integration are both built on top of the WCF Web Programming Model.
Source:
http://msdn.microsoft.com/en-us/library/system.servicemodel.webhttpbinding.aspx
Full Stack .NET Developer
Commented:
Try changing this line in your config:
<endpoint address="" binding="basicHttpBinding" contract="SimFreeGateway.ITextService" bindingConfiguration="StreamedHttp" bindingNamespace="xxx" />

Open in new window

For this:
<endpoint address="" binding="webHttpBinding" contract="SimFreeGateway.ITextService" bindingConfiguration="WebConfiguration" bindingNamespace="xxx" />

Open in new window

Author

Commented:
This solved my problem and was an oversight on my part.

Thanks for your help
Carlos VillegasFull Stack .NET Developer

Commented:
Good to know buddy

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial