Java HttpClient 或 PostMethod 截断返回数据至 64k
我正在使用 Apache Commons HttpClient 从服务器获取一些数据。我的问题是返回的 XML 数据总是被截断为前 64k。我希望这可能只是对相关对象设置限制的情况,但显然不是 - 或者至少,我找不到有关这种方法的任何信息。我假设这是一个客户端问题,因为服务器属于另一家公司,并且可能可以很好地向其他人提供数据。
有什么想法吗?
顺便说一句,我的代码非常简单:
protected String send(Server server, String query) throws Exception {
PostMethod post = new PostMethod(server.getUrl());
post.setParameter("XMLString", query);
try {
client.executeMethod(post);
return post.getResponseBodyAsString();
} finally {
post.releaseConnection();
}
}
仅供参考,使用 InputStreams 而不是 getResponseBodyAsString() 方法的以下代码也会发生同样的情况。
protected String send(Server server, String query) throws Exception {
PostMethod post = new PostMethod(server.getUrl());
post.setParameter("XMLString", query);
try {
client.executeMethod(post);
InputStream stream = post.getResponseBodyAsStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
String line = null;
StringBuilder sb = new StringBuilder();
while ( (line=reader.readLine()) != null ) {
sb.append(line);
}
return sb.toString();
} finally {
post.releaseConnection();
}
}
那么,你知道是否有某个地方设置了限制吗?事实上,字符串响应总是 64k 似乎表明一定有。但在哪里?
谢谢阿拉斯泰尔
I'm using Apache Commons HttpClient to grab some data from a server. My problem is that the returned XML data is always truncated to the first 64k. I was hoping this might just be a case of setting a limit on the relevant object, but apparently not - or at least, I can't find any information about such a method. I'm assuming it's a client issue as the server belongs to another company and presumably serves the data fine to everyone else.
Any ideas?
My code btw is super simple:
protected String send(Server server, String query) throws Exception {
PostMethod post = new PostMethod(server.getUrl());
post.setParameter("XMLString", query);
try {
client.executeMethod(post);
return post.getResponseBodyAsString();
} finally {
post.releaseConnection();
}
}
fyi, same thing happens with the following code using InputStreams rather than the getResponseBodyAsString() method.
protected String send(Server server, String query) throws Exception {
PostMethod post = new PostMethod(server.getUrl());
post.setParameter("XMLString", query);
try {
client.executeMethod(post);
InputStream stream = post.getResponseBodyAsStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
String line = null;
StringBuilder sb = new StringBuilder();
while ( (line=reader.readLine()) != null ) {
sb.append(line);
}
return sb.toString();
} finally {
post.releaseConnection();
}
}
So, do you know if there is a limit set somewhere? The fact that the String is response is always 64k does seem to suggest there must be. But where?!
Thanks
Alastair
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
勘误表:64k 限制是错误的。它就像在 Eclipse 调试器的人工制品中一样,不会显示似乎是截断的罪魁祸首。
解决方案:我现在的代码工作如下:
我承认我不知道为什么有效,但推测它与内容长度设置有关。
另外,请注意这些方法已被弃用,我使用它们只是因为我正在处理遗留系统。如果您正在开发适应性更强的系统,我建议您遵循@HellGhost 的建议。
ERRATUM: the 64k limit is false. It as in artefact of the eclipse debugger which won't show appears the be the truncating culprit.
SOLUTION: I have now got the code working as follows:
I'll admit I don't know why that works, but speculate it's something to do with the content length settings.
Separately, be aware that these methods are deprecated and I'm only using them because I'm working on a legacy system. If you are working on a more adaptable system I'd suggest following @HellGhost's advice.
将 ResponseBody 保存为字符串并返回该字符串,因为在返回结果之前会调用方法
releaseConnection()
。此外,您应该考虑是否真的想进一步使用 HttpClient 3.x 库,因为开发已经确定,并且它的 HttpClient 和 HttpCore 模块中已被 HttpComponents 库项目取代。
新 HttpClient 的使用示例:
Save the ResponseBody as string and return the string, because the method
releaseConnection()
is called before returning the result.Furthermore, you should consider whether you really want to use the library HttpClient 3.x even further, since the development has been set and it has been replaced by the library HttpComponents project in its HttpClient and HttpCore modules.
An example use for the new HttpClient: