底层调用 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的方法。

 

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值