2012-09-18 45 views
3

研磨機使用jython作爲主要腳本語言。如何在研磨機中提交SOAP請求

我需要測試一些只有soap接口的web服務。

我一直沒有找到一種方法來使這個工作。我是新來的磨牀,雖然他們表示將使用XmlRpcClient的示例腳本,甚至這個例子中出現了錯誤,指出「導入錯誤:沒有模塊名爲apache」

+0

聽起來像你的類路徑或pythonpath沒有正確設置。也許你可以包含你的jython代碼和包含錯誤信息的日誌輸出? –

回答

2

這裏是我的web服務的示例代碼:

# coding=utf-8 

import traceback 
from net.grinder.script.Grinder import grinder 
from net.grinder.script import Test 
from net.grinder.plugin.http import HTTPPluginControl, HTTPRequest 
from HTTPClient import NVPair 

connectionDefaults = HTTPPluginControl.getConnectionDefaults() 
httpUtilities = HTTPPluginControl.getHTTPUtilities() 
connectionDefaults.useContentEncoding = 1 
log = grinder.logger.info 

class TestRunner: 

    def __call__(self): 
     your_url_for_web_service = '' 
     your_soap_message_body = '' 
     soapMessage = """<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<SOAP-ENV:Body>"""+ 
    your_soap_message_body+""" 
</SOAP-ENV:Body> 
</SOAP-ENV:Envelope>""" 
     self.send_service(your_url_for_web_service, soapMessage) 

    def send_service(self, soapMessage):     
     request = HTTPRequest() 
     headers = (
        NVPair("Content-Type", "text/xml; charset=utf-8"), 
        NVPair("Content-Length", str(len(soapMessage))), 
        ) 
     request.setHeaders(headers) 
     httpResponseObject = request.POST(url, soapMessage) 
     soapAsString = httpResponseObject.getText() 
     log(soapAsString) 

def instrumentMethod(test, method_name, c=TestRunner): 
    """Instrument a method with the given Test.""" 
    unadorned = getattr(c, method_name) 
    import new 
    method = new.instancemethod(test.wrap(unadorned), None, c) 
    setattr(c, method_name, method) 


# Replace each method with an instrumented version. 
instrumentMethod(Test(1, 'soap request showcase example'), 'send_service') 

一定要更改call方法如下:

your_url_for_web_service = '' 
your_soap_message_body = '' 

與Web服務相應的值。

+1

@ user912563你遇到過什麼問題?我特意將我的版本拿到肥皂反應的身上。當您使用兩個字節編碼的utf-8字符時,我的解決方案可以工作。例如šđčćž。只使用getText()不適用於包含這些字符的soapResponse。 –