答案 0 :(得分:0)
根据我的理解,你有一个请求列表,你只想一个接一个地自动抛出一个端点,寻找成功或失败的响应。
在这种情况下,这是我几个月前做的自动化项目的工作代码。添加您的端点,SOAPAction标头(如果需要)和成功标准,通过循环函数调用一次一个地提供XML请求,您应该好好去。
// This method takes a string object representing the XML request we want to
// run, manipulates it
// to add our header and footer, sends the message to a SOA endpoint, waits
// for a response,
// parses the response, and then logs the response for later analysis
public static void sendRequest(String request) {
try {
// Create a SOAPConnection
SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = factory.createConnection();
// This output stream is used for writing the contents of the SOAP
// message and response for logging
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// This stringbuilder will hold the response message from SOA
StringBuilder soapResp = new StringBuilder();
// THis stringbuilder will hold the initial message we sent to SOA
StringBuilder soapMsg = new StringBuilder();
// This line calls the "getSoapMessageFromString" method and passes
// it our header constant, the raw XML
// request, and our footer constant to generate a SOAPMessage object
SOAPMessage message = getSoapMessageFromString(EA_SOAP_ENV_HEADER
+ request + EA_SOAP_ENV_FOOTER);
// Create an endpoint from the previoulsy selected string which is a
// URL type object
URL endpoint = new URL("endpoint");
// Get the MIME headers from the message object we just created
MimeHeaders headers = message.getMimeHeaders();
// Add the SOAPAction header with a value of "establishActivity" to
// the headers
headers.addHeader("SOAPAction", "yourAction");
// Send a SOAPMessage (request) and then wait for SOAPMessage
// (response)
SOAPMessage response = connection.call(message, endpoint);
// Write the initial message to the byte array output string
message.writeTo(baos);
// append the baos data (initial message) to the stringbuilder for
// processing
soapMsg.append(baos.toString());
// Reset the baos for use on the response message
baos.reset();
// Write the response message to the byte array output string
response.writeTo(baos);
// append the baos data (response message) to the stringbuilder for
// processing
soapResp.append(baos.toString());
// Reset the baos for use on the next message
baos.reset();
// If the response message does NOT contain the text
// 'cmdStatus="OK"', then
// send the initial request to the log formatted in large red bold
// text and
// also send the error response message to the log formatted in
// normal red text
if (soapResp.toString().indexOf("cmdStatus=\"OK\"") < 0) {
sendToLog("<pre id=\"content\"><font size=\"5\" color=\"red\"><b>REQUEST: "
+ soapMsg.toString().trim().replaceAll("<", "<")
.replaceAll(">", ">")
+ "</b></font></pre><p>");
sendToLog("<pre id=\"content\"><font color=\"red\">ERROR MESSAGE:<P>"
+ soapResp.toString().trim().replaceAll("<", "<")
.replaceAll(">", ">") + "</font></pre>");
}
// If the text 'cmdStatus="OK"' DOES exist in the response, then
// simply add one to the successfully re-run count
else {
successCount++;
}
// Add one to the total count of records processed
totalCount++;
// Close the SOAPConnection
connection.close();
soapMsg.setLength(0);
soapResp.setLength(0);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
// This method takes an XML string as input and uses it to create a new
// SOAPMessage object
// and then returns that object for further use.
private static SOAPMessage getSoapMessageFromString(String xml)
throws SOAPException, IOException {
MessageFactory factory = MessageFactory.newInstance();
// Create a new message object with default MIME headers and the data
// from the XML string we passed in
SOAPMessage message = factory
.createMessage(
new MimeHeaders(),
new ByteArrayInputStream(xml.getBytes(Charset
.forName("UTF-8"))));
return message;
}