简体   繁体   English

使用Apache HTTPComponents消耗块编码数据

[英]Consuming chunk encoded data using Apache HTTPComponents

Is there a helper method in HTTP Client that allows you to consume chunk encoded data whenever one chunk arrived on the wire? HTTP客户端中是否有一个帮助程序方法,当一个块到达网络时,该方法可让您使用块编码的数据? Perhaps I overlooked this but I couldn't find anything related with this topic. 也许我忽略了这一点,但找不到与该主题相关的任何内容。 I did see this Chunked http decoding in java? 我确实在Java中看到了此Chunked HTTP解码吗? but that ChunkedInputStream is no longer available in HTTP Client 4.x 但是ChunkedInputStream在HTTP Client 4.x中不再可用

Here's what coming in over the wire. 这就是通过电线来的东西。

HttpResponseHandlerImpl.setContentType(): application/json
HttpResponseHandlerImpl.setContentLength(): -1
58495 [main] DEBUG org.apache.http.wire  - << "24[\r][\n]"
58495 [main] DEBUG org.apache.http.wire  - << "{"firstName":"David","lastName":"0"}"
58495 [main] DEBUG org.apache.http.wire  - << "[\r][\n]"
58495 [main] DEBUG org.apache.http.wire  - << "24[\r][\n]"
58495 [main] DEBUG org.apache.http.wire  - << "{"firstName":"David","lastName":"1"}"
58495 [main] DEBUG org.apache.http.wire  - << "[\r][\n]"
58495 [main] DEBUG org.apache.http.wire  - << "24[\r][\n]"
58495 [main] DEBUG org.apache.http.wire  - << "{"firstName":"David","lastName":"2"}"
58501 [main] DEBUG org.apache.http.wire  - << "[\r][\n]"
58501 [main] DEBUG org.apache.http.wire  - << "0[\r][\n]"
58501 [main] DEBUG org.apache.http.wire  - << "[\r][\n]"

What I want to do is to process a chunk at a time. 我要做的是一次处理一个块。 Is there any helper method that allows you to do this? 有没有可以帮助您执行此操作的辅助方法? or I just need to get the InputStream and do this manually? 或者我只需要获取InputStream并手动执行此操作? Manually as in get the chunk size then read the buffer based on the chunk size. 手动获取块大小,然后根据块大小读取缓冲区。

Not the idealistic solution since I was hoping to utilize the HttpComponents helper if any to parse chunk encoded data but I got this working using the Jackson's JsonParser http://jackson.codehaus.org/ , and I know that the response returning back from my web service would always be JSON objects. 这不是理想的解决方案,因为我希望利用HttpComponents帮助程序(如果有的话)来解析大块编码的数据,但是我使用Jackson的JsonParser http://jackson.codehaus.org/进行了此工作,并且我知道响应从我的返回Web服务将始终是JSON对象。

Here's the code 这是代码

    try {

        MappingJsonFactory jsonFactory = new MappingJsonFactory();

        JsonParser jsonParser = jsonFactory.createParser(inputStream);

        JsonToken token = null;

        token = jsonParser.nextToken();

        StringBuffer buffer = new StringBuffer();

        do {

            if (token == JsonToken.START_OBJECT) {
                buffer.append("{");
            } else if (token == JsonToken.END_OBJECT) {
                buffer.append("}");
                System.out.println("Received chunk: " + buffer.toString());
                buffer.setLength(0);
            } else if (token == JsonToken.FIELD_NAME) {
                jsonParser.nextToken();
                buffer.append("\"" + jsonParser.getCurrentName() + "\":");
                buffer.append("\"" + jsonParser.getText() + "\":");
            } else if (token == JsonToken.VALUE_STRING) {
                buffer.append("\"" + jsonParser.getCurrentName() + "\":");
                buffer.append("\"" + jsonParser.getText() + "\":");
            }

            token = jsonParser.nextToken();

        } while (token != null);

    } catch (Throwable th) {
        th.printStackTrace();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM