将xml数据发送到WCF REST服务
有一个自托管的WCF REST服务,需要向其发送xml post消息。 这个问题似乎被问了好几次,回答了好几次,但在尝试了每一个解决方案后,我仍然没有取得任何成功 服务器:接口将xml数据发送到WCF REST服务,wcf,rest,Wcf,Rest,有一个自托管的WCF REST服务,需要向其发送xml post消息。 这个问题似乎被问了好几次,回答了好几次,但在尝试了每一个解决方案后,我仍然没有取得任何成功 服务器:接口 [ServiceContract] public interface ISDMobileService { [OperationContract] [WebInvoke(Method = "POST", BodyStyle=WebMessageBodyStyle.Bare, RequestFormat =
[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();
}
}
类程序
{
静态void Main(字符串[]参数)
{
WebServiceHost主机=新的WebServiceHost(服务类型),新的Uri(“http://172.16.3.4:7310"));
WebHttpBinding webbind=新的WebHttpBinding(WebHttpSecurityMode.None);
ServiceEndpoint ep=host.AddServiceEndpoint(typeof(ISDMobileService),webbind,“”;
ServiceDebugBehavior stp=host.Description.Behaviors.Find();
stp.httpsheppageenabled=false;
host.Open();
Console.WriteLine(“服务已启动并正在运行。按“回车”退出>>>”;
Console.ReadLine();
host.Close();
}
}
来自fiddler的请求在“请求正文”中没有任何内容,工作正常,并在服务类的ProcessMessage方法内触发断点,“请求正文”中的任何数据变量,
e、 例如:test | | test | | inputXml=“test”| | test等给出HTTP/1.1400错误请求
非常感谢您在这方面提供的任何帮助以下几点:
- 由于您使用的是
,因此不需要在Main中显式添加服务端点(调用WebServiceHost
)host.AddServiceEndpoint(…)
- 该操作采用
参数;如果要以XML格式发送,则需要将字符串包装到适当的元素中。请尝试此正文,它应该可以工作:string
这是一个用XML编码的字符串
你也可以用不同的格式发送,比如JSON。这个请求也应该可以
POST http://.../ProcessMessage
Host: ...
Content-Type: application/json
Content-Length: <the actual length>
"This is a string encoded in JSON"
POSThttp://.../ProcessMessage
主持人:。。。
内容类型:application/json
内容长度:
“这是一个用JSON编码的字符串”
POST http://.../ProcessMessage
Host: ...
Content-Type: application/json
Content-Length: <the actual length>
"This is a string encoded in JSON"