一段代码演示http客户端缓存的几个参数

以前的代码片段:

/**
 *
 * 
 * 实现获取福彩双色球的开奖号码
 */
public class ParserSSQ extends ParserSportGameHTML{
    int MAX_GETDATASIZE = 5 * 1024;

    public String parserLotteryHtml() throws Exception {
        String ssqurl = EnvConfig.getInstance().getGAMESSQURL();

        String GAMESSQ_HTTP_LAST_MODIFIED = LastSuccConfig.getInstance().GAMESSQ_HTTP_LAST_MODIFIED;
        String GAMESSQ_HTTP_ETAG = LastSuccConfig.getInstance().GAMESSQ_HTTP_ETAG;

        if (EnvConfig.getInstance().isGAMESSQ_USE_HTTPHEAD()){
            GAMESSQ_HTTP_LAST_MODIFIED = "";
            GAMESSQ_HTTP_ETAG = "";
        }

        String result = "";
        boolean isModify = true;
        StringBuffer buffer = new StringBuffer();
        String HTTP_head_first = "";

        issuename_  = ssqurl;
        awardurl_ = ssqurl;

        // System.out.println("");
        //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
        URL url = new URL(ssqurl);
        HttpURLConnection huc = (HttpURLConnection) url.openConnection();
        huc.setRequestMethod("GET");
        huc.setDoOutput(true);
        huc.setDoInput(true);
        huc.setAllowUserInteraction(false);
        if (EnvConfig.getInstance().isGAMESSQ_USE_HTTPHEAD()){
            System.out.println("使用HTTP_LAST_MODIFIED及HTTP_ETAG");
            huc.setRequestProperty("If-Modified-Since",GAMESSQ_HTTP_LAST_MODIFIED);
            huc.setRequestProperty("If-None-Match", GAMESSQ_HTTP_ETAG);
        }
        huc.connect();

        //获取回来的流
        InputStream in = huc.getInputStream();

        int bytes = 0; int i = 0;
        while ( (bytes = in.read()) != -1) {
            buffer.append( (char) bytes);
            i = i + 1;
            if ( i > MAX_GETDATASIZE ) break;
        }
        in.close();

        GAMESSQ_HTTP_LAST_MODIFIED = huc.getHeaderField("Last-Modified");
        GAMESSQ_HTTP_ETAG = huc.getHeaderField("ETag");
        HTTP_head_first = huc.getHeaderField(0);
        huc.disconnect();

        if (GAMESSQ_HTTP_LAST_MODIFIED == null) GAMESSQ_HTTP_LAST_MODIFIED = "";
        if (GAMESSQ_HTTP_ETAG == null) GAMESSQ_HTTP_ETAG = "";

        if (EnvConfig.getInstance().getISDEBUG() == 1) {
            System.out.println("首行:"+HTTP_head_first);
            System.out.println("Last-Modified:"+GAMESSQ_HTTP_LAST_MODIFIED);
            System.out.println("ETag:"+GAMESSQ_HTTP_ETAG);
            System.out.println("长度="+buffer.length());
            //System.out.println("内容="+buffer.substring(0,buffer.length()));
        }

        String notModified = "Not Modified";
        if (HTTP_head_first.indexOf(notModified) >= 0){
            isModify = false;
        }
        //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

        if (isModify) {
            if (EnvConfig.getInstance().isGAMESSQ_USE_HTTPHEAD()){
                LastSuccConfig.getInstance().GAMESSQ_HTTP_ETAG = GAMESSQ_HTTP_ETAG;
                LastSuccConfig.getInstance().GAMESSQ_HTTP_LAST_MODIFIED = GAMESSQ_HTTP_LAST_MODIFIED;
                LastSuccConfig.getInstance().saveNewValueToFile();
            }
            result = GetResult(buffer);
        }

        ShowLogs(result);
        return result ;
    }



应该主要到,上面的代码稍加修改,就可以应用于http客户端的缓存(先利用Head获取内容是否修改,然后再用Get获取内容)

你可能感兴趣的:(etag,Last-Modified,if-none-match)