2012-02-09 128 views
0

我正在嘗試使用返回java DataHandler的基於SOAP的WS。我使用grails和apache httpclient.PostMethod。如果我使用soap工具,我會以附件的形式獲取文件(請參閱img - BTW,soapclient.com有一個很棒的工具)。返回DataHandler的Java SOAP web服務

enter image description here

在我的Grails控制器:

class connectToSoap { 

def getFile = { 

    def payload = """ 
    <?xml version="1.0" encoding="UTF-8" standalone="no"?> 
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org  
     <userLogicalId>${params.userLogicalId}</userLogicalId> 
     <clientLogicalId>${params.clientLogicalId}</clientLogicalId> 
     </mns1:getFile></SOAP-ENV:Body> 
    </SOAP-ENV:Envelope> // Summarized payload 
    """ 
    def method = new PostMethod(url) 
    method.setRequestHeader("Content-disposition", "attachment;filename=antocha.zip") 
    method.addRequestHeader("Content-Type","application/octet-stream") 
    method.setRequestEntity(new StringRequestEntity(payload)) 
    def client = new HttpClient() 
    def statusCode = client.executeMethod(method) 

    InputStream inputStream = method.getResponseBodyAsStream() 
    OutputStream out = new FileOutputStream(new File("c:\\var\\nfile.zip")) 
    byte[] bytes = new byte[1024] 
    while ((read = inputStream.read(bytes)) != -1) { 
     out.write(bytes, 0, read) 
    } 
    inputStream.close(); 
    out.flush(); 
    out.close(); 

    method.releaseConnection() 
} 

當我運行它,我得到inputStream.read異常(groovy.lang.MissingPropertyException:沒有這樣的屬性)。我猜應該處理附件文件應該以不同的方式處理?

任何人都可以給我一個示例代碼使用httpclient.PostMethod進行SOAP調用,返回一個DataHandler?謝謝,我真的很感謝你能給予的幫助。

回答

0

我修改了requestHeader,它工作。我得到了由SOAP服務提供的文件。

method.addRequestHeader( 「內容類型」, 「文本/ XML」) method.addRequestHeader( 「接受」, 「文本/ XML,應用/ XML; Q = 0.9」) method.setRequestEntity(新StringRequestEntity (淨荷))