SOAPリクエスト送信サンプルコードです。
リクエスト送信プログラム
package test1;
import java.io.FileOutputStream;
import java.util.Iterator;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFault;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
public class SampleMsgClient1 {
public static void main(String[] args) {
try {
// SOAPメッセージの送信先への仮想コネクションを取得
String endPoint = new String("http://localhost:8080/Calculator");
// SSL
// String endPoint = new String("https://localhost:8080/Calculator");
// 送信先のURLを用意し、SOAPConnectionFactory オブジェクトからSOAPConnectionオブジェクトを取得する
SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
SOAPConnection conn = scf.createConnection();
// 送信するSOAPメッセージを表すSOAPMessageオブジェクトを取得
MessageFactory mf = MessageFactory.newInstance();
// SOAPMessageオブジェクト取得
SOAPMessage msg = mf.createMessage();
// 送信するSOAPメッセージを作成
SOAPPart part = msg.getSOAPPart();
SOAPEnvelope env = part.getEnvelope();
env.getHeader().detachNode();
env.addNamespaceDeclaration("jax", "http://jaxws.example/");
SOAPBody body = env.getBody();
SOAPBodyElement elm = body.addBodyElement(env.createName("jax:add"));
elm.addChildElement(env.createName("arg0"));
((SOAPElement) body.getElementsByTagName("arg0").item(0)).addChildElement(env.createName("calcReq"));
((SOAPElement) body.getElementsByTagName("calcReq").item(0)).addChildElement(env.createName("param1"))
.addTextNode("3");
((SOAPElement) body.getElementsByTagName("calcReq").item(0)).addChildElement(env.createName("param2"))
.addTextNode("7");
((SOAPElement) body.getElementsByTagName("arg0").item(0)).addChildElement(env.createName("precision"))
.addTextNode("0");
msg.saveChanges();
FileOutputStream requestFile = new FileOutputStream("src/test1/request.xml");
msg.writeTo(requestFile);
requestFile.close();
// SOAPメッセージ送信
SOAPMessage reply = conn.call(msg, endPoint);
// 受信したSOAPメッセージをファイル出力
FileOutputStream replyFile = new FileOutputStream("src/test1/reply.xml");
reply.writeTo(replyFile);
replyFile.close();
// SOAPボディにFaultが設定されているかを判定
part = reply.getSOAPPart();
env = part.getEnvelope();
body = env.getBody();
if (body.hasFault()) {
// Faultが返ってきた場合はエラー処理を行う
System.out.println("SOAP Fault.");
SOAPFault fault = body.getFault();
String faultActor = fault.getFaultActor();
String faultCode = fault.getFaultCode();
String faultString = fault.getFaultString();
System.out.println("faultActor :" + faultActor);
System.out.println("faultCode :" + faultCode);
System.out.println("faultString:" + faultString);
Iterator> it = fault.getChildElements();
while (it.hasNext()) {
SOAPElement detailElm = (SOAPElement) it.next();
System.out.println("Detail:" + detailElm);
}
} else {
System.out.println("SOAP Fault.");
Iterator> it = body.getChildElements();
while (it.hasNext()) {
SOAPElement bodyElm = (SOAPElement) it.next();
System.out.println("Element(addResponse):" + bodyElm);
Iterator> res = bodyElm.getChildElements();
while (res.hasNext()) {
SOAPElement ret = (SOAPElement) res.next();
System.out.println("Element(return):" + ret);
Iterator> elem = ret.getChildElements();
while (elem.hasNext()) {
SOAPElement retCd = (SOAPElement) elem.next();
System.out.println("Element(returnCd):" + retCd);
System.out.println("Value:" + retCd.getValue());
if (retCd.hasChildNodes()) {
Iterator> answer = retCd.getChildElements(env.createName("answer"));
while (answer.hasNext()) {
SOAPElement ans = (SOAPElement) answer.next();
System.out.println("Element:" + ans);
System.out.println("Value:" + ans.getValue());
}
Iterator> remainder = retCd.getChildElements(env.createName("remainder"));
while (remainder.hasNext()) {
SOAPElement rema = (SOAPElement) remainder.next();
System.out.println("Element:" + rema);
System.out.println("Value:" + rema.getValue());
}
}
}
}
}
}
// コネクションを閉じる
conn.close();
} catch (SOAPException e) {
// SOAPException出力
System.out.println("SOAPException raised.");
System.out.println(" Message:" + e.getMessage());
System.out.println(" Cause :" + e.getCause());
e.printStackTrace();
} catch (Throwable t) {
// 例外情報とスタックトレースを標準出力
t.printStackTrace();
}
}
}