用HTTP GET调用web service

下面的网址提供了国内飞机航班时刻表 WEB 服务
http://webservice.webxml.com.cn/webservices/DomesticAirline.asmx

wsdl如下:
http://webservice.webxml.com.cn/webservices/DomesticAirline.asmx?wsdl

调用web service可以采用很多方式 比如axis2、xfire、cxf都可以根据wsdl反向生成客户端代码 简化开发

下面我采用的是http get的方式调用其web service,采用的是HttpClient4
Java代码 复制代码
  1. import java.net.URLEncoder;    
  2.   
  3. import org.apache.http.client.HttpClient;    
  4. import org.apache.http.client.ResponseHandler;    
  5. import org.apache.http.client.methods.HttpGet;    
  6. import org.apache.http.impl.client.BasicResponseHandler;    
  7. import org.apache.http.impl.client.DefaultHttpClient;    
  8. import org.apache.http.params.HttpProtocolParams;    
  9.   
  10. /**   
  11. * @author Tony Shen   
  12.  
  13. */    
  14. public class DomesticAirlineTest {    
  15.         public static void main(String[] args) throws Exception{       
  16.         String url = "/webservices/DomesticAirline.asmx/getDomesticAirlinesTime";       
  17.         String host = "www.webxml.com.cn";    
  18.         String param = "startCity="+URLEncoder.encode("北京""utf-8")+"&lastCity="+URLEncoder.encode("上海""utf-8")+"&theDate=&userID=";    
  19.             
  20.         HttpClient httpclient = new DefaultHttpClient();    
  21.         httpclient.getParams().setParameter(HttpProtocolParams.HTTP_CONTENT_CHARSET,"UTF-8");    
  22.   
  23.         HttpGet httpget = new HttpGet("http://"+host+url+"?"+param);    
  24.   
  25.         ResponseHandler<String> responseHandler = new BasicResponseHandler();    
  26.         String responseBody = httpclient.execute(httpget, responseHandler);    
  27.         System.out.println(responseBody);    
  28.         httpclient.getConnectionManager().shutdown();    
  29.         }    
  30. }   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<?xml version="1.0" encoding="UTF-8"?> <!-- 通用web service调用. XmlFileCache.getInstance().init("D:\\wid6.1\\workspace\\intf\\src\\config\\ws\\");//默认XmlFileCache.getInstance().init()加载classes/ws/下的所有xml文件. InvokeService ser=new InvokeServiceImpl(); Map<String, Object> map = new HashMap<String, Object>(); map.put("serSupplier", "afasfasf"); map.put("callTime", "ffffffffff"); Map<String, WebServiceResultBean> rs = ser.invoke("EOMS", "isAlive1", map); System.out.println(rs.get("return").getValue()); --> <serviceGroup> <service id="EOMS" endpoint="http://10.101.16.52:8089/CRSS/SvrIntr/HLRSvrLib/HLRSvr.asmx?wsdl"> <!-- 为stub 方式调用,事先生成好存根,程序没有找到存根,则会告诉你生成方法 --> <operation id="isAlive1" method="ReverseSMSSend" processor="com.asiainfo.commons.ADBProcessorAdapter" namespace="http://boco.com.cn/fr/SvrLib/HlrQuery" stubClass="samples.quickstart.clients.HLRSvrLibImplStub"> <param> <item name="strDestNumber" type="java.lang.String" /> </param> <return> <item name="returnCode" type="java.lang.String" /> <item name="resultDesc" type="java.lang.String" /> <item name="serviceName" type="java.lang.String" /> <item name="bBOOLsmNationalRoam" type="java.lang.String" /> </return> </operation> <!-- OM模型 调用web service ,action 为soapaction 参见wsdl--> <operation id="isAlive2" method="isAlive" processor="OMProcessorAdapter" namespace="http://service.asiainfo.com/" action="http://service.asiainfo.com/isAlive" stubClass="test.eoms.clients.EOMSProcessSheetStub"> <param> <item name="serSupplier" type="java.lang.String" /> <item name="callTime" type="java.lang.String" /> </param> <return> <item name="return" type="java.lang.String" /> </return> </operation> </service> </serviceGroup> 如果用adbprocessorAdapter 只要指定输入参数和输出参数,支持复杂返回参数.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值