简体   繁体   English

使用XML响应创建Web服务

[英]Create a web service with XML response

What is the easiest way to create a web service with XML response? 用XML响应创建Web服务的最简单方法是什么?

  1. Use WCF to create the web service? 使用WCF创建Web服务? (seems really complicated) (似乎真的很复杂)
  2. If i want to use WCF to create my web service, where do I start? 如果要使用WCF创建Web服务,该从哪里开始?

The easiest way to create a web service with an XML response is, no kidding, to put an XML file on a standard web server and serve it as a static file. 创建带有XML响应的Web服务的最简单方法是,无需开玩笑,将XML文件放在标准Web服务器上并将其作为静态文件使用。

I'm guessing you want something more flexible than that, though... 我猜你想要比这更灵活的东西,但是...

You've got several options, and WCF is at the more complex (but flexible) end of the spectrum. 您有几个选择,而WCF处于频谱的更复杂(但更灵活)的一端。 First question: what's your client? 第一个问题:您的客户是什么? Are you writing it? 你在写吗? Do you want to write a web service that can be consumed by other clients? 您是否要编写可供其他客户端使用的Web服务?

Do you want to use REST -- ie plain-old-XML (POX) over plain-old-HTTP? 您是否要使用REST-即,在纯旧HTTP上使用纯旧XML(POX)? XML-RPC? XML-RPC? SOAP? 肥皂?

WCF supports all of these, so this really depends on which clients you want to support. WCF支持所有这些功能,因此,这实际上取决于您要支持的客户端。

Update: If you want to support XML-RPC, you could do worse than start with this implementation of XML-RPC for WCF by Clemens Vasters . 更新:如果要支持XML-RPC,可能比Clemens Vasters的WCF XML-RPC的实现开始时做的还差。 I asked a question about this here . 在这里问了一个问题。

Few links are available in this article. 本文中的链接很少。 Hope they will help you - 希望他们能帮助您-

http://social.msdn.microsoft.com/Forums/en/wcf/thread/b082d6de-d1e9-4e51-a0ab-0fe98d7003e6 http://social.msdn.microsoft.com/Forums/en/wcf/thread/b082d6de-d1e9-4e51-a0ab-0fe98d7003e6

In your case, I would definitely use WCF with the REST binding ( webHttpBinding ) - and I would disagree about it being complicated to learn. 在您的情况下,我肯定会将WCF与REST绑定( webHttpBinding )一起使用-我不同意它的学习复杂性。

Check out these resources to get started: 查看这些资源以开始使用:

I just made a web service. 我刚刚做了一个网络服务。

PHP server side code: PHP服务器端代码:

<?php // instantiate SOAP server
function sendXmlMsg($msg){
return $msg;
}
ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSD
$server = new SoapServer("mark.wsdl");
// Register exposed method
$server->addFunction('sendXmlMsg'); // generate captcha
//$server->addFunction('check_captcha'); // check captcha ID
$server->handle(); //?>

My WSDL file is 我的WSDL文件是

<?xml version ='1.0' encoding ='UTF-8' ?>
<definitions name='Msg91'
  targetNamespace='http://localhost/webtest/test.wsdl'
  xmlns:tns='http://localhost/webtest/test.wsdl'
  xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
  xmlns:xsd='http://www.w3.org/2001/XMLSchema'
  xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'
  xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
  xmlns='http://schemas.xmlsoap.org/wsdl/'>



<message name='sendXmlMsgRequest'>
  <part name='msg' type='xsd:string'/>
</message>
<message name='sendXmlMsgResponse'>
  <part name='Result' type='xsd:string'/>
</message>
<portType name='Msg91PortType'>
  <operation name='sendXmlMsg'>
    <input message='tns:sendXmlMsgRequest'/>
    <output message='tns:sendXmlMsgResponse'/>
  </operation>
</portType>

<binding name='Msg91Binding' type='tns:Msg91PortType'>
  <soap:binding style='rpc'
    transport='http://schemas.xmlsoap.org/soap/http'/>
    <operation name='sendXmlMsg'>
    <soap:operation soapAction='urn:xmethods-delayed-quotes#sendXmlMsg'/>
    <input>
      <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
        encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
    </input>
    <output>
      <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
        encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
    </output>
  </operation>
</binding>

<service name='Msg91Service'>
  <port name='Msg91Port' binding='tns:Msg91Binding'>
    <soap:address location='http://localhost/webtest/test.php'/>
  </port>
</service>
</definitions>

Client side PHP file: 客户端PHP文件:

<?php
$client = new SoapClient("mark.wsdl");
$params= array('HiT');
echo $client->__soapCall( 'sendXmlMsg', $params );
?>

I hope this will help you. 我希望这能帮到您。

It's actually pretty easy to create a WCF service. 创建WCF服务实际上很容易。 There are plenty of tutorials online. 在线上有很多教程。

As for returning xml, there are a few ways. 至于返回xml,有几种方法。 You can do this with an 'old school' SOAP web service by converting the xml to a string in the service and then convert back in the client. 您可以通过将xml转换为服务中的字符串,然后再将其转换回客户端来使用“老式”的SOAP Web服务来实现。 It's not pretty but it works. 它不是很漂亮,但是可以。

An alternative, and the way I'd do it, would be to use WCF and create a data contract that maps your xml. 一种替代方法是,我将使用WCF并创建一个映射XML的数据协定。

You can do some pretty good stuff with data contracts, like pass round datasets and custom types but this can sometimes limit the binding types you can use. 您可以使用数据协定做一些相当不错的工作,例如传递数据集和自定义类型,但这有时会限制您可以使用的绑定类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM