---
title: "WARC里的HTTP响应"
author: "cloverprince"
date: "2011-02-16T17:20:54Z"
tags: ["HTTP", "WARC", "解析"]
categories: ["编程语言"]
slug: "warc-http"
draft: false
lastmod: "2011-07-06T16:01:53Z"
id: 908073
url: "https://cloverprince.iteye.com/blog/908073"
---

WARC是一种格式。Heritrix（\[url]http://crawler.archive.org/\[/url]）用它原封不动地储存HTTP请求和HTTP响应的全文。包括请求/状态行、头、内容。 读WARC格式很简单。利用Heritrix中提供的工具即可。 \[code="java"] package warc; import java.io.\*; import org.apache.commons.io.IOUtils; import org.archive.io.\*; import org.archive.io.warc.\*; public class MyWarcReading { public static void main(String\[] args) throws Exception { ArchiveReader ar = WARCReaderFactory.get("/path/to/your/file.warc.gz"); // ArchiveReader对象本身实现了Iterator接口，可以用for循环。 for (ArchiveRecord rec : ar) { // 获取WARC记录头。 ArchiveRecordHeader header = rec.getHeader(); // 可以对header做一些操作。 // 读出这条记录的内容。ArchiveRecord继承了InputStream类。 // 我使用了Commons-IO。不过这样读太暴力了…… byte\[] content = IOUtils.toByteArray(rec); // 对内容做什么都行。 } ar.close(); } } \[/code] 如果里面储存了爬虫爬的结果，那么整个HTTP响应会成为一条记录的内容。这种记录是原封不动的响应，包括status line, headers, entity，都有。mimetype是application/http，如HTTP/1.1的RFC中所述。 \[code="java"] ArchiveRecord rec; // 一条记录 if(rec.getHeader().getMimetype().equals( "application/http; msgtype=response")) { // 这是HTTP响应 } \[/code] 如何解析这个响应呢？ 想过用Apache Httpcomponents(\[url]http://hc.apache.org/\[/url])的HTTP Core，但是它的解析器与Socket耦合太强，还使用了很多自己的数据结构以提高效率。Jetty的解析器没有文档（undocumented）。 最后，发现如果只是想读HTTP头，还是用BufferedReader自己读比较好。 \[code="java"] private static void parseHtml(ArchiveRecord rec) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(rec,"ISO-8859-1")); String statusLine = br.readLine(); System.out.println("Status:"+statusLine); System.out.println("===HTTP headers==="); while(true) { String line = br.readLine(); if(line==null || line.isEmpty()) { break; } System.out.println(line); } byte\[] bytes = IOUtils.toByteArray(rec); // WRONG! System.out.println("===HTTP entity==="); System.out.println(new String(bytes, "UTF-8")); } \[/code] 但是，由于BufferedReader的缓冲，此时读出的entity是不正确的。