java简单实现webservice接口

webservice实现有多种方式

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

http实现webservice接口调用例子:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class HttpPostTest {
    void testPost(String urlStr) {
        try {
            URL url = new URL(urlStr);
            URLConnection con = url.openConnection();
            con.setDoOutput(true);
            con.setRequestProperty("Pragma:", "no-cache");
            con.setRequestProperty("Cache-Control", "no-cache");
            con.setRequestProperty("Content-Type", "text/xml");
            
            OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());    
            String xmlInfo = getXmlInfo();
            out.write(new String(xmlInfo));
            out.flush();
            out.close();
            BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String line = "";
            StringBuffer buf = new StringBuffer();
            for (line = br.readLine(); line != null; line = br.readLine()) {
                buf.append(new String(line.getBytes(),"UTF-8"));
            }
            System.out.println(buf.toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private String getXmlInfo() {
        // 通过wsdl文件可以查看接口xml格式数据,构造调用接口xml数据
        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/">"
                    + "<SOAP-ENV:Body>"
                    +    "<m:getItemDetailSingle xmlns:m=/"http:xxxxxxxxxxxxxxxxxx//">"
                    +        "<itemMo>"
                    +            "<category>工厂类</category>"
                    +            "<city>北京</city>"
                    +            "<flag>1</flag>"
                    +            "<itemId>0</itemId>"
                    +            "<itemIndex>1</itemIndex>"
                    +            "<keyword></keyword>"
                    +            "<mobile>2147483647</mobile>"
                    +            "<password>123456</password>"
                    +            "<userName>sohu</userName>"
                    +        "</itemMo>"
                    +    "</m:getItemDetailSingle>"
                    + "</SOAP-ENV:Body>"
                    + "</SOAP-ENV:Envelope>";
        return xml;
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        String url = "http://localhost:9003/dataService/services/Job";
        new HttpPostTest().testPost(url);
    }
}

socke方式实现例子:

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;


public class WebServiceClient {

    /**
     * @param args
     * @throws IOException 
     * @throws UnknownHostException 
     */
    public static void main(String[] args) throws UnknownHostException, IOException {
        Socket socket = new Socket("localhost",9003);   
        OutputStream os = socket.getOutputStream();   
        InputStream is = socket.getInputStream();   
        //System.out.println(socket.isConnected());
        String httpSend = "POST /dataService/services/Job HTTP/1.1/r/n"  
                        + "Content-Type:text/xml/r/n"  
                        + "Host:localhost:9003/r/n"  
                        + "Content-Length:1024/r/n"  
                        + "/r/n"  
                        + "<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/">"
                        + "<SOAP-ENV:Body>"
                        +    "<m:getItemDetailSingle xmlns:m=/"http://localhost//">"
                        +        "<itemMo>"
                        +            "<category>工厂类</category>"
                        +            "<city>北京</city>"
                        +            "<flag>1</flag>"
                        +            "<itemId>0</itemId>"
                        +            "<itemIndex>1</itemIndex>"
                        +            "<keyword>String</keyword>"
                        +            "<mobile>2147483647</mobile>"
                        +            "<password>123456</password>"
                        +            "<userName>sohu</userName>"
                        +        "</itemMo>"
                        +    "</m:getItemDetailSingle>"
                        + "</SOAP-ENV:Body>"
                        + "</SOAP-ENV:Envelope>";
        os.write(httpSend.getBytes());   
        os.flush();   
  
        InputStreamReader ireader = new InputStreamReader(is);   
        java.io.BufferedReader breader = new java.io.BufferedReader(ireader);   
           
        String responseLine = "";   
           
        while((responseLine = breader.readLine()) != null)   
        {   
            System.out.println(new String(responseLine.getBytes(),"UTF-8"));   
        }   
            
        System.out.println("");   

    }

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值