Java简单实现webservice接口


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 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. }  
  • 1
  • 2
  • 下一页

相关内容