11

Is there any simple http response parser implementation? The idea is to put in the complete response as one big string and be able to retrieve stuff like statuscode, body etc. through the interface.

The requests/responses are not sent directly over TCP/IP so there is no need for anything but the core rfc 2616 parsing implementation.

2 Answers 2

12

If you use for instance Apache HttpClient you will get a java response object which you can use to extract headers or the message body. Consider the following sample

HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(new HttpGet("http://www.foo.com/"));
Header[] headers = response.getAllHeaders();
InputStream responseBody = response.getEntity().getContent();

If you only want to parse a reponse, perhaps the HttpMessageParser will be useful:

Abstract message parser intended to build HTTP messages from an arbitrary data source.

Sign up to request clarification or add additional context in comments.

4 Comments

IMHO the OP wants to parse the raw HTTP response that he has in a String (maybe from some dump).
Thomasz is right. I'm working with raw responses... there are no URIs and HttpGet won't work.
@mibollma, see the second suggestion using HttpMessageParser
That looks much better. Thanks alot.
0

I recomend http-request built on apache http api.

HttpRequest<String> httpRequest = HttpRequestBuilder.createGet(someUri, String.class)
    .responseDeserializer(ResponseDeserializer.ignorableDeserializer())
    .build();

public void send(){
   ResponseHandler<String> responseHandler = httpRequest.execute();
   String responseBody = responseHandler.get();
   int statusCode = responseHandler.getStatusCode();
}

I higly recomend read documentation before use.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.