아래처럼 SAAJ를 이용해서 SOAP 1.1 버전의 메소드를 호출해서 사용할 수 있다..
흠.. soap 보다는 json with rest 방식으로.. ^^;;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.transform.dom.DOMSource;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class SoapClient implements IClient {
public static String soapActionUrl = "http://localhost/soap_http/test.asmx?op=GetPurchaseListWBC";
private SoapClient(){};
public static SoapClient newInstance() {
return new SoapClient();
}
public static String getRequestMessage(int serviceCode, int uuid) {
String query = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
+ "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
+ "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" >\n"
//+ "xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\">\n"
+ " <soap:Body>\n"
+ " <GetPurchaseListWBC xmlns=\"http://tempuri.org/\">\n"
+ " <serviceCode>"+String.valueOf(serviceCode)+"</serviceCode>\n"
+ " <uuid>"+ String.valueOf(uuid) +"</uuid>\n"
+ " </GetPurchaseListWBC>\n"
+ " </soap:Body>\n"
+ "</soap:Envelope>";
return query;
}
public SOAPMessage request(String soapActionUrl) throws Exception {
Document doc = null;
DocumentBuilder docBuilder = null;
DOMSource domSource = null;
SOAPConnection connection = null;
SOAPMessage resultMessage = null;
try {
docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
doc = docBuilder.parse(new ByteArrayInputStream(getRequestMessage(1,2).getBytes()));
domSource = new DOMSource(doc);
SOAPMessage message = MessageFactory.newInstance().createMessage();
message.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "utf-8");
message.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
message.getSOAPPart().setContent(domSource);
message.getMimeHeaders().addHeader("SOAPAction", "http://tempuri.org/GetPurchaseListWBC");
//String url = "http://localhost:1351/test.asmx?op=GetPurchaseListWBC";
connection = SOAPConnectionFactory.newInstance().createConnection();
resultMessage = connection.call(message, soapActionUrl);
NodeList nodes = resultMessage.getSOAPBody().getChildNodes();
for (int i = 0; i < nodes.getLength() ; i++) {
Node node = nodes.item(i);
this.traverse(node);
}
} finally {
connection.close();
}
return resultMessage;
}
public void traverse (Node node) {
String nodeName = node.getNodeName();
String nodeValue = node.getNodeValue();
if(!"#text".equals(nodeName))
System.out.println("<" + nodeName +">");
if (node.hasChildNodes()) {
NodeList nodes= node.getChildNodes();
for (int i=0; i<nodes.getLength(); i++) {
traverse (nodes.item(i));
}
} else {
System.out.println(nodeValue);
}
if(!"#text".equals(nodeName))
System.out.println ("</" + nodeName +">");
}
}
'Java' 카테고리의 다른 글
| #ifdef #ifndef in Java (0) | 2010/06/15 |
|---|---|
| 멀티쓰레드 프로그램 평가기준 (0) | 2010/06/01 |
| 간단한 SOAP 메시지 콜 (0) | 2010/05/26 |
| garbage collector (0) | 2010/04/29 |
| Circular Dependency Problem (0) | 2010/03/26 |