Regex Rookie
Regex Rookie

Reputation: 10662

Is there a function that strips HTTP response headers?

HttpResponse.getEntity().getContent() returns everything... including response headers, javascript code, response body (of course!) etc.

Is there a function that cleans this up and provides only the response body?

Upvotes: 0

Views: 1509

Answers (2)

biddulph.r
biddulph.r

Reputation: 5266

You can filter out patterns using Android with your own method. Pass in the String, and apply pattern filters to remove things you don't want.

public String filter(String searchString)
    {
        String content = searchString;
        // Remove the new line characters.
        Pattern newLineChar = Pattern.compile("\n+");
        Matcher mLine = newLineChar.matcher(content);
        while (mLine.find())
            content = mLine.replaceAll("");
        // Return the clean content
        return content;
    }

You can get quite complicated with your patterns, and filter pretty much any expression you want. (You may need to use regular expressions etc). The example above replaces a new line (\n) with 0 length string, to remove all of them from the string. You can build up patterns, or you can just iterate through again to remove something else.

You will also need a couple of imports for this to work:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

Upvotes: 1

vbence
vbence

Reputation: 20333

You have to read the data from the InputStream to a buffer. Search for this regex:

\r\n\r\n(.*)

This will give you the things after the header.

Or you can replace it with an empty string, if you are searching for:

^.*?\r\n\r\n

Upvotes: 1

Related Questions