在Java中分块的http解码? [英] Chunked http decoding in java?
问题描述
我正在解码http数据包。
我遇到了一个大块问题的问题。
当我收到一个http数据包时,它有一个标题和正文。
当transefer-encoding被分块时我不知道该怎么办?
I am decoding http packets. And I faced a problem that chunk problem. When I get a http packet it has a header and body. When transefer-encoding is chunked I don't know what to do ?
是否有一个有用的API或类用于在JAVA中对数据进行dechunk?
Is there a useful API or class for dechunk the data in JAVA ?
如果有人经历过http解码,请告诉我一个怎么做的方法?
And if someone , experienced about http decoding , please show me a way how to do this ?
推荐答案
使用完整的HTTP客户端,例如 Apache HttpComponents Client 或仅使用Java SE提供了 java.net .URLConnection
(这里的迷你教程)。两者都完全透明地处理它,并为您提供正常 InputStream
。 HttpClient反过来还附带一个 ChunkedInputStream
你只需用。装饰你的 InputStream
。
Use a fullworthy HTTP client like Apache HttpComponents Client or just the Java SE provided java.net.URLConnection
(mini tutorial here). Both handles it fully transparently and gives you a "normal" InputStream
back. HttpClient in turn also comes with a ChunkedInputStream
which you just have to decorate your InputStream
with.
如果你真的坚持为这个库增加一个库,那么我建议你创建一个类 ChunkedInputStream扩展InputStream
并相应地写入逻辑。您可以在此维基百科文章中找到有关如何解析它的更多详细信息。
If you really insist in homegrowing a library for this, then I'd suggest to create a class like ChunkedInputStream extends InputStream
and write logic accordingly. You can find more detail how to parse it in this Wikipedia article.
这篇关于在Java中分块的http解码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!