webservice实现有多种方式

比如最常用的有axis框架,xfire框架,通过该框架可以发布wsdl接口,也可以实现webservice客户端,目前eclipse都有集成的插件,可以根据wsdl文件生成webservice客户端调用接口,但是这样部署的时候必须依赖框架的jar包,有时候可能因为环境等等原因,我们仅仅需要wsdl中的某一个接口,这时候可以通过http接口或socket接口直接发生xml数据,来调用服务端webservice服务,其实webservice底层还是发送xml数据,只是框架封装了对xml数据进行序列化与反序列化操作,下面以两个简单的例子说明http方式和socket方式。

 

 

http实现webservice接口调用例子:



1. import java.io.BufferedReader;  
2. import java.io.IOException;  
3. import java.io.InputStreamReader;  
4. import java.io.OutputStreamWriter;  
5. import java.io.UnsupportedEncodingException;  
6. import .MalformedURLException;  
7. import .URL;  
8. import .URLConnection;  
9.   
10. public class
11. void
12. try
13. new
14.             URLConnection con = url.openConnection();  
15. true);  
16. "Pragma:", "no-cache");  
17. "Cache-Control", "no-cache");  
18. "Content-Type", "text/xml");  
19.               
20. out = new
21.             String xmlInfo = getXmlInfo();  
22. out.write(new
23. out.flush();  
24. out.close();  
25. new BufferedReader(new
26. "";  
27. new
28. for (line = br.readLine(); line != null; line = br.readLine()) {  
29. new String(line.getBytes(),"UTF-8"));  
30.             }  
31. out.println(buf.toString());  
32. catch
33.             e.printStackTrace();  
34. catch
35.             e.printStackTrace();  
36.         }  
37.     }  
38.   
39. private
40. // 通过wsdl文件可以查看接口xml格式数据,构造调用接口xml数据
41. "<SOAP-ENV:Envelope xmlns:SOAP-ENV=/"http://schemas.xmlsoap.org/soap/envelope//" xmlns:SOAP-ENC=/"http://schemas.xmlsoap.org/soap/encoding//" xmlns:xsi=/"http://www.w3.org/2001/XMLSchema-instance/" xmlns:xsd=/"http://www.w3.org/2001/XMLSchema/">"
42. "<SOAP-ENV:Body>"
43. "<m:getItemDetailSingle xmlns:m=/"http:xxxxxxxxxxxxxxxxxx//">"
44. "<itemMo>"
45. "<category>工厂类</category>"
46. "<city>北京</city>"
47. "<flag>1</flag>"
48. "<itemId>0</itemId>"
49. "<itemIndex>1</itemIndex>"
50. "<keyword></keyword>"
51. "<mobile>2147483647</mobile>"
52. "<password>123456</password>"
53. "<userName>sohu</userName>"
54. "</itemMo>"
55. "</m:getItemDetailSingle>"
56. "</SOAP-ENV:Body>"
57. "</SOAP-ENV:Envelope>";  
58. return
59.     }  
60.   
61. public static void
62. "http://localhost:9003/dataService/services/Job";  
63. new
64.     }  
65. }


 

 

socke方式实现例子:

 




1. import java.io.IOException;  
2. import java.io.InputStream;  
3. import java.io.InputStreamReader;  
4. import java.io.OutputStream;  
5. import .Socket;  
6. import .UnknownHostException;  
7.   
8.   
9. public class
10.   
11. /**
12.      * @param args
13.      * @throws IOException 
14.      * @throws UnknownHostException 
15.      */
16. public static void
17. new Socket("localhost",9003);     
18.         OutputStream os = socket.getOutputStream();     
19. is
20. //System.out.println(socket.isConnected());
21. "POST /dataService/services/Job HTTP/1.1/r/n"
22. "Content-Type:text/xml/r/n"
23. "Host:localhost:9003/r/n"
24. "Content-Length:1024/r/n"
25. "/r/n"
26. "<SOAP-ENV:Envelope xmlns:SOAP-ENV=/"http://schemas.xmlsoap.org/soap/envelope//" xmlns:SOAP-ENC=/"http://schemas.xmlsoap.org/soap/encoding//" xmlns:xsi=/"http://www.w3.org/2001/XMLSchema-instance/" xmlns:xsd=/"http://www.w3.org/2001/XMLSchema/">"
27. "<SOAP-ENV:Body>"
28. "<m:getItemDetailSingle xmlns:m=/"http://localhost//">"
29. "<itemMo>"
30. "<category>工厂类</category>"
31. "<city>北京</city>"
32. "<flag>1</flag>"
33. "<itemId>0</itemId>"
34. "<itemIndex>1</itemIndex>"
35. "<keyword>String</keyword>"
36. "<mobile>2147483647</mobile>"
37. "<password>123456</password>"
38. "<userName>sohu</userName>"
39. "</itemMo>"
40. "</m:getItemDetailSingle>"
41. "</SOAP-ENV:Body>"
42. "</SOAP-ENV:Envelope>";  
43.         os.write(httpSend.getBytes());     
44.         os.flush();     
45.     
46. new InputStreamReader(is);     
47. new
48.              
49. "";     
50.              
51. while((responseLine = breader.readLine()) != null)     
52.         {     
53. out.println(new String(responseLine.getBytes(),"UTF-8"));     
54.         }     
55.               
56. out.println("");     
57.   
58.     }  
59.   
60. }




webservice实现有多种方式

比如最常用的有axis框架,xfire框架,通过该框架可以发布wsdl接口,也可以实现webservice客户端,目前eclipse都有集成的插件,可以根据wsdl文件生成webservice客户端调用接口,但是这样部署的时候必须依赖框架的jar包,有时候可能因为环境等等原因,我们仅仅需要wsdl中的某一个接口,这时候可以通过http接口或socket接口直接发生xml数据,来调用服务端webservice服务,其实webservice底层还是发送xml数据,只是框架封装了对xml数据进行序列化与反序列化操作,下面以两个简单的例子说明http方式和socket方式。

 

 

http实现webservice接口调用例子:




1. import java.io.BufferedReader;  
2. import java.io.IOException;  
3. import java.io.InputStreamReader;  
4. import java.io.OutputStreamWriter;  
5. import java.io.UnsupportedEncodingException;  
6. import .MalformedURLException;  
7. import .URL;  
8. import .URLConnection;  
9.   
10. public class
11. void
12. try
13. new
14.             URLConnection con = url.openConnection();  
15. true);  
16. "Pragma:", "no-cache");  
17. "Cache-Control", "no-cache");  
18. "Content-Type", "text/xml");  
19.               
20. out = new
21.             String xmlInfo = getXmlInfo();  
22. out.write(new
23. out.flush();  
24. out.close();  
25. new BufferedReader(new
26. "";  
27. new
28. for (line = br.readLine(); line != null; line = br.readLine()) {  
29. new String(line.getBytes(),"UTF-8"));  
30.             }  
31. out.println(buf.toString());  
32. catch
33.             e.printStackTrace();  
34. catch
35.             e.printStackTrace();  
36.         }  
37.     }  
38.   
39. private
40. // 通过wsdl文件可以查看接口xml格式数据,构造调用接口xml数据
41. "<SOAP-ENV:Envelope xmlns:SOAP-ENV=/"http://schemas.xmlsoap.org/soap/envelope//" xmlns:SOAP-ENC=/"http://schemas.xmlsoap.org/soap/encoding//" xmlns:xsi=/"http://www.w3.org/2001/XMLSchema-instance/" xmlns:xsd=/"http://www.w3.org/2001/XMLSchema/">"
42. "<SOAP-ENV:Body>"
43. "<m:getItemDetailSingle xmlns:m=/"http:xxxxxxxxxxxxxxxxxx//">"
44. "<itemMo>"
45. "<category>工厂类</category>"
46. "<city>北京</city>"
47. "<flag>1</flag>"
48. "<itemId>0</itemId>"
49. "<itemIndex>1</itemIndex>"
50. "<keyword></keyword>"
51. "<mobile>2147483647</mobile>"
52. "<password>123456</password>"
53. "<userName>sohu</userName>"
54. "</itemMo>"
55. "</m:getItemDetailSingle>"
56. "</SOAP-ENV:Body>"
57. "</SOAP-ENV:Envelope>";  
58. return
59.     }  
60.   
61. public static void
62. "http://localhost:9003/dataService/services/Job";  
63. new
64.     }  
65. }


 

socke方式实现例子:

 



1. import java.io.IOException;  
2. import java.io.InputStream;  
3. import java.io.InputStreamReader;  
4. import java.io.OutputStream;  
5. import .Socket;  
6. import .UnknownHostException;  
7.   
8.   
9. public class
10.   
11. /**
12.      * @param args
13.      * @throws IOException 
14.      * @throws UnknownHostException 
15.      */
16. public static void
17. new Socket("localhost",9003);     
18.         OutputStream os = socket.getOutputStream();     
19. is
20. //System.out.println(socket.isConnected());
21. "POST /dataService/services/Job HTTP/1.1/r/n"
22. "Content-Type:text/xml/r/n"
23. "Host:localhost:9003/r/n"
24. "Content-Length:1024/r/n"
25. "/r/n"
26. "<SOAP-ENV:Envelope xmlns:SOAP-ENV=/"http://schemas.xmlsoap.org/soap/envelope//" xmlns:SOAP-ENC=/"http://schemas.xmlsoap.org/soap/encoding//" xmlns:xsi=/"http://www.w3.org/2001/XMLSchema-instance/" xmlns:xsd=/"http://www.w3.org/2001/XMLSchema/">"
27. "<SOAP-ENV:Body>"
28. "<m:getItemDetailSingle xmlns:m=/"http://localhost//">"
29. "<itemMo>"
30. "<category>工厂类</category>"
31. "<city>北京</city>"
32. "<flag>1</flag>"
33. "<itemId>0</itemId>"
34. "<itemIndex>1</itemIndex>"
35. "<keyword>String</keyword>"
36. "<mobile>2147483647</mobile>"
37. "<password>123456</password>"
38. "<userName>sohu</userName>"
39. "</itemMo>"
40. "</m:getItemDetailSingle>"
41. "</SOAP-ENV:Body>"
42. "</SOAP-ENV:Envelope>";  
43.         os.write(httpSend.getBytes());     
44.         os.flush();     
45.     
46. new InputStreamReader(is);     
47. new
48.              
49. "";     
50.              
51. while((responseLine = breader.readLine()) != null)     
52.         {     
53. out.println(new String(responseLine.getBytes(),"UTF-8"));     
54.         }     
55.               
56. out.println("");     
57.   
58.     }  
59.   
60. }