`
endual
  • 浏览: 3545590 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

底层调用 Web Services

 
阅读更多

底层调用 Web Services

分类: ┣ Android ┫ 120人阅读 评论 (0) 收藏 举报

      以前一直把Web Services说在嘴边,都没亲自去试验下,或者都是通过别人的API去调用,没有从底层去实验,今天病好很多,下午也闲来无事,便把这个一直留在心里的任务给完成了,以满足自己。

      Web Services的原理我就不仔细说了,可以参考一本《Web Services技术、架构和应用》,书虽有点厚,但很经典。

      本文调用http://www.webxml.com.cn/zh_cn/index.aspx提供的Web Services,该网站提供了几种WEB服务。本文以手机号码归属地查询为例,介绍调用Web Services的方法。

       首先在http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo,我们便可看到该方法的参数传递形式。

 

  1. SOAP 1.1  
  2. 以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。  
  3. POST /WebServices/MobileCodeWS.asmx HTTP/1.1  
  4. Host: webservice.webxml.com.cn  
  5. Content-Type: text/xml; charset=utf-8  
  6. Content-Length: length  
  7. SOAPAction: "http://WebXml.com.cn/getMobileCodeInfo"   
  8. <?xml version="1.0"  encoding= "utf-8" ?>  
  9. <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd= "http://www.w3.org/2001/XMLSchema"  xmlns:soap= "http://schemas.xmlsoap.org/soap/envelope/" >  
  10.   <soap:Body>  
  11.     <getMobileCodeInfo xmlns="http://WebXml.com.cn/" >  
  12.       <mobileCode>string </mobileCode>  
  13.       <userID>string </userID>  
  14.     </getMobileCodeInfo>  
  15.   </soap:Body>  
  16. </soap:Envelope>  
  17. HTTP/1.1 200 OK  
  18. Content-Type: text/xml; charset=utf-8  
  19. Content-Length: length  
  20. <?xml version="1.0"  encoding= "utf-8" ?>  
  21. <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd= "http://www.w3.org/2001/XMLSchema"  xmlns:soap= "http://schemas.xmlsoap.org/soap/envelope/" >  
  22.   <soap:Body>  
  23.     <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/" >  
  24.       <getMobileCodeInfoResult>string </getMobileCodeInfoResult>  
  25.     </getMobileCodeInfoResponse>  
  26.   </soap:Body>  
  27. </soap:Envelope>  
  28. SOAP 1.2  
  29. 以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。  
  30. POST /WebServices/MobileCodeWS.asmx HTTP/1.1  
  31. Host: webservice.webxml.com.cn  
  32. Content-Type: application/soap+xml; charset=utf-8  
  33. Content-Length: length  
  34. <?xml version="1.0"  encoding= "utf-8" ?>  
  35. <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd= "http://www.w3.org/2001/XMLSchema"  xmlns:soap12= "http://www.w3.org/2003/05/soap-envelope" >  
  36.   <soap12:Body>  
  37.     <getMobileCodeInfo xmlns="http://WebXml.com.cn/" >  
  38.       <mobileCode>string </mobileCode>  
  39.       <userID>string </userID>  
  40.     </getMobileCodeInfo>  
  41.   </soap12:Body>  
  42. </soap12:Envelope>  
  43. HTTP/1.1 200 OK  
  44. Content-Type: application/soap+xml; charset=utf-8  
  45. Content-Length: length  
  46. <?xml version="1.0"  encoding= "utf-8" ?>  
  47. <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd= "http://www.w3.org/2001/XMLSchema"  xmlns:soap12= "http://www.w3.org/2003/05/soap-envelope" >  
  48.   <soap12:Body>  
  49.     <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/" >  
  50.       <getMobileCodeInfoResult>string </getMobileCodeInfoResult>  
  51.     </getMobileCodeInfoResponse>  
  52.   </soap12:Body>  
  53. </soap12:Envelope>  
 

 

      本文以socket为例子,通过底层方式调用Web Services,把上述的协议描述输成代码,如下:

  1. String strHeader =  "POST /WebServices/MobileCodeWS.asmx HTTP/1.1/r/n"   
  2.         + "Host: webservice.webxml.com.cn/r/n"   
  3.         + "Content-Type: text/xml; charset=utf-8/r/n"   
  4.         + "Content-Length: $Length/r/n"   
  5.         + "SOAPAction: /" http: //WebXml.com.cn/getMobileCodeInfo/"/r/n/r/n";   
  6. String strBody = "<?xml version=/" 1.0/ " encoding=/" utf-8/ "?>/r/n"   
  7.         + "<soap:Envelope xmlns:xsi=/" http: //www.w3.org/2001/XMLSchema-instance/" xmlns:xsd=/"http://www.w3.org/2001/XMLSchema/" xmlns:soap=/"http://schemas.xmlsoap.org/soap/envelope//">/r/n"   
  8.         + "<soap:Body>/r/n"   
  9.         + "<getMobileCodeInfo xmlns=/" http: //WebXml.com.cn//">/r/n"   
  10.         + "<mobileCode>$MobileNo</mobileCode>/r/n"   
  11.         + "<userID>$UserID</userID>/r/n"  +  "</getMobileCodeInfo>/r/n"   
  12.         + "</soap:Body>/r/n"  +  "</soap:Envelope>/r/n" ;  
 

 

      在这里要注意,代码的形式,协议Header和协议Body要区分清楚,否则会出现调用HTTP400或者HTTP500错误。

      使用Socket 完整代码如下:

 

  1. import java.io.BufferedInputStream;  
  2. import java.io.InputStream;  
  3. import java.io.OutputStream;  
  4. import java.net.InetAddress;  
  5. import java.net.Socket;  
  6. public   class  JavaTest {  
  7.     public   static   void  main(String[] argv) {  
  8.         String strHeader = "POST /WebServices/MobileCodeWS.asmx HTTP/1.1/r/n"   
  9.                 + "Host: webservice.webxml.com.cn/r/n"   
  10.                 + "Content-Type: text/xml; charset=utf-8/r/n"   
  11.                 + "Content-Length: $Length/r/n"   
  12.                 + "SOAPAction: /" http: //WebXml.com.cn/getMobileCodeInfo/"/r/n/r/n";   
  13.         String strBody = "<?xml version=/" 1.0/ " encoding=/" utf-8/ "?>/r/n"   
  14.                 + "<soap:Envelope xmlns:xsi=/" http: //www.w3.org/2001/XMLSchema-instance/" xmlns:xsd=/"http://www.w3.org/2001/XMLSchema/" xmlns:soap=/"http://schemas.xmlsoap.org/soap/envelope//">/r/n"   
  15.                 + "<soap:Body>/r/n"   
  16.                 + "<getMobileCodeInfo xmlns=/" http: //WebXml.com.cn//">/r/n"   
  17.                 + "<mobileCode>$MobileNo</mobileCode>/r/n"   
  18.                 + "<userID>$UserID</userID>/r/n"  +  "</getMobileCodeInfo>/r/n"   
  19.                 + "</soap:Body>/r/n"  +  "</soap:Envelope>/r/n" ;  
  20.           
  21.         String path = "webservice.webxml.com.cn" ; //   
  22.         try  {  
  23.             InetAddress addr = InetAddress.getByName(path);  
  24.             Socket socket = new  Socket(addr, 80);  
  25.             OutputStream os = socket.getOutputStream();  
  26.             InputStream is  = socket.getInputStream();  
  27.             strBody = strBody.replaceAll("//$MobileNo" "15013055281" );  
  28.             strBody = strBody.replaceAll("//$UserID" "" );  
  29.             byte [] bodyBytes = strBody.getBytes();  
  30.             strHeader = strHeader.replaceAll("//$Length" ""  + bodyBytes.length);  
  31.             String httpSend = strHeader + strBody;  
  32.             System.out .println(httpSend);  
  33.             os.write(httpSend.getBytes());  
  34.             os.flush();  
  35.               
  36.             byte [] recvBytes =  new   byte [4096];  
  37.             int  length = 0;  
  38.             BufferedInputStream bis = new  BufferedInputStream( is );  
  39.               
  40.             while ((length = bis.read(recvBytes) ) != -1){  
  41.                  String recvStr = new  String(recvBytes,  "UTF-8" );  
  42.                  System.out .println(recvStr);  
  43.             }  
  44.         } catch  (Exception e) {  
  45.             e.printStackTrace();  
  46.         }  
  47.     }  
  48. }  
 

 

      使用HttpURLConnection 关键代码代码如下(原理都一样,只不过少写了点东西而已):

 

  1. String path =  "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx" ;  
  2. try  {  
  3.       
  4.     strBody = strBody.replaceAll("//$MobileNo" "15013055288" );  
  5.     strBody = strBody.replaceAll("//$UserID" "" );  
  6.     byte [] bodyBytes = strBody.getBytes();  
  7.     strHeader = strHeader  
  8.             .replaceAll("//$Length" ""  + bodyBytes.length);  
  9.     String httpSend = strBody;  
  10.     System.out .println(httpSend);  
  11.     URL url = new  URL(path);  
  12.     HttpURLConnection conn = (HttpURLConnection)url.openConnection();    
  13.         conn.setRequestMethod("POST" );    
  14.         conn.setDoOutput(true );    
  15.         conn.setConnectTimeout(5 * 1000);    
  16.            
  17.         conn.setRequestProperty("Content-Type" , "application/soap+xml; charset=utf-8" );    
  18.         conn.setRequestProperty("Content-Length" , String.valueOf(bodyBytes.length));    
  19.         OutputStream os = conn.getOutputStream();  
  20.         os.write(bodyBytes);    
  21.         os.flush();  
  22.           
  23.         InputStream is  = conn.getInputStream();  
  24.          
  25.          
  26.             byte [] recvBytes =  new   byte [4096];  
  27.     int  length = 0;  
  28.     BufferedInputStream bis = new  BufferedInputStream( is );  
  29.       
  30.     while ((length = bis.read(recvBytes) ) != -1){  
  31.          String recvStr = new  String(recvBytes,  "UTF-8" );  
  32.          System.out .println(recvStr);  
  33.     }  
  34. catch  (Exception e) {  
  35.     e.printStackTrace();  
  36. }  
 

 

      得到调用结果如下:

 

  1. HTTP/1.1 200 OK  
  2. Date: Thu, 10 Feb 2011 14:35:02 GMT  
  3. Server: Microsoft-IIS/6.0  
  4. X-Powered-By: ASP.NET  
  5. X-AspNet-Version: 2.0.50727  
  6. Cache-Control: private , max-age=0  
  7. Content-Type: text/xml; charset=utf-8  
  8. Content-Length: 434  
  9. <?xml version="1.0"  encoding= "utf-8" ?>  
  10. <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd= "http://www.w3.org/2001/XMLSchema" >  
  11.     <soap:Body>  
  12.         <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/" >  
  13.             <getMobileCodeInfoResult>15013055288:广东 广州 广东移动全球通卡               </getMobileCodeInfoResult>  
  14.         </getMobileCodeInfoResponse>  
  15.     </soap:Body>  
  16. </soap:Envelope>  
 

 

      以上便是从底层调用Web Services的方法。

 

 

      版权所有,转载请注明出处

分享到:
评论

相关推荐

    C++调用Web services的控制台应用程序

    本项目"**C++调用Web services的控制台应用程序**"正是这样一个示例,它展示了如何在C++环境下创建一个能调用Web服务的简单命令行应用。 首先,我们要理解Web服务的本质。Web服务是一种基于HTTP协议,使用XML(可...

    Java与.NET 的Web Services相互调用

    通过设置事件处理程序,当按钮被点击时,调用Web服务的getName方法并显示结果。 **二、用Java做客户端调用.NET写的Web Services** 1. **创建.NET Web Service** 在VS.NET中新建一个ASP Web Service工程,添加一个...

    WebServices服务接口调用---基于rpc方式应用

    WebServices服务接口调用是分布式系统中常见的技术手段,它允许不同系统间的应用程序通过网络进行通信。本主题将深入探讨基于RPC(Remote Procedure Call)方式的WebServices应用,特别是使用sun-jaxws.xml配置文件...

    一个利用C++CLI实现Web Services 的方法调用程序例子

    这个“一个利用C++CLI实现Web Services的方法调用程序例子”是一个很好的学习资源,用于理解如何在C++CLI环境中调用Web服务。 Web服务是一种基于互联网的、可互操作的应用程序接口,它们通过标准协议如SOAP(简单...

    C++使用webservices

    ATL提供了一套工具,使得开发者可以轻松地创建和调用Web Services,而无需深入理解底层的SOAP和WSDL细节。 以下是一个简单的步骤来展示如何在VC++中调用XML Web Service: 1. **理解WSDL**:首先,你需要获取Web ...

    js调用外部天气预报,手机查询等webservices

    它简化了服务器端和客户端的交互,使得开发者可以使用JS调用.NET Web服务,而无需关心底层实现。 三、调用外部天气预报Web服务 调用外部天气预报Web服务通常需要以下步骤: 1. 查找合适的天气预报API,如...

    web services开发文档

    Web服务是一种基于网络的、标准化的软件交互方式,它允许不同的应用程序之间进行数据交换和功能调用。在本文中,我们将深入探讨Web服务的核心概念、技术栈以及开发过程。 一、Web服务概述 Web服务主要利用了开放的...

    Web Services Axis实现

    通过Axis,开发者可以快速地构建和部署Web Services,无需深入了解底层的SOAP和WSDL(Web服务描述语言)细节。 2. **Axis的工作原理** Axis通过WSDL文件来理解Web Service的接口定义,然后自动生成客户端的Stub...

    如何通过PL/SQL访问到Web Services

    随着Web Services的广泛应用,Oracle也提供了相应的技术支持,让开发人员能够通过PL/SQL直接调用Web Services。这篇文章主要探讨如何在Oracle环境中,利用PL/SQL的UTL_DBWS包来访问Web Services。 在Oracle 9i版本...

    XFire开发Web Services

    ### 使用XFire开发Web Services详解 #### 一、概述 XFire是一款开源的轻量级Web服务框架,它简化了Web服务的开发过程,使得开发者能够更轻松地创建和使用Web服务。本篇文章将详细介绍如何使用XFire来开发Web ...

    Programming Web Services with SOAP

    WSDL,即Web服务描述语言(Web Services Description Language),是一种基于XML的语言,用于描述网络服务的功能、消息格式以及如何访问这些服务。WSDL文件包含了服务的抽象接口定义、具体的绑定细节(如传输协议和...

    精通JBoss——EJB与Web Services开发精解

    《精通JBoss——EJB与Web Services开发精解》是一本深入探讨企业级Java应用开发的专业书籍,专注于JBoss应用服务器的使用以及EJB(Enterprise JavaBeans)和Web Services的集成开发。本书旨在帮助读者全面掌握如何在...

    Building XML Web Services with PHP NuSOAP

    调用Web服务同样可以通过NuSOAP客户端类来实现。你只需指定目标Web服务的URL和WSDL文件,NuSOAP会自动解析WSDL,生成必要的SOAP请求。然后,你可以像调用本地函数一样调用远程服务的方法,NuSOAP会处理所有的SOAP...

    NetBeans6.0中使用WebServices

    5. 在Main.java中的main方法中,调用Web服务操作,NetBeans将自动插入相关代码。 6. 运行Main.java,如果出现异常,可能是因为缺少必要的JAR文件(如JAX-WS 2.1库)。添加缺失的库后,程序应能正常运行。 通过以上...

    Packt.Alfresco.3.Web.Services

    在Alfresco中,Web Services扮演着关键的角色,它们允许开发人员通过HTTP协议调用Alfresco的功能,实现远程访问和操作内容的能力。Web Services提供了一种标准化的方式,使得不同平台和语言的应用程序能够与Alfresco...

    gsoap 调用 web service 的接口工具

    在VC++开发环境下,gSOAP作为调用Web Service接口的工具,极大地简化了开发者的工作。 gSOAP的核心功能包括: 1. **代码生成器**:gSOAP提供了一个强大的代码生成器,可以从WSDL(Web Services Description ...

    socket 、webservices、Json的区别

    Socket、WebServices和JSON是三种在信息技术领域中广泛使用的通信和数据交换技术,它们各自有其独特的用途和特点。下面将分别对这三个概念进行详细解释,并探讨它们之间的区别。 Socket,通常被称为套接字,是网络...

Global site tag (gtag.js) - Google Analytics