Parse HTTP response bytes in java

I am trying to parse byte[] in java, which is an HTTP response. The question is, is there any simple parser for responding to HTTP for Java? , and this is exactly my question, but the accepted answer does not help me. If I look at http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/io/HttpMessageParser.html , I don’t understand how this will help me.

+6
source share
2 answers

Hope this helps you.

 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); 

This code produces the following output:

 HTTP/1.1 200 OK [Content-Length: 100, Content-Type: text/plain, Server: some-server] 
+8
source

Check this out: https://github.com/ipinyol/proxy-base

This is a simple custom HTTP proxy. The readHeader method of the org.mars.proxybase.ProxyThread class parses the http headers, DataInputStream data (which is read in bytes) and returns an object of type Header with header information.

Also, you probably know that either you have the length of the content length in the header, or you have fragmented data that you should read in pieces in the http response. The readContent and readContentByChunk methods of the same class read content. You can study the code yourself and make the appropriate changes.

0
source

Source: https://habr.com/ru/post/977184/


All Articles