Web Service实践之——开始XFire(2)

我们看到我们的WebService已经布署成功了,我们再看看它的WSDL信息:

Xml代码 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <wsdl:definitions targetNamespace="http://cn.com.pansky/SayHiService" xmlns:tns="http://cn.com.pansky/SayHiService" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">  
  3.   <wsdl:types>  
  4. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://cn.com.pansky/SayHiService">  
  5. <xsd:element name="sayHi">  
  6. <xsd:complexType>  
  7. <xsd:sequence>  
  8. <xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="xsd:string"/>  
  9. </xsd:sequence>  
  10. </xsd:complexType>  
  11. </xsd:element>  
  12. <xsd:element name="sayHiResponse">  
  13. <xsd:complexType>  
  14. <xsd:sequence>  
  15. <xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string"/>  
  16. </xsd:sequence>  
  17. </xsd:complexType>  
  18. </xsd:element>  
  19. </xsd:schema>  
  20.   </wsdl:types>  
  21.   <wsdl:message name="sayHiRequest">  
  22.     <wsdl:part name="parameters" element="tns:sayHi">  
  23.     </wsdl:part>  
  24.   </wsdl:message>  
  25.   <wsdl:message name="sayHiResponse">  
  26.     <wsdl:part name="parameters" element="tns:sayHiResponse">  
  27.     </wsdl:part>  
  28.   </wsdl:message>  
  29.   <wsdl:portType name="SayHiServicePortType">  
  30.     <wsdl:operation name="sayHi">  
  31.       <wsdl:input name="sayHiRequest" message="tns:sayHiRequest">  
  32.     </wsdl:input>  
  33.       <wsdl:output name="sayHiResponse" message="tns:sayHiResponse">  
  34.     </wsdl:output>  
  35.     </wsdl:operation>  
  36.   </wsdl:portType>  
  37.   <wsdl:binding name="SayHiServiceHttpBinding" type="tns:SayHiServicePortType">  
  38.     <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>  
  39.     <wsdl:operation name="sayHi">  
  40.       <wsdlsoap:operation soapAction=""/>  
  41.       <wsdl:input name="sayHiRequest">  
  42.         <wsdlsoap:body use="literal"/>  
  43.       </wsdl:input>  
  44.       <wsdl:output name="sayHiResponse">  
  45.         <wsdlsoap:body use="literal"/>  
  46.       </wsdl:output>  
  47.     </wsdl:operation>  
  48.   </wsdl:binding>  
  49.   <wsdl:service name="SayHiService">  
  50.     <wsdl:port name="SayHiServiceHttpPort" binding="tns:SayHiServiceHttpBinding">  
  51.       <wsdlsoap:address location="http://localhost/stove/services/SayHiService"/>  
  52.     </wsdl:port>  
  53.   </wsdl:service>  
  54. </wsdl:definitions>  

 这个文件跟我们用Axis生成的基本是一样的。

5、享受美味的时刻
WebService这道大餐算是烹制好了,现在是享用美餐的时候了。
我们写一个客户端吃掉这道大餐:

Java代码 
  1. package cn.com.pansky.webservice.xfire.study;  
  2.   
  3. import java.net.MalformedURLException;  
  4. import java.util.Map;  
  5.   
  6. import org.codehaus.xfire.client.Client;  
  7. import org.codehaus.xfire.client.XFireProxyFactory;  
  8. import org.codehaus.xfire.service.Service;  
  9. import org.codehaus.xfire.service.binding.ObjectServiceFactory;  
  10. import org.codehaus.xfire.transport.http.CommonsHttpMessageSender;  
  11.   
  12. public class SayHiClient{  
  13.   public static void main(String args[]) {  
  14.     String serviceURL = "http://localhost/stove/services/SayHiService";  
  15.     Service serviceModel = new ObjectServiceFactory().create(SayHiService.class,null,"http://cn.com.pansky/SayHiService",null);  
  16.   
  17.     XFireProxyFactory serviceFactory = new XFireProxyFactory();  
  18.   
  19.     try{  
  20.       SayHiService service = (SayHiService) serviceFactory.create(serviceModel, serviceURL);  
  21.       Client client = Client.getInstance(service);  
  22.       //client.addOutHandler(new OutHeaderHandler());  
  23.   
  24.       // disable timeout  
  25.       client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, "1");  
  26.   
  27.       String hello = service.sayHi("张山疯");  
  28.       System.out.println("服务器对[张山疯] 的回答是:" + hello );  
  29.   
  30.       hello = service.sayHi(null);  
  31.       System.out.println("服务器胡言乱语说:" + hello );  
  32.   
  33.     } catch (MalformedURLException e) {  
  34.       e.printStackTrace();  
  35.     }  
  36.   }  
  37. }  

 编译这个类,再执行一下,服务器服务的结果是:

2008-5-22 17:39:17 org.apache.commons.httpclient.HttpMethodBase writeRequest
信息: 100 (continue) read timeout. Resume sending the request
2008-5-22 17:39:17 org.apache.commons.httpclient.HttpMethodBase readResponse
信息: Discarding unexpected response: HTTP/1.1 100 Continue
服务器对[张山疯] 的回答是:张山疯, 你吃了吗?没吃回家吃去吧。
2008-5-22 17:39:20 org.apache.commons.httpclient.HttpMethodBase writeRequest
信息: 100 (continue) read timeout. Resume sending the request
2008-5-22 17:39:20 org.apache.commons.httpclient.HttpMethodBase readResponse
信息: Discarding unexpected response: HTTP/1.1 100 Continue
服务器胡言乱语说:连名字也不肯告诉我吗?

好了,WebService布署成功。

下课!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值