开发者

Send xml data to the WCF REST service

开发者 https://www.devze.com 2023-03-22 17:01 出处:网络
There is a self hosted WCF REST service, need to send an xml post message to it. Seems like this question was asked and answered several times, but after trying every solution I still didn`t get any s

There is a self hosted WCF REST service, need to send an xml post message to it. Seems like this question was asked and answered several times, but after trying every solution I still didn`t get any success.

Server: interface

[ServiceContract]
public interface ISDMobileService
{
    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle=WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat=WebMessageFormat.Xml)]
    int ProcessMessage(string inputXml);
}

Server: class

public class Service : ISDMobileService
{
    public int ProcessMessage(string inputXml)
    {
        Console.WriteLine( "ProcessMessage : " + inputXml );
        return 0;
     }  
}

Server: hosting

class Program
{
    static void Main(string[] args)
    {
        WebServiceHost          host    =   new WebServiceHost(typeof(Service), new Uri("http://172.16.3.4:7310"));
        WebHttpBinding          webbind = new WebHttpBinding(WebHttpSecurityMode.None);

        ServiceEndpoint         ep      = host.AddServiceEndpoint(typeof(ISDMobileService), webbind, "");
开发者_运维问答        ServiceDebugBehavior    stp     =   host.Description.Behaviors.Find<ServiceDebugBehavior>();
        stp.HttpsHelpPageEnabled    =   false;

        host.Open();
        Console.WriteLine("Service is up and running. Press 'Enter' to quit >>>");
        Console.ReadLine();

        host.Close();
    }
}

Send xml data to the WCF REST service

Request from fiddler without anything in the "Request Body" works just fine and fires break point inside ProcessMessage method of Service class, any variant of data in "Request Body", e.g.: test || <inputXml>test</inputXml> || inputXml="test" || <?xml version="1.0" encoding="UTF-8" ?><inputXml>test</inputXml> etc. gives HTTP/1.1 400 Bad Request

Will appreciate any help with this


A few things:

  • Since you're using WebServiceHost, you don't need to explicitly add the service endpoint (call to host.AddServiceEndpoint(...) in your Main.
  • The operation takes a string parameter; if you want to send it in XML, you need to wrap the string in the appropriate element. Try this body and it should work:

Body:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">This is a string encoded in XML</string>

You can also send it in different formats, such as JSON. This request should work as well

POST http://.../ProcessMessage
Host: ...
Content-Type: application/json
Content-Length: <the actual length>

"This is a string encoded in JSON"
0

精彩评论

暂无评论...
验证码 换一张
取 消