簡體   English   中英

如何將現有XML文件作為請求導入到Java中的SoapUI API?

[英]How to import existing XML files as requests to SoapUI API in JAVA?

  1. 我已經有針對SOAP的XML請求。
  2. 我想使用JAVA中的SoapUI API導入這些XML文件作為進行負載測試的請求。
  3. 除了SoapUI之外,是否還有其他工具可以提供相似的API並且易於使用? ps:Loadtest是JAVA項目的一部分,我必須在JAVA中做所有事情。

據我了解,您有一系列的請求,您只想將它​​們一個接一個地自動扔到端點,以查找成功或失敗的響應。

在這種情況下,這是幾個月前我完成的自動化項目中的有效代碼。 添加您的端點,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("<", "&lt;")
                            .replaceAll(">", "&gt;")
                    + "</b></font></pre><p>");
            sendToLog("<pre id=\"content\"><font color=\"red\">ERROR MESSAGE:<P>"
                    + soapResp.toString().trim().replaceAll("<", "&lt;")
                            .replaceAll(">", "&gt;") + "</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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM