简体   繁体   English

是否有一个剥离HTTP响应头的函数?

[英]Is there a function that strips HTTP response headers?

HttpResponse.getEntity().getContent() returns everything ... including response headers, javascript code, response body (of course!) etc. HttpResponse.getEntity()。getContent()返回所有内容 ......包括响应头,javascript代码,响应主体(当然!)等。

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. 您必须将InputStream中的数据读取到缓冲区。 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. 您可以使用自己的方法使用Android过滤出模式。 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. 上面的示例用0长度字符串替换新行(\\ n),以从字符串中删除所有这些行。 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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