我正在尝试解析java中的byte[]
,它是HTTP响应的表示。有一个问题Is there any simple http response parser for Java?,这正是我的问题,但是被接受的答案对我没有帮助。如果我看http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/io/HttpMessageParser.html,我不知道这对我有什么帮助。
最佳答案
我希望这可以帮助您入门
String s = "HTTP/1.1 200 OK\r\n" +
"Content-Length: 100\r\n" +
"Content-Type: text/plain\r\n" +
"Server: some-server\r\n" +
"\r\n";
SessionInputBufferImpl sessionInputBuffer = new SessionInputBufferImpl(new HttpTransportMetricsImpl(), 2048);
sessionInputBuffer.bind(new ByteArrayInputStream(s.getBytes(Consts.ASCII)));
DefaultHttpResponseParser responseParser = new DefaultHttpResponseParser(sessionInputBuffer);
HttpResponse response = responseParser.parse();
System.out.println(response);
此代码产生以下输出:
HTTP/1.1 200 OK [Content-Length: 100, Content-Type: text/plain, Server: some-server]