将xml数据发送到WCF REST服务

时间:2011-07-25 23:28:02

标签: wcf rest

有一个自托管的WCF REST服务,需要向它发送一条xml post消息。 似乎这个问题被问了几遍,但在尝试了每一个解决方案之后,我仍然没有取得任何成功。

服务器:界面

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

服务器:类

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

服务器:托管

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();
    }
}

fiddler request

来自fiddler的请求在“Request Body”中没有任何内容工作正常并且在Service Class的ProcessMessage方法内触发断点,“Request Body”中的任何数据变体, 例如:test || &LT; inputXml&GT;试验&LT; / inputXml&GT; || inputXml =“test”|| &lt;?xml version =“1.0”encoding =“UTF-8”?&gt;&lt; inputXml&gt; test&lt; / inputXml&gt;提供HTTP / 1.1 400错误请求

欢迎任何帮助

1 个答案:

答案 0 :(得分:3)

一些事情:

  • 由于您使用的是WebServiceHost,因此您无需在主网站中明确添加服务端点(致电{1}}。
  • 该操作采用host.AddServiceEndpoint(...)参数;如果要以XML格式发送它,则需要将字符串包装在适当的元素中。尝试这个身体,它应该工作:

体:

string

您也可以使用不同的格式发送它,例如JSON。此请求也应该有效

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