- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Java 11 的 java.net.http.HttpClient 从 Http Response 读取分块数据,但一次只能获取一行。我需要一次获取一整 block 。
这是我的代码:
final InputStream eventStream;
try {
HttpResponse<InputStream> httpResponse = httpClient.send(HttpRequest
.newBuilder(
new URI(this.config.getEnvironmentAccess().getUrl() + ":<port>/status/?pretty=true"))
.GET().build(), BodyHandlers.ofInputStream());
LOGGER.info("event stream HttpResponse received");
LOGGER.info("statusCode: {}", httpResponse.statusCode());
LOGGER.info("headers: {}", httpResponse.headers());
LOGGER.info("version: {}", httpResponse.version());
LOGGER.info("request: {}", httpResponse.request());
eventStream = httpResponse.body();
} catch (IOException | InterruptedException | URISyntaxException e) {
throw new RuntimeException("Unable to get status event stream", e);
}
BufferedReader br = new BufferedReader(new InputStreamReader(eventStream));
String line = "";
try {
while ((line = br.readLine()) != null) {
LOGGER.info("readLine(): {}", line);
}
} catch (IOException e) {
throw new RuntimeException("Unable to read status event stream", e);
}
但是我收到的响应是单独的几行:
824 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:352 - starting to listen for thing script responses . . .
2018-10-15 20:43:34,057-0400 9107 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:362 - Thing event stream HttpResponse received
2018-10-15 20:43:34,058-0400 9108 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:363 - statusCode: 200
2018-10-15 20:43:34,059-0400 9109 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:364 - headers: java.net.http.HttpHeaders@a6764984 { {content-type=[application/json], date=[Tue, 16 Oct 2018 00:43:34 GMT], server=[Jetty(9.2.z-SNAPSHOT)], transfer-encoding=[chunked]} }
2018-10-15 20:43:34,060-0400 9110 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:365 - version: HTTP_1_1
2018-10-15 20:43:34,060-0400 9110 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:366 - request: http://thing-url.company.com:<port>/status/?pretty=true GET
2018-10-15 20:43:34,063-0400 9113 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): {
2018-10-15 20:43:34,063-0400 9113 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): "Header" : {
2018-10-15 20:43:34,064-0400 9114 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): "name" : "header",
2018-10-15 20:43:34,066-0400 9116 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): "value" : {
2018-10-15 20:43:34,067-0400 9117 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): "version" : 1.2
2018-10-15 20:43:34,067-0400 9117 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): }
2018-10-15 20:43:34,068-0400 9118 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): }
2018-10-15 20:43:34,100-0400 9150 [main] INFO c.p.perf.replay.ThingReplay.replayScripts:108 - submitting thing script #1 of 10
2018-10-15 20:43:34,111-0400 9161 [main] INFO c.p.perf.api.dao.ThingDao.submitScript:100 - submitting script to thing: {"submissionTime":"2018-10-03T04:34:19.054Z[GMT]","user":"userperson@company.com","sessionId":"5fa9bc47fd5d450da9323b5d35b14e89","requestId":"25ed5909e04d4caba4d57c41dd85dea0","service":"dataManagerService.getData","requestNumber":177}
2018-10-15 20:43:34,188-0400 9238 [main] INFO c.p.perf.replay.ThingReplay.replayScripts:108 - submitting thing script #2 of 10
2018-10-15 20:43:34,191-0400 9241 [main] INFO c.p.perf.api.dao.ThingDao.submitScript:100 - submitting script to thing: {"submissionTime":"2018-10-03T04:34:23.157Z[GMT]","user":"userperson@company.com","sessionId":"5fa9bc47fd5d450da9323b5d35b14e89","requestId":"0ddeebddaa854c6ab2ac33b911af28aa","service":"dataManagerService.getData","requestNumber":180}
2018-10-15 20:43:34,233-0400 9283 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): } {
2018-10-15 20:43:34,233-0400 9283 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): "Group" : {
2018-10-15 20:43:34,233-0400 9283 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): "name" : "groupStart",
2018-10-15 20:43:34,234-0400 9284 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): "value" : {
2018-10-15 20:43:34,234-0400 9284 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): "groupId" : "57b94dcc-fad8-40f2-bb86-6d6894f26f26",
2018-10-15 20:43:34,235-0400 9285 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): "requestInfo" : [ {
2018-10-15 20:43:34,235-0400 9285 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): "sessionId" : "5fa9bc47fd5d450da9323b5d35b14e89",
2018-10-15 20:43:34,235-0400 9285 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): "requestId" : "25ed5909e04d4caba4d57c41dd85dea0"
2018-10-15 20:43:34,235-0400 9285 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): } ]
2018-10-15 20:43:34,236-0400 9286 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): }
2018-10-15 20:43:34,236-0400 9286 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): }
2018-10-15 20:43:34,245-0400 9295 [main] INFO c.p.perf.replay.ThingReplay.replayScripts:108 - submitting thing script #3 of 10
2018-10-15 20:43:34,246-0400 9296 [main] INFO c.p.perf.api.dao.ThingDao.submitScript:100 - submitting script to thing: {"submissionTime":"2018-10-03T04:34:27.362Z[GMT]","user":"userperson@company.com","sessionId":"5fa9bc47fd5d450da9323b5d35b14e89","requestId":"0b8fa91c6b20478589d66f90af80f481","service":"dataManagerService.getData","requestNumber":182}
2018-10-15 20:43:34,246-0400 9296 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): } {
2018-10-15 20:43:34,247-0400 9297 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): "Event" : {
2018-10-15 20:43:34,247-0400 9297 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): "name" : "jobStart",
2018-10-15 20:43:34,247-0400 9297 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): "value" : {
2018-10-15 20:43:34,248-0400 9298 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): "stats" : {
2018-10-15 20:43:34,249-0400 9299 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): "totalTasks" : 1,
2018-10-15 20:43:34,250-0400 9300 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): "runningTasks" : 0,
2018-10-15 20:43:34,250-0400 9300 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): "completeTasks" : 0,
2018-10-15 20:43:34,250-0400 9300 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): "failedTasks" : 0
2018-10-15 20:43:34,251-0400 9301 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): },
2018-10-15 20:43:34,251-0400 9301 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): "coreMsUsed" : -1,
2018-10-15 20:43:34,252-0400 9302 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): "requestInfo" : [ {
2018-10-15 20:43:34,252-0400 9302 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): "sessionId" : "5fa9bc47fd5d450da9323b5d35b14e89",
2018-10-15 20:43:34,253-0400 9303 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): "requestId" : "25ed5909e04d4caba4d57c41dd85dea0"
2018-10-15 20:43:34,253-0400 9303 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): } ]
2018-10-15 20:43:34,253-0400 9303 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): }
2018-10-15 20:43:34,254-0400 9304 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): }
2018-10-15 20:43:34,254-0400 9304 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): } {
2018-10-15 20:43:34,254-0400 9304 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForEvents:378 - readLine(): "Event" : {
我意识到这是因为我正在使用 BufferedReader 并告诉它记录我得到的每一行,但我不知道如何将这些行作为 block 读取。
有人可以帮我弄清楚如何自动读取这些 block ,以便我可以将它们解析为 JSON,一次一个事件?
<小时/>必须使用 JsonProcessor 来解析 Json 流:
private void listenForEvents(final IMakeHttpRequests httpClient) {
assert httpClient != null : "httpClient cannot be null";
LOGGER.info("starting to listen for script responses . . .");
final InputStream eventStream;
final JsonParser parser;
try {
HttpResponse<InputStream> httpResponse = httpClient.send(HttpRequest
.newBuilder(
new URI(this.config.getEnvironmentAccess().getUrl() + ":<port>/status/?pretty=true"))
.GET().build(), BodyHandlers.ofInputStream());
eventStream = httpResponse.body();
} catch (IOException | InterruptedException | URISyntaxException e) {
throw new RuntimeException("Unable to get status event stream", e);
}
try {
parser = JsonUtils.getObjectMapper().getFactory().createParser(eventStream);
while (parser.nextToken() != null) {
final TreeNode tree = parser.readValueAsTree();
LOGGER.info("tree: {}", tree);
}
} catch (IOException e) {
throw new RuntimeException("Unable to parse event stream to Json", e);
}
}
2018-10-16 13:25:32,081-0400 12622 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForThingEvents:352 - starting to listen for script responses . . .
2018-10-16 13:25:32,808-0400 13349 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForThingEvents:363 - Thing event stream HttpResponse received
2018-10-16 13:25:32,808-0400 13349 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForThingEvents:364 - statusCode: 200
2018-10-16 13:25:32,808-0400 13349 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForThingEvents:365 - headers: java.net.http.HttpHeaders@67b87c21 { {content-type=[application/json], date=[Tue, 16 Oct 2018 17:25:33 GMT], server=[Jetty(9.2.z-SNAPSHOT)], transfer-encoding=[chunked]} }
2018-10-16 13:25:32,809-0400 13350 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForThingEvents:366 - version: HTTP_1_1
2018-10-16 13:25:32,809-0400 13350 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForThingEvents:367 - request: http://<server>.company.com:<port>/status/?pretty=true GET
2018-10-16 13:25:32,819-0400 13360 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForThingEvents:379 - tree: {"Header":{"name":"header","value":{"version":1.2}}}
2018-10-16 13:25:32,835-0400 13376 [main] INFO c.p.perf.replay.ThingReplay.replayScripts:108 - submitting pipeline script #1 of 10
2018-10-16 13:25:32,843-0400 13384 [main] INFO c.p.perf.api.dao.ThingDao.submitScript:100 - submitting script to pipeline: {"submissionTime":"2018-10-03T04:34:19.054Z[GMT]","user":"userperson@company.com","sessionId":"5fa9bc47fd5d450da9323b5d35b14e89","requestId":"25ed5909e04d4caba4d57c41dd85dea0","service":"dataManagerService.getData","requestNumber":177}
2018-10-16 13:25:33,074-0400 13615 [main] INFO c.p.perf.replay.ThingReplay.replayScripts:108 - submitting pipeline script #2 of 10
2018-10-16 13:25:33,075-0400 13616 [main] INFO c.p.perf.api.dao.ThingDao.submitScript:100 - submitting script to pipeline: {"submissionTime":"2018-10-03T04:34:23.157Z[GMT]","user":"userperson@company.com","sessionId":"5fa9bc47fd5d450da9323b5d35b14e89","requestId":"0ddeebddaa854c6ab2ac33b911af28aa","service":"dataManagerService.getData","requestNumber":180}
2018-10-16 13:25:33,117-0400 13658 [main] INFO c.p.perf.replay.ThingReplay.replayScripts:108 - submitting pipeline script #3 of 10
2018-10-16 13:25:33,117-0400 13658 [main] INFO c.p.perf.api.dao.ThingDao.submitScript:100 - submitting script to pipeline: {"submissionTime":"2018-10-03T04:34:27.362Z[GMT]","user":"userperson@company.com","sessionId":"5fa9bc47fd5d450da9323b5d35b14e89","requestId":"0b8fa91c6b20478589d66f90af80f481","service":"dataManagerService.getData","requestNumber":182}
2018-10-16 13:25:33,237-0400 13778 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForThingEvents:379 - tree: {"Group":{"name":"groupStart","value":{"groupId":"b205e673-2eb1-4352-b207-fd4917be292a","requestInfo":[{"sessionId":"5fa9bc47fd5d450da9323b5d35b14e89","requestId":"25ed5909e04d4caba4d57c41dd85dea0"}]}}}
2018-10-16 13:25:33,238-0400 13779 [pool-1-thread-1] INFO c.p.perf.replay.ThingReplay.listenForThingEvents:379 - tree: {"Event":{"name":"jobStart","value":{"stats":{"totalTasks":1,"runningTasks":0,"completeTasks":0,"failedTasks":0},"coreMsUsed":-1,"requestInfo":[{"sessionId":"5fa9bc47fd5d450da9323b5d35b14e89","requestId":"25ed5909e04d4caba4d57c41dd85dea0"}]}}}
. . .
最佳答案
似乎您正在逐行读取流,无论 block 如何传入(BufferedReader 负责将数据重新构造为行)。然而,没有人保证单个 block 包含单个 json 记录。服务器可以基于其内部缓冲区对数据进行分块。
解析流时,您可以查看 Jackson Streaming API。编程模型稍微复杂一些,但它可能适合您的需要。例子 https://www.baeldung.com/jackson-streaming-api或默认的 jre 实现 https://docs.oracle.com/javaee/7/api/javax/json/stream/package-summary.html
关于java - 使用 Java 11 HttpClient 读取分块数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52826540/
初学者 android 问题。好的,我已经成功写入文件。例如。 //获取文件名 String filename = getResources().getString(R.string.filename
我已经将相同的图像保存到/data/data/mypackage/img/中,现在我想显示这个全屏,我曾尝试使用 ACTION_VIEW 来显示 android 标准程序,但它不是从/data/dat
我正在使用Xcode 9,Swift 4。 我正在尝试使用以下代码从URL在ImageView中显示图像: func getImageFromUrl(sourceUrl: String) -> UII
我的 Ubuntu 安装 genymotion 有问题。主要是我无法调试我的数据库,因为通过 eclipse 中的 DBMS 和 shell 中的 adb 我无法查看/data/文件夹的内容。没有显示
我正在尝试用 PHP 发布一些 JSON 数据。但是出了点问题。 这是我的 html -- {% for x in sets %}
我观察到两种方法的结果不同。为什么是这样?我知道 lm 上发生了什么,但无法弄清楚 tslm 上发生了什么。 > library(forecast) > set.seed(2) > tts lm(t
我不确定为什么会这样!我有一个由 spring data elasticsearch 和 spring data jpa 使用的类,但是当我尝试运行我的应用程序时出现错误。 Error creatin
在 this vega 图表,如果我下载并转换 flare-dependencies.json使用以下 jq 到 csv命令, jq -r '(map(keys) | add | unique) as
我正在提交一个项目,我必须在其中创建一个带有表的 mysql 数据库。一切都在我这边进行,所以我只想检查如何将我所有的压缩文件发送给使用不同计算机的人。基本上,我如何为另一台计算机创建我的数据库文件,
我有一个应用程序可以将文本文件写入内部存储。我想仔细看看我的电脑。 我运行了 Toast.makeText 来显示路径,它说:/数据/数据/我的包 但是当我转到 Android Studio 的 An
我喜欢使用 Genymotion 模拟器以如此出色的速度加载 Android。它有非常好的速度,但仍然有一些不稳定的性能。 如何从 Eclipse 中的文件资源管理器访问 Genymotion 模拟器
我需要更改 Silverlight 中文本框的格式。数据通过 MVVM 绑定(bind)。 例如,有一个 int 属性,我将 1 添加到 setter 中的值并调用 OnPropertyChanged
我想向 Youtube Data API 提出请求,但我不需要访问任何用户信息。我只想浏览公共(public)视频并根据搜索词显示视频。 我可以在未经授权的情况下这样做吗? 最佳答案 YouTube
我已经设置了一个 Twilio 应用程序,我想向人们发送更新,但我不想回复单个文本。我只是想让他们在有问题时打电话。我一切正常,但我想在发送文本时显示传入文本,以确保我不会错过任何问题。我正在使用 p
我有一个带有表单的网站(目前它是纯 HTML,但我们正在切换到 JQuery)。流程是这样的: 接受用户的输入 --- 5 个整数 通过 REST 调用网络服务 在服务器端运行一些计算...并生成一个
假设我们有一个名为 configuration.js 的文件,当我们查看内部时,我们会看到: 'use strict'; var profile = { "project": "%Projec
这部分是对 Previous Question 的扩展我的: 我现在可以从我的 CI Controller 成功返回 JSON 数据,它返回: {"results":[{"id":"1","Sourc
有什么有效的方法可以删除 ios 中 CBL 的所有文档存储?我对此有疑问,或者,如果有人知道如何从本质上使该应用程序像刚刚安装一样,那也会非常有帮助。我们正在努力确保我们的注销实际上将应用程序设置为
我有一个 Rails 应用程序,它与其他 Rails 应用程序通信以进行数据插入。我使用 jQuery $.post 方法进行数据插入。对于插入,我的其他 Rails 应用程序显示 200 OK。但在
我正在为服务于发布请求的 API 调用运行单元测试。我正在传递请求正文,并且必须将响应作为帐户数据返回。但我只收到断言错误 注意:数据是从 Azure 中获取的 spec.js const accou
我是一名优秀的程序员,十分优秀!