欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

xFire 下的WebService 客户端接入

程序员文章站 2022-06-14 16:17:54
...
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.httpclient.params.HttpClientParams;
import org.codehaus.xfire.MessageContext;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.handler.AbstractHandler;
import org.codehaus.xfire.transport.http.CommonsHttpMessageSender; 
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.ourlinc.tern.util.XmlHelper;

/**
 * web service接口封装
 * 
 * 
 */
public class Test {

    public static void main(String[] args) {
        Test test = new Test();
        test.setWorkNo("xx");
        test.setPassword("xx");
        test.setWsdl("http://xxx/xxx.asmx?WSDL");
        test.getCarrayStations();
    }

    public Test() {
        super();
    }

    /**
     * soap验证账户
     */
    ClientAuthenticationHandler m_Account = new ClientAuthenticationHandler();

    public void setWorkNo(String workno) {
        m_Account.workno = workno;
    }

    public void setPassword(String password) {
        m_Account.password = password;
    }

    public static class ClientAuthenticationHandler extends AbstractHandler {
        private String workno;
        private String password;

        public ClientAuthenticationHandler(String username, String password) {
            super();
            this.workno = username;
            this.password = password;
        }

        public ClientAuthenticationHandler() {
            super();
        }

        // 命名空间 http://tempuri.org/
        public static final String NS = "http://tempuri.org/";

        public void invoke(MessageContext context) throws Exception {
            // 为SOAP Header构造验证信息
            org.jdom.Element el = new org.jdom.Element("header", org.jdom.Namespace
                    .getNamespace(NS));
            context.getOutMessage().setHeader((org.jdom.Element) el);
            org.jdom.Element auth = new org.jdom.Element("SecurityHeader", org.jdom.Namespace
                    .getNamespace(NS));
            org.jdom.Element workno_el = new org.jdom.Element("WorkNo", org.jdom.Namespace
                    .getNamespace(NS));
            workno_el.addContent(workno);
            org.jdom.Element password_el = new org.jdom.Element("Password", org.jdom.Namespace
                    .getNamespace(NS));
            password_el.addContent(password);
            auth.addContent(workno_el);
            auth.addContent(password_el);
            el.addContent(auth);
        }
    }

    // 查询全部始发车站(一个售票系统可能有多个始发车站)
    public void getCarrayStations() {
        Object[] result = null;
        Client client = null;
        Object[] params = null;
        try {
            params = new Object[] { "" };
            client = getClient(m_Wsdl);
            client.toString();
            result = client.invoke("GetCarrayStations", params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String[] results = getResultStrings(result);
        if (null != results) {
            for (String s : results) {
                System.out.println(s);
            }
        }
        closeClient(client);
        return;
    }

    URL m_Wsdl;

    Client client = getClient(m_Wsdl);

    /**
     * 取得服务接口客户端实例
     * 
     * @return 客户端实例
     * @throws Exception
     */
    Client getClient(URL wsdl) {
        Client client;
        try {
            client = new Client(wsdl);
            HttpClientParams params = new HttpClientParams();
            params.setParameter(HttpClientParams.USE_EXPECT_CONTINUE, Boolean.FALSE);
            // params.setParameter(HttpClientParams.CONNECTION_MANAGER_TIMEOUT,
            // 10 * 1000L);
            params.setConnectionManagerTimeout(10 * 1000L);
            // params.setParameter(HttpClientParams.SO_TIMEOUT, 20 *1000L);
            params.setSoTimeout(40 * 1000);
            client.setProperty(CommonsHttpMessageSender.HTTP_CLIENT_PARAMS, params);
            client.addOutHandler(m_Account);
            return client;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 出错时关闭由getClient()取得的客户端实例
     * 
     * @param client
     *            客户端实例
     */
    void closeClient(Client client) {
        client.close();
    }

    public void setWsdl(String wsdl) {
        URL url;
        if (wsdl.startsWith("http://") || wsdl.startsWith("https://")) {
            try {
                url = new URL(wsdl);
            } catch (MalformedURLException e) {
                throw new IllegalArgumentException(e);
            }
        } else {
            url = ClassLoader.getSystemResource(wsdl);
            if (null == url) {
                url = Thread.currentThread().getContextClassLoader().getResource(wsdl);
            }
        }
        m_Wsdl = url;
    }

    String[] getResultStrings(Object[] result) {
        if (null == result || 0 == result.length) {
            System.out.println("接口无返回结果");
            // _Logger.info("接口无返回结果");
            return null;
        }
        Element e = ((Document) result[0]).getDocumentElement();
        NodeList nodes = e.getChildNodes();
        String[] items = new String[nodes.getLength()];
        for (int i = 0; i < items.length; i++) {
            Node node = nodes.item(i);
            items[i] = XmlHelper.getElementText(node);
        }
        return items;
    }

}

 

只是一个模板,稍作收藏,以备不时之需!