我尝试使用基于SOAP的WS来返回java DataHandler。我使用grails和apache httpclient.PostMethod。如果我使用肥皂工具,我将文件作为附件(参见img - BTW,soapclient.com有一个很好的工具)。
在我的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给我一个示例代码来进行返回DataHandler的SOAP调用吗?谢谢,我真的很感激你能给予的任何帮助。
答案 0 :(得分:0)
我修改了requestHeader并且它工作正常。我得到了SOAP服务提供的文件。
method.addRequestHeader( “内容类型”, “文本/ XML”) method.addRequestHeader( “接受”, “文本/ XML,应用/ XML; Q = 0.9”) method.setRequestEntity(new StringRequestEntity(payload))