从多部分HTTP响应

时间:2017-01-03 10:51:13

标签: java json apache-httpclient-4.x apache-httpcomponents apache-commons-fileupload

我使用Apache HttpClient处理一个返回包含json的multipart / form-data响应的Web服务。

我很难分别提取每个JSON字符串,所以我可以读取json字符串。

我确实读过Stackoverflow上的类似帖子,有些人建议使用Apache commons fileupload,但我不确定如何将JSON字符串与包含其他一些文本的整个响应分开,例如边界字符串,内容类型等等

响应如下所示。

--Boundary_16003419_2104021487_1483424496169
Content-Type: application/json
Content-RequestId: GetMailboxes
Status-Code: 200
X-Server-Response-Time: 4ms
X-Server-Chain: domain.com
Content-RequestDuration: 5

{JSON}
--Boundary_16003419_2104021487_1483424496169
Content-Type: application/json
Content-RequestId: GetFolders
Status-Code: 200
X-Server-Response-Time: 8ms
X-Server-Chain: domain.com
Content-RequestDuration: 10

{JSON}
--Boundary_16003419_2104021487_1483424496169
Content-Type: application/json
Content-RequestId: GetAlerts
Status-Code: 200
X-Server-Response-Time: 10ms
X-Server-Chain: domain.com
Content-RequestDuration: 12

{JSON}
--Boundary_16003419_2104021487_1483424496169
Content-Type: application/json
Content-RequestId: GetAccounts
Status-Code: 200
X-Server-Response-Time: 11ms
X-Server-Chain: domain.com
Content-RequestDuration: 12

{JSON}
--Boundary_16003419_2104021487_1483424496169
Content-Type: application/json
Content-RequestId: GetAllSavedSearches
Status-Code: 200
X-Server-Response-Time: 10ms
X-Server-Chain: domain.com
Content-RequestDuration: 12

{JSON}
--Boundary_16003419_2104021487_1483424496169
Content-Type: application/json
Content-RequestId: GetAthenaSegment
Status-Code: 200
X-Server-Response-Time: 14ms
Content-RequestDuration: 21

{JSON}
--Boundary_16003419_2104021487_1483424496169
Content-Type: application/json
Content-RequestId: ListFolderThreads
Status-Code: 200
X-Server-Response-Time: 110ms
Content-RequestDuration: 116

{JSON}
--Boundary_16003419_2104021487_1483424496169
Content-Type: application/json
Content-RequestId: GetUserInfo
Status-Code: 200
X-Server-Response-Time: 197ms
Content-RequestDuration: 204

{JSON}
--Boundary_16003419_2104021487_1483424496169
Content-RequestId: Status
Content-Type: application/json

{JSON}
--Boundary_16003419_2104021487_1483424496169--

任何可靠的方法吗?

1 个答案:

答案 0 :(得分:2)

一个选项是Apache Mime4j。如果您希望应用程序处理响应内容而不在内存中构建完整的DOM树,则可能希望使用MimeTokenStreamMimeStreamParser,如here所述。

相关问题