java简单实现webservice接口

自作多情 提交于 2020-01-28 03:21:53

java简单实现webservice接口

 

webservice实现有多种方式

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

 

 

http实现webservice接口调用例子:

[c-sharp] view plain copy
  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 java.net.MalformedURLException;  
  7. import java.net.URL;  
  8. import java.net.URLConnection;  
  9.   
  10. public class HttpPostTest {  
  11.     void testPost(String urlStr) {  
  12.         try {  
  13.             URL url = new URL(urlStr);  
  14.             URLConnection con = url.openConnection();  
  15.             con.setDoOutput(true);  
  16.             con.setRequestProperty("Pragma:", "no-cache");  
  17.             con.setRequestProperty("Cache-Control", "no-cache");  
  18.             con.setRequestProperty("Content-Type", "text/xml");  
  19.               
  20.             OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());      
  21.             String xmlInfo = getXmlInfo();  
  22.             out.write(new String(xmlInfo));  
  23.             out.flush();  
  24.             out.close();  
  25.             BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));  
  26.             String line = "";  
  27.             StringBuffer buf = new StringBuffer();  
  28.             for (line = br.readLine(); line != null; line = br.readLine()) {  
  29.                 buf.append(new String(line.getBytes(),"UTF-8"));  
  30.             }  
  31.             System.out.println(buf.toString());  
  32.         } catch (MalformedURLException e) {  
  33.             e.printStackTrace();  
  34.         } catch (IOException e) {  
  35.             e.printStackTrace();  
  36.         }  
  37.     }  
  38.   
  39.     private String getXmlInfo() {  
  40.         // 通过wsdl文件可以查看接口xml格式数据,构造调用接口xml数据  
  41.         String xml = "<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 xml;  
  59.     }  
  60.   
  61.     public static void main(String[] args) throws UnsupportedEncodingException {  
  62.         String url = "http://localhost:9003/dataService/services/Job";  
  63.         new HttpPostTest().testPost(url);  
  64.     }  
  65. }  

 

 

 

socke方式实现例子:

 

[c-sharp] view plain copy
  1. import java.io.IOException;  
  2. import java.io.InputStream;  
  3. import java.io.InputStreamReader;  
  4. import java.io.OutputStream;  
  5. import java.net.Socket;  
  6. import java.net.UnknownHostException;  
  7.   
  8.   
  9. public class WebServiceClient {  
  10.   
  11.     /** 
  12.      * @param args 
  13.      * @throws IOException  
  14.      * @throws UnknownHostException  
  15.      */  
  16.     public static void main(String[] args) throws UnknownHostException, IOException {  
  17.         Socket socket = new Socket("localhost",9003);     
  18.         OutputStream os = socket.getOutputStream();     
  19.         InputStream is = socket.getInputStream();     
  20.         //System.out.println(socket.isConnected());  
  21.         String httpSend = "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.         InputStreamReader ireader = new InputStreamReader(is);     
  47.         java.io.BufferedReader breader = new java.io.BufferedReader(ireader);     
  48.              
  49.         String responseLine = "";     
  50.              
  51.         while((responseLine = breader.readLine()) != null)     
  52.         {     
  53.             System.out.println(new String(responseLine.getBytes(),"UTF-8"));     
  54.         }     
  55.               
  56.         System.out.println("");     
  57.   
  58.     }  
  59.   
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!