如何使用 SAAJ 创建 SOAP 请求 API
How create SOAP request using SAAJ API
我知道我可能会问一个微不足道的问题,但无法正确创建所需的 SOAP 请求格式。
此类 SOAP 请求
<?xml version="1.0" encoding="UTF-8" ?>
<SOAP:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP:Body>
<GetParameter xmlns="http://examples/2001">
<request>
<MonCode xmlns="http://newsite/mon">Latency</MonCode>
<TimeFrom xmlns="http://newsite/mon">2016-10-26T11:00</TimeFrom>
<TimeTo xmlns="http://newsite/mon">2016-10-26T12:00</TimeTo>
</request>
</GetParameter>
</SOAP:Body>
</SOAP:Envelope>
我尝试使用 Java 的 SAAJ API 创建 SOAP 请求。
我发现了一些问题:
和
Working Soap client example
但不明白,如何为我的活动创建 SOAP 请求。
请帮助创建 SOAP 请求。
p.s。试试这个代码
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
envelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
envelope.addNamespaceDeclaration("soap", "http://schemas.xmlsoap.org/soap/envelop/");
SOAPBody body = message.getSOAPBody();
SOAPElement bodyElement = body.addChildElement("GetParameter");
bodyElement.setAttribute("xmlns", "http://examples/2001");
bodyElement = body.addChildElement("request");
SOAPElement paramsElement = bodyElement.addChildElement("MonCode");
paramsElement.setAttribute("xmlns", "http://newsite/mon");
paramsElement.addTextNode("Latency");
paramsElement = bodyElement.addChildElement("TimeFrom");
paramsElement.setAttribute("xmlns", "http://newsite/mon");
paramsElement.addTextNode("2016-10-26T11:00");
paramsElement = bodyElement.addChildElement("TimeTo");
paramsElement.setAttribute("xmlns", "http://newsite/mon");
paramsElement.addTextNode("2016-10-26T12:00");
但结果得到没有值“http://examples/2001" "http://newsite/mon”的 SOAP 请求。
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelop/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><SOAP-ENV:Header/>
<SOAP-ENV:Body>
<GetParameter xmlns=""/>
<request>
<MonCode xmlns="">Latency</MonCode>
<TimeFrom xmlns="">2016-10-26T11:00</TimeFrom>
<TimeTo xmlns="">2016-10-26T12:00</TimeTo>
</request>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
如何解决这个问题?
此代码解决任务
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
// Retrieve different parts
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope soapEnvelope = soapMessage.getSOAPPart().getEnvelope();
soapEnvelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
soapEnvelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
// Two ways to extract headers
// SOAPHeader soapHeader = soapEnvelope.getHeader();
// soapHeader = soapMessage.getSOAPHeader();
// Two ways to extract body
SOAPBody soapBody = soapEnvelope.getBody();
soapBody = soapMessage.getSOAPBody();
// To add some element
SOAPElement GetParamter=soapBody.addBodyElement(new javax.xml.namespace.QName("http://examples/2001", "GetParamter"));
GetParamter = GetParamter.addChildElement(new javax.xml.namespace.QName("request"));
SOAPElement MonCode=GetParamter.addChildElement(new javax.xml.namespace.QName("http://newsite/mon", "MonCode"));
MonCode.addTextNode("Latency");
SOAPElement TimeFrom=GetParamter.addChildElement(new javax.xml.namespace.QName("http://newsite/mon", "TimeFrom"));
TimeFrom.addTextNode("2016-10-26T11:00");
SOAPElement TimeTo=GetParamter.addChildElement(new javax.xml.namespace.QName("http://newsite/mon", "TimeTo"));
TimeTo.addTextNode("2016-10-26T12:00");
我认为使用Java SAAJ 不简单,使用不灵活
这个解决的更好
public static void main(String args[]) throws Exception {
String addr = "http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx";
String request = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:example=\"http://ws.cdyne.com/\"><SOAP-ENV:Header/><SOAP-ENV:Body>\n" +
"<example:VerifyEmail><example:email>mutantninja@gmail.com</example:email>\n" +
"<example:LicenseKey>123</example:LicenseKey></example:VerifyEmail>\n" +
"</SOAP-ENV:Body></SOAP-ENV:Envelope>";
URL url = null;
try {
url = new URL(addr);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) url.openConnection();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
connection.setRequestProperty("Content-Length", String.valueOf(request.length()));
connection.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("SoapAction", "http://ws.cdyne.com/VerifyEmail");
connection.setDoOutput(true);
PrintWriter pw = null;
try {
pw = new PrintWriter(connection.getOutputStream());
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
pw.write(request);
pw.flush();
try {
connection.connect();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BufferedReader rd = null;
try {
rd = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String line;
String respond = "";
try {
respond = rd.readLine();
while ((line = rd.readLine()) != null)
respond = line;
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println(respond);
}
我知道我可能会问一个微不足道的问题,但无法正确创建所需的 SOAP 请求格式。
此类 SOAP 请求
<?xml version="1.0" encoding="UTF-8" ?>
<SOAP:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP:Body>
<GetParameter xmlns="http://examples/2001">
<request>
<MonCode xmlns="http://newsite/mon">Latency</MonCode>
<TimeFrom xmlns="http://newsite/mon">2016-10-26T11:00</TimeFrom>
<TimeTo xmlns="http://newsite/mon">2016-10-26T12:00</TimeTo>
</request>
</GetParameter>
</SOAP:Body>
</SOAP:Envelope>
我尝试使用 Java 的 SAAJ API 创建 SOAP 请求。
我发现了一些问题:
和 Working Soap client example
但不明白,如何为我的活动创建 SOAP 请求。
请帮助创建 SOAP 请求。
p.s。试试这个代码
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
envelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
envelope.addNamespaceDeclaration("soap", "http://schemas.xmlsoap.org/soap/envelop/");
SOAPBody body = message.getSOAPBody();
SOAPElement bodyElement = body.addChildElement("GetParameter");
bodyElement.setAttribute("xmlns", "http://examples/2001");
bodyElement = body.addChildElement("request");
SOAPElement paramsElement = bodyElement.addChildElement("MonCode");
paramsElement.setAttribute("xmlns", "http://newsite/mon");
paramsElement.addTextNode("Latency");
paramsElement = bodyElement.addChildElement("TimeFrom");
paramsElement.setAttribute("xmlns", "http://newsite/mon");
paramsElement.addTextNode("2016-10-26T11:00");
paramsElement = bodyElement.addChildElement("TimeTo");
paramsElement.setAttribute("xmlns", "http://newsite/mon");
paramsElement.addTextNode("2016-10-26T12:00");
但结果得到没有值“http://examples/2001" "http://newsite/mon”的 SOAP 请求。
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelop/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><SOAP-ENV:Header/>
<SOAP-ENV:Body>
<GetParameter xmlns=""/>
<request>
<MonCode xmlns="">Latency</MonCode>
<TimeFrom xmlns="">2016-10-26T11:00</TimeFrom>
<TimeTo xmlns="">2016-10-26T12:00</TimeTo>
</request>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
如何解决这个问题?
此代码解决任务
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
// Retrieve different parts
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope soapEnvelope = soapMessage.getSOAPPart().getEnvelope();
soapEnvelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
soapEnvelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
// Two ways to extract headers
// SOAPHeader soapHeader = soapEnvelope.getHeader();
// soapHeader = soapMessage.getSOAPHeader();
// Two ways to extract body
SOAPBody soapBody = soapEnvelope.getBody();
soapBody = soapMessage.getSOAPBody();
// To add some element
SOAPElement GetParamter=soapBody.addBodyElement(new javax.xml.namespace.QName("http://examples/2001", "GetParamter"));
GetParamter = GetParamter.addChildElement(new javax.xml.namespace.QName("request"));
SOAPElement MonCode=GetParamter.addChildElement(new javax.xml.namespace.QName("http://newsite/mon", "MonCode"));
MonCode.addTextNode("Latency");
SOAPElement TimeFrom=GetParamter.addChildElement(new javax.xml.namespace.QName("http://newsite/mon", "TimeFrom"));
TimeFrom.addTextNode("2016-10-26T11:00");
SOAPElement TimeTo=GetParamter.addChildElement(new javax.xml.namespace.QName("http://newsite/mon", "TimeTo"));
TimeTo.addTextNode("2016-10-26T12:00");
我认为使用Java SAAJ 不简单,使用不灵活
这个解决的更好
public static void main(String args[]) throws Exception {
String addr = "http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx";
String request = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:example=\"http://ws.cdyne.com/\"><SOAP-ENV:Header/><SOAP-ENV:Body>\n" +
"<example:VerifyEmail><example:email>mutantninja@gmail.com</example:email>\n" +
"<example:LicenseKey>123</example:LicenseKey></example:VerifyEmail>\n" +
"</SOAP-ENV:Body></SOAP-ENV:Envelope>";
URL url = null;
try {
url = new URL(addr);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) url.openConnection();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
connection.setRequestProperty("Content-Length", String.valueOf(request.length()));
connection.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("SoapAction", "http://ws.cdyne.com/VerifyEmail");
connection.setDoOutput(true);
PrintWriter pw = null;
try {
pw = new PrintWriter(connection.getOutputStream());
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
pw.write(request);
pw.flush();
try {
connection.connect();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BufferedReader rd = null;
try {
rd = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String line;
String respond = "";
try {
respond = rd.readLine();
while ((line = rd.readLine()) != null)
respond = line;
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println(respond);
}