java java中的分块http解码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3717640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:08:53  来源:igfitidea点击:

Chunked http decoding in java?

javahttpchunked-encoding

提问by CodingForever

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 ?

我正在解码 http 数据包。我遇到了一个问题,即块问题。当我收到一个 http 数据包时,它有一个标头和正文。当传输编码被分块时,我不知道该怎么办?

Is there a useful API or class for dechunk the data in JAVA ?

是否有有用的 API 或类用于在 JAVA 中拆分数据?

And if someone , experienced about http decoding , please show me a way how to do this ?

如果有人对 http 解码有经验,请告诉我如何做到这一点?

回答by BalusC

Use a fullworthy HTTP client like Apache HttpComponents Clientor just the Java SE provided java.net.URLConnection(mini tutorial here). Both handles it fully transparently and gives you a "normal" InputStreamback. HttpClient in turn also comes with a ChunkedInputStreamwhich you just have to decorate your InputStreamwith.

使用完整的 HTTP 客户端,如Apache HttpComponents 客户端或仅提供的 Java SE java.net.URLConnection这里的迷你教程)。两者都完全透明地处理它并为您提供“正常”的InputStream背部。HttpClient 反过来也带有一个ChunkedInputStream你只需要装饰你InputStream的。

If you really insist in homegrowing a library for this, then I'd suggest to create a class like ChunkedInputStream extends InputStreamand write logic accordingly. You can find more detail how to parse it in this Wikipedia article.

如果你真的坚持为此在家里开发一个图书馆,那么我建议创建一个类似的类ChunkedInputStream extends InputStream并相应地编写逻辑。您可以在这篇 Wikipedia 文章中找到有关如何解析它的更多详细信息。

回答by Maurice Perry

Apache HttpComponents

Apache HttpComponents

Oh, and if we are talking about the client side, HttpUrlConnectiondoes this as well.

哦,如果我们在谈论客户端,HttpUrlConnection会这样做

回答by Andrejs

If you are looking for a simple API try Jodd Httplibrary (http://jodd.org/doc/http.html). It handles Chunked transfer encoding for you and you get the whole body as a string back.

如果您正在寻找一个简单的 API,请尝试使用Jodd Http库 ( http://jodd.org/doc/http.html)。它为您处理分块传输编码,您可以将整个正文作为字符串返回。

From the docs:

从文档:

HttpRequest httpRequest = HttpRequest.get("http://jodd.org");
HttpResponse response = httpRequest.send();

System.out.println(response);

回答by Vadzim

Here is quick-and-dirty alternative that requires no dependency except Oracle JRE:

这是快速而肮脏的替代方案,除了 Oracle JRE 之外不需要任何依赖项:

private static byte[] unchunk(byte[] content) throws IOException {
    ByteArrayInputStream bais = new ByteArrayInputStream(content);
    ChunkedInputStream cis = new ChunkedInputStream(bais, new HttpClient() {}, null);
    return readFully(cis);
}

It uses the same sun.net.www.http.ChunkedInputStreamas java.net.HttpURLConnectiondoes behind the scene.

它使用与幕后相同的sun.net.www.http.ChunkedInputStream方法java.net.HttpURLConnection

This implementation doesn't provide detailed exceptions (line numbers) on wrong content format.

此实现不提供错误内容格式的详细异常(行号)。

It works with Java 8 but could fail in with next release. You've been warned.

它适用于 Java 8,但在下一个版本中可能会失败。你已经被警告了。

Could be useful for prototyping though.

不过可能对原型设计很有用。

You can choose any readFullyimplementation from Convert InputStream to byte array in Java.

您可以在 Java 中readFullyConvert InputStream to byte array 中选择任何实现。