我正在使用Grails Plugin rest = 0.7来使用Rest Webservice。
当服务的响应是xml时,一切正常,但如果响应是文件类型,如pdf,它必须在发送请求时开始下载,但下载根本没有开始。
以下代码在grails服务中实现。
String httpUrl = 'http://abc.com/myService'
String data = '<methodcall protocol="2" method="avalidmethodname"><cmdid/><data><project_id>1</project_id><user_id>2</user_id><operation>ABC</operation><filter><status_type_id>1</status_type_id><scope_bits>00</scope_bits></filter></data></methodcall>'
String respText = ''
HttpClient httpClient = new DefaultHttpClient()
HttpResponse response
try {
HttpPost httpPost = new HttpPost(httpUrl)
httpPost.setHeader("Content-Type", "text/xml")
HttpEntity reqEntity = new StringEntity(data, "UTF-8")
reqEntity.setContentType("text/xml")
reqEntity.setChunked(true)
httpPost.setEntity(reqEntity)
response = httpClient.execute(httpPost)
// HttpEntity resEntity = response.getEntity()
// respText = resEntity.getContent().text
}
finally {
httpClient.getConnectionManager().shutdown()
}
return response
// return respText
代码中的注释行是针对xml响应的情况。
请帮我解决这个问题,我不确定我使用的方法是否有效,以防万一网络服务提供文件回复。
答案 0 :(得分:0)
尝试添加
response.setContentType("text/pdf");
前
response = httpClient.execute(httpPost);