底层调用 Web Services


分类: ┣ Android ┫ 2011-02-10 22:40 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. "http://WebXml.com.cn/getMobileCodeInfo"
8. "1.0"  encoding= "utf-8"
9. "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. "http://WebXml.com.cn/"
12. string
13. string
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. "1.0"  encoding= "utf-8"
21. "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. "http://WebXml.com.cn/"
24. string
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. "1.0"  encoding= "utf-8"
35. "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. "http://WebXml.com.cn/"
38. string
39. string
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. "1.0"  encoding= "utf-8"
47. "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. "http://WebXml.com.cn/"
50. string
51.      </getMobileCodeInfoResponse>   
52.    </soap12:Body>   
53.  </soap12:Envelope>

 

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


1. "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. "<?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
7. public   static   void
8. "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. "<?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. "webservice.webxml.com.cn" ; //
22. try
23.              InetAddress addr = InetAddress.getByName(path);   
24. new
25.              OutputStream os = socket.getOutputStream();   
26. is
27. "//$MobileNo" ,  "15013055281"
28. "//$UserID" ,  ""
29. byte
30. "//$Length" ,  ""
31.              String httpSend = strHeader + strBody;   
32. out
33.              os.write(httpSend.getBytes());   
34.              os.flush();   
35.                 
36. byte [] recvBytes =  new   byte
37. int
38. new  BufferedInputStream( is
39.                 
40. while
41. new  String(recvBytes,  "UTF-8"
42. out
43.              }   
44. catch
45.              e.printStackTrace();   
46.          }   
47.      }   
48.  }

 

 

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

 


1. "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"
2. try
3.         
4. "//$MobileNo" ,  "15013055288"
5. "//$UserID" ,  ""
6. byte
7.      strHeader = strHeader   
8. "//$Length" ,  ""
9.      String httpSend = strBody;   
10. out
11. new
12.      HttpURLConnection conn = (HttpURLConnection)url.openConnection();     
13. "POST"
14. true
15.          conn.setConnectTimeout(5 * 1000);     
16.              
17. "Content-Type" , "application/soap+xml; charset=utf-8"
18. "Content-Length"
19.          OutputStream os = conn.getOutputStream();   
20.          os.write(bodyBytes);     
21.          os.flush();   
22.             
23. is
24.            
25.            
26. byte [] recvBytes =  new   byte
27. int
28. new  BufferedInputStream( is
29.         
30. while
31. new  String(recvBytes,  "UTF-8"
32. out
33.      }   
34. catch
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. private
7.  Content-Type: text/xml; charset=utf-8   
8.  Content-Length: 434   
9. "1.0"  encoding= "utf-8"
10. "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. "http://WebXml.com.cn/"
13.              <getMobileCodeInfoResult>15013055288:广东 广州 广东移动全球通卡               </getMobileCodeInfoResult>   
14.          </getMobileCodeInfoResponse>   
15.      </soap:Body>   
16.  </soap:Envelope>

 

 

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

 

 

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