jr3
jr3

Reputation: 915

RESTful WCF returns error code 415?

I am getting a {"The remote server returned an error: (415) Unsupported Media Type."} when I run a client against a RESTful web service I have created. The content types are correct in the client. The service runs correctly, under the WCF test client in vs 2010. I think my problem is within my client code or the way I'm sending/formatting the XML. Any advice is GREATLY appreciated.

Using VS2010, .net4

Web Service code:

public Charge GetCharge(Charge aCharge)
    {
        return aCharge;
    }

[ServiceContract(Namespace = "http://www.test.com/Charge")]
public interface IService1
{

    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "getcharge",
      RequestFormat = WebMessageFormat.Xml,
      ResponseFormat = WebMessageFormat.Xml,
      BodyStyle = WebMessageBodyStyle.Bare)]
    Charge GetCharge(Charge aCharge);
}


// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract(Namespace = "http://www.test.com/Charge")]
public class Charge
{
    [DataMember]
    public string ID
    {
        get;
        set;
    }

    [DataMember]
    public string Hours
    {
        get;
        set;
    }


    [DataMember]
    public string Comment
    {
        get;
        set;
    }

    [DataMember]
    public string DateT
    {
        get;
        set;
    }
}

Client code:

HttpWebRequest req = null;
        HttpWebResponse res = null;
        try
        {
            string url = "http://localhost:1657/Service1.svc/getcharge";
            req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/xml";
            req.Timeout = 30000;
            req.Headers.Add("SOAPAction", url);

            System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
            xmlDoc.XmlResolver = null;
            xmlDoc.Load(@"c:\file\benCharge.xml");
            string sXML = xmlDoc.InnerXml;
            req.ContentLength = sXML.Length;
            System.IO.StreamWriter sw = new System.IO.StreamWriter(req.GetRequestStream());
            sw.Write(sXML);
            sw.Close();

            res = (HttpWebResponse)req.GetResponse();

        }
        catch (Exception ex)
        {
            System.Console.WriteLine(ex.Message);
        }

Client XML:

<Charge xmlns="http://www.test.com/Charge"><ID>1</ID><Hours>10</Hours><Comment>Mobile app development</Comment><DateT>01/01/2011</DateT></Charge>

Upvotes: 2

Views: 3662

Answers (3)

Rajesh
Rajesh

Reputation: 7886

Can you create the same data contract on the client and rather than reading from an xml file create the object convert it to byte array and post it to the stream and see if your request succeeds.

Make sure to monitor your request with Fiddler.

I guess the reason for the error is to do with your xml file. You just need to make sure that the xml when deserialized using DataContractSerializer comes back in the same format as your object.

Some code to post using HttpWebRequest object:

byte[] requestBodyBytes = ToByteArrayUsingDataContractSer(requestBody);
                request.ContentLength = requestBodyBytes.Length;
                using (Stream postStream = request.GetRequestStream())
                    postStream.Write(requestBodyBytes, 0, requestBodyBytes.Length);


private static byte[] ToByteArrayUsingDataContractSer<T>(T requestBody)
        {
            byte[] bytes = null;
            var serializer1 = new DataContractSerializer(typeof(T));
            var ms1 = new MemoryStream();
            serializer1.WriteObject(ms1, requestBody);
            ms1.Position = 0;
            var reader = new StreamReader(ms1);
            bytes = ms1.ToArray();
            return bytes;
        }

I guess from your client you are not passing in a message as part of the message body that it needs. Hope the above information helps you.

Upvotes: 1

Yahia
Yahia

Reputation: 70379

I am a bit confused - you say that this is a RESTful service but later on in your post you show something named SOAP - SOAP and REST are NOT the same!

Use Fiddler or Wireshark to see the difference between your client and the working client and then change accordingly.

For working source code in a similar config like yours see http://social.msdn.microsoft.com/Forums/en/wcf/thread/d11a7997-d60b-4895-8f69-9ef2175087e2

Basically they have a charset added to the ContentType AND they do not use SOAPAction in the header.

Upvotes: 0

Maurice
Maurice

Reputation: 27632

Did you check the actual message headers on the wire using Fiddler? Check both the ContentType, ie what you are sending, and the Accept, what you want back in return. Either MIME type could cause this problem.

Upvotes: 0

Related Questions