在使用带有Restlet 1.1的Apache HTTP客户端库4.x时,我遇到间歇性异常org.apache.commons.httpclient.ProtocolException: Unbuffered entity enclosing request can not be repeated.
。请求实体的类型为InputRepresentation
,与已知大小的ByteArrayInputStream
相关联,并且在实例化时指定了大小。
在类writeRequestBody
org.apache.commons.httpclient.methods.EntityEnclosingMethod
中抛出异常
if ((this.repeatCount > 0) && !requestEntity.isRepeatable()) {
throw new ProtocolException(
"Unbuffered entity enclosing request can not be repeated.");
}
说实话,这个例外的原因尚不清楚(特别是由于其间歇性)。但是,一些研究表明,使用Apache BufferedHttpEntity来包装请求实体应该会有所帮助。
有没有办法通知Restlet在将其请求传递给Apache库进行处理时使用BufferedHttpEntity?可能是问题的原因是什么?
答案 0 :(得分:1)
如果可以再现(重复)实体内容流,这在ByteArrayInputStream中肯定是这种情况,则不需要BufferedHttpEntity。只需要确保原始请求实体从HttpEntity#getContent()方法返回一个新的InputStream实例,并且HttpEntity#isRepeatable()返回true。
请注意org.apache.commons.httpclient.ProtocolException
来自旧版(EOL版)3.x.请确保您的应用程序中没有某种版本混淆
答案 1 :(得分:1)
这是与Apache HTTP Client集成的Restlet Framework连接器的限制。
在RF版本2.0中,有一个新的ClientResource类,其中包含“retryAttempts”和“retryDelay”属性,它提供相同的行为,但基于Restlet抽象。