用Java编写SOAP客户端

时间:2013-11-28 06:49:00

标签: java soap-client

我用Java编写了一个自定义SOAP客户端,虽然它有效,但我觉得我编写它的方式非常简单。我对OOP并不擅长,正如你所看到的,所有的工作都是在一堂课上完成的。你能帮忙改进一下或提出任何想法吗?

我已阅读有关创建肥皂客户端的文章,例如this,我可以说这些课程组织得很好。

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;

import org.w3c.dom.Document;

public class ConInfoService {
private final static String BLANK = "";
    private final static String SOAP_SERVER = "http://server.com/ConInfoService.asmx";
    private final static String PREFIX_XSI = "xsi"; 
    private final static String PREFIX_XSD = "xsd"; 
    private final static String PREFIX_SOAP = "soap";
    private final static String URI_HOST = "server.com";
    private final static String URI_GETCULIST = "http://tempuri.org/GetCuList";
    private final static String URI_XSI = "http://www.w3.org/1999/XMLSchema-instance";
    private final static String URI_XSD = "http://www.w3.org/1999/XMLSchema";
    private final static String URI_SOAP = "http://schemas.xmlsoap.org/soap/envelope/";
    private final static String URI_TEMPURI = "http://tempuri.org/";
    private final static String HEADER_HOST = "Host";
    private final static String HEADER_SOAPACTION = "SOAPAction";

    public SOAPMessage getCuList(Date asDate,  String asConAvailable, String asConChanged, String asMso) throws Exception {
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();

            String soapServer = SOAP_SERVER;;
            SOAPMessage soapMessage = createSOAPRequest(asDate, asConAvailable, asConChanged, asMso);
            return soapConnection.call(soapMessage, soapServer);
    }

    private static SOAPMessage createSOAPRequest(Date asDate,  String asConAvailable, String asConChanged, String asMso) throws Exception {
        if (asDate != null && asConAvailable != null && asConChanged != null && asMso != null) {
            MessageFactory messageFactory = MessageFactory.newInstance();
            SOAPMessage soapMessage = messageFactory.createMessage();
            SOAPPart soapPart = soapMessage.getSOAPPart();

            SOAPEnvelope envelope = soapPart.getEnvelope();
            envelope.addNamespaceDeclaration(PREFIX_XSI, URI_XSI);
            envelope.addNamespaceDeclaration(PREFIX_XSD, URI_XSD);
            envelope.addNamespaceDeclaration(PREFIX_SOAP, URI_SOAP);

            SOAPBody soapBody = envelope.getBody();
            SOAPElement getCuListElem = soapBody.addChildElement("GetCuList", BLANK, URI_TEMPURI);
            SOAPElement asDateElem = getCuListElem.addChildElement("asDate", BLANK);
            SOAPElement asConAvailableElem = getCuListElem.addChildElement("asConAvailable", BLANK);
            SOAPElement asConChangedElem = getCuListElem.addChildElement("asConChanged", BLANK);
            SOAPElement asMsoElem = getCuListElem.addChildElement("asMso", BLANK);      

            asDateElem.addTextNode(formatDate(asDate));
            asConAvailableElem.addTextNode(asConAvailable.trim());
            asConChangedElem.addTextNode(asConChanged.trim());
            asMsoElem.addTextNode(asMso.trim());

            MimeHeaders headers = soapMessage.getMimeHeaders();
            headers.addHeader(HEADER_HOST, URI_HOST);
            headers.addHeader(HEADER_SOAPACTION, URI_GETCULIST);

            soapMessage.saveChanges();

            return soapMessage;
        }

        return null;
    }
}

1 个答案:

答案 0 :(得分:5)

为什么不以基于wsdl的类生成方式使用JAX-WS / JAXB?例如thisthis