Search code examples
c#.netwcfiishttp-status-code-415

.Net WCF 415 Missing Content Type


I runnning into a problem I am unable to fix. I have found some related questions on SO, but none have helped me so far.

This is the code that is running:

HttpWebRequest handler = (HttpWebRequest)WebRequest.Create("someUrl");
handler.Method = "POST";

string postData = "some data";

byte[] data = Encoding.ASCII.GetBytes(postData);
handler.ContentLength = data.Length;
using (var stream = handler.GetRequestStream())
{
    stream.Write(data, 0, data.Length);
}
handler.ContentType = "application/xml; charset=utf-8";
HttpWebResponse response = (HttpWebResponse)handler.GetResponse();

The endpoint (url) is a WCF REST interface. When I run this code against the test REST interface, it works fine.

However when I run it against the live REST interface, I get this error:

The remote server returned an error: (415) Missing Content Type.`

Now the REST interface is the same on both endpoints. However there are 2 differences that may affect the outcome:

First, the live REST runs over https and has

<security mode="Transport"/>

in the web.config under the webHttpBinding section.

Second, the test REST server is up to date concerning Windows (.Net) updates), the live REST server not.

Does anyone has a clue how to solve this problem?

Thanks in advance!


Solution

  • Ok, found it using Fiddler: when this line

    using (var stream = handler.GetRequestStream())
    

    is being executed the connection is already setup, so when I later set the Content Type, its too late.

    When I move this line of code almost to the top

    handler.ContentType = "application/xml; charset=utf-8";
    

    its working and I can see the Content Type being transferred.

    It doesn't answer the quesion why initially it worked on the test environment and not on the live, but for now..