我应该如何在HttpServlet中记录发送到doPost的HttpServletRequest以便以后回放? [英] How should I record `HttpServletRequest`s that are sent to `doPost` in an `HttpServlet` for later playback?
问题描述
我想将一些请求(类型为 javax.servlet.http.HttpServletRequest
)转储到文件中,然后稍后从文件中重播它们,以便我可以测试将来对所做的更改> HttpServlet
.最好的方法是什么?
I want to dump some requests (of type javax.servlet.http.HttpServletRequest
) into a file and then later replay them from the file, so that I can test future changes to the HttpServlet
. What's the best way to accomplish this?
到目前为止,我正在尝试从与请求关联的输入流中提取数据,并将此二进制数据保存到文件中.最终,这可能需要像在每个保存的输入流之前存储字节数之类的东西,以便我知道一个请求在哪里结束而另一个在哪里开始.
So far, I'm trying to pull data out of the input stream associated with the request, and save this binary data into a file. Ultimately, this may require something like storing a byte-count prior to each saved input stream, so that I know where one request ends and the other begins.
有更简单的方法吗?
**澄清一下,这些不是涉及浏览器的请求.到目前为止,没有一个答案可以解决我的特定问题,我认为这可以归结为对HttpServletRequest进行序列化和反序列化.我试过只是从request.getInputStream()返回的输入流中提取字节.不幸的是,如果我将其转换为字符串,则似乎无法通过Message.Builder.mergeFrom(bytes)解析生成的字节.
** to clarify, these are not requests involving a browser. So far none of the answers solves my particular problem, which I suppose boils down to serializing and deserializing an HttpServletRequest. I've tried just pulling the bytes from the input stream returned by request.getInputStream(). Unfortunately, if I turn this into a string, it seems that the resulting bytes cannot be parsed by Message.Builder.mergeFrom(bytes).
我向所有知道如何解决我的问题的人悬赏.
I'm putting up a bounty for anyone who knows how to solve my problem.
推荐答案
在我的特殊情况下,我最终通过使用 CodedOutputStream
编写 com.google.protobuf来解决了该问题.GeneratedMessage
(下面的 foo
):
In my particular case, I eventually solved the problem by using a CodedOutputStream
to write the com.google.protobuf.GeneratedMessage
(foo
below):
ByteArrayOutputStream bos = new ByteArrayOutputStream();
CodedOutputStream cos = CodedOutputStream.newInstance(bos);
foo.writeTo(cos);
可以使用 CodedInputStream
将其读取,例如:
This can be read in using a CodedInputStream
with something like:
byte[] ba = readBytesFromOutputFile();
CodedInputStream cis = CodedInputStream.newInstance(ba);
然后,可以将 CodedInputStream
传递给消息生成器以完成反序列化.
The CodedInputStream
can then be passed to a message builder to complete the deserialization.
这篇关于我应该如何在HttpServlet中记录发送到doPost的HttpServletRequest以便以后回放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!