用HTTP GET调用web service

来源:互联网 发布:讨鬼传2女生捏脸数据 编辑:程序博客网 时间:2024/06/05 05:08

下面的网址提供了国内飞机航班时刻表 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. }