简体   繁体   中英

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 ?

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM