java - 遍历属性文件中的 SOAP 请求

标签 java web-services soap properties configuration

我正在编写一个 java 插件,我打算用它来测试一些网络服务。 Web 服务的这些 SOAP 位于一个属性文件中,并分组在它们所属的 WSDL 下(订阅者、网络、用户等)。此外,还有一些与每个 Web 服务关联的正则表达式用于测试响应。

属性示例

#Web services to be tested and regexes to test responses
 #List of web service groups used (WSDLs)
 webservice.list = SubscriberMgmt,NetworkMgmt

 # < -- SubscriberMgmt -- >
 #getSubscriberDevices
 webservice.subscriber = <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.blah.blah.com"><soapenv:Header/><soapenv:Body><ws:getSubscriberDevices><PhoneNumber></PhoneNumber><LastName></LastName><MACAddress></MACAddress><ExternalId></ExternalId><AccountExternalId>john</AccountExternalId><IPAddress></IPAddress></ws:getSubscriberDevices></soapenv:Body></soapenv:Envelope>
 webservice.SubscriberMgmt.regex = subscriberId="(.+?)"
 webservice.SubscriberMgmt.regex.1 = externalId="(.+?)"

 #getMpegResultsById
 webservice.subscriber.1 = <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.blah.blah.com"><soapenv:Header/><soapenv:Body><ws:getMpegResultsById><SubscriberId>100016</SubscriberId><Duration>2880</Duration></ws:getMpegResultsById></soapenv:Body></soapenv:Envelope> 
 webservice.SubscriberMgmt.1.regex = id="(.+?)"
 webservice.SubscriberMgmt.1.regex.1 = externalId="(.+?)"

我目前有代码使用属性文件中的每个 WSDL 进行连接,所以说当“webservicegroup”变量为 SubscriberMgmt 时,我想测试 .subscriber web 服务并检查响应是否包含相应的正则表达式。 (“数据”变量目前仅对应于来自属性文件的一个 SOAP 请求)

//Soap Request
        try
        {
            for(String webservicegroup : webserviceList)
            {
                URL url = new URL("http://" + server + "/webservices/" + webservicegroup);
                URLConnection conn = url.openConnection();
                conn.setDoOutput(true);
                conn.setRequestProperty("Content-type", "text/xml; charset=utf-8");
                conn.setRequestProperty("SOAPAction", "\"\"");
                String loginEnc = new BASE64Encoder().encodeBuffer((username + ":" + password).getBytes());
                loginEnc = loginEnc.replaceAll("\n", "");
                conn.setRequestProperty("Authorization", "Basic " + loginEnc);
                conn.setConnectTimeout(timeout);
                conn.setReadTimeout(timeout);

                OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

                //Send request
                wr.write(data);
                wr.flush();
                wr.close();
                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

                //Save response
                String line;

                while ((line = in.readLine()) != null)
                {
                    response += line;
                }
                in.close();
            }
        }

关于执行此操作的最佳方法有什么想法吗?非常感谢任何帮助

最佳答案

假设您的连接和 POST/GET 代码正常工作:

第 1 步:将整个响应作为字符串获取:

String response = new String(ByteStreams.toByteArray(inputStream), "UTF-8");

在上面的代码行中,ByteStreams 类是 google 的 guava 库的一部分。如果您愿意,可以在 apache commons-io 中找到类似的代码。

第 2 步:测试正则表达式:

if( response.matches(regex) ) { ... }

关于java - 遍历属性文件中的 SOAP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17169637/

相关文章:

java - 将对象从堆栈传输到堆栈时无法正确复制

c# - 保护网络服务

xml - 用于纯 XML 而不是 SOAP 的 WSDL 服务

java - 第一次尝试后发送 SOAP NullPointerException 和 "Depth limit reached"

java - 在实例创建或使用 new 关键字创建对象期间何时分配内存?

java - 提供程序 com.levigo.jbig2.util.log.JDKLoggerBridge 不是子类型

java - 判断一个圆圈是否在另一个圆圈内

java - Java中POST请求体不能包含中文字符

c# - 如何覆盖 WcF 对象的 ToString 方法?

java - 无法查看 SOAP 调用中的回复