【发布时间】:2014-06-11 12:44:42
【问题描述】:
您好,我正在尝试在 grails 中进行 POST,我尝试了 2 种不同的方法。 我想复制这个 curl 命令:
curl -F upload=@in.xml http://server.com/service.pl > out.xml
这个命令工作正常。 到目前为止我尝试过的如下:
def actualInputFile = new File("/path/to/file/in.xml");
def retval = [success: false, message: null];
HttpClient postClient = new HttpClient();
MultipartPostMethod postMethod = new MultipartPostMethod('http://server.com/service.pl');
postClient.httpConnectionManager.getParams().setSoTimeout(300000);
postMethod.addParameter("upload",actualInputFile);
int status = postClient.executeMethod(postMethod);
def data = postMethod.getResponseBodyAsString();
postMethod.releaseConnection();
def returnedFile = new File("/path/to/file/out.xml");
returnedFile.write(data);
这会产生 500 错误
我也试过了:
def actualInputFile = new File("/path/to/file/in.xml");
def http = new HTTPBuilder( 'http://server.com/service.pl' )
println "START POST"
def postBody = [upload:actualInputFile] // will be url-encoded
http.post( body: postBody,
requestContentType: XML ) { resp ->
println "POST Success: ${resp.statusLine}"
}
这根本行不通。
获得 500 的第一个使用折旧方法。
有什么想法吗?
【问题讨论】: