HttpRequest 没有 Request 实体的 getter,无法让我在拦截器中操作它。使用instanceof 检查类型也不起作用。有人对我如何实现这一目标有任何想法吗?
public class FastinfosetRequestInterceptor implements HttpRequestInterceptor
{
@Override
public void process(HttpRequest request, HttpContext context)
throws HttpException, IOException
{
if(request instanceof HttpPost)
{
HttpEntity rqEntity = ((HttpPost)request).getEntity();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
rqEntity.writeTo(baos);
byte[] encodedRq;
try
{
encodedRq = FastInfosetUtils.encodeToFastInfoSet(baos.toByteArray());
}
catch (ParserConfigurationException | SAXException
| TransformerException e)
{
throw new IOException("Error while encoding request to FastInfoSet", e);
}
((HttpPost) request).setEntity(new ByteArrayEntity(encodedRq));
}
}
}
最佳答案
这应该可以解决问题
if(request instanceof HttpEntityEnclosingRequest)
{
HttpEntity rqEntity = ((HttpEntityEnclosingRequest) request).getEntity();
关于apache - 在 Apache HttpClient 中使用 HttpRequestInterceptor 操作请求实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27517074/