Gzip方式數據請求以及解碼

Android App中進行網絡請求,我們都司空見慣。那麼進行http請求時可以進行壓縮請求,你造麼。。。

簡介

HTTP定義了一些標準的內容編碼類型,並允許用擴展的形式添加更多的編碼。
  Content-Encoding header 就用這些標準化的代號來說明編碼時使用的算法
  Content-Encoding值
  gzip  表明實體採用GNU zip編碼
  compress 表明實體採用Unix的文件壓縮程序
  deflate  表明實體是用zlib的格式壓縮的
  identity  表明沒有對實體進行編碼。當沒有Content-Encoding header時, 就默認爲這種情況
  gzip, compress, 以及deflate編碼都是無損壓縮算法,用於減少傳輸報文的大小,不會導致信息損失。 其中gzip通常效率最高, 使用最爲廣泛。
  
好處

壓縮的好處
  http壓縮對純文本可以壓縮至原內容的40%, 從而節省了60%的數據傳輸。

使用方式

  1. 添加頭信息
HttpPost httpPost = new HttpPost(MainURL);

        httpPost.addHeader("Accept-Encoding", "gzip");
  1. 解碼
Header ceheader = entity.getContentEncoding();
                    if (ceheader != null) {
                        for (HeaderElement element : ceheader.getElements()) {
                            if (element.getName().equalsIgnoreCase("gzip")) {
                                httpResponse.setEntity(new GZipDecompressingEntity(httpResponse.getEntity()));
                            }
                        }
                    }

算了,把我的網絡請求方式完整的放出來吧

public static String PostInfo(Context context,List<NameValuePair> subinfo) {
        String responseInfo = "";
        HttpPost httpPost = new HttpPost(MainURL);
        //採用Gzip格式請求
        httpPost.addHeader("Accept-Encoding", "gzip");

        HttpResponse httpResponse = null;
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(subinfo, HTTP.UTF_8));
            HttpClient client = new DefaultHttpClient();
            if(context!=null){
                //加入cookie
                PersistentCookieStore  cookieStore= new PersistentCookieStore(context);  
                ((AbstractHttpClient) client).setCookieStore(cookieStore);  
//              httpPost.setHeader("Cookie",cookieStore.toString());
            }
            String useragent=(String) client.getParams().getParameter(HttpProtocolParams.USER_AGENT);
            client.getParams().setParameter(HttpProtocolParams.USER_AGENT,"chuanyue_app"+useragent);

            httpResponse = client.execute(httpPost);

            CommonTools.makeLog("useragent", (String) client.getParams().getParameter(HttpProtocolParams.USER_AGENT));
            // 連接時間20秒超時
            client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20000);
            // 傳輸時間30S超時
            client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 30000);
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                if(context!=null){
                    //cookie問題
                    @SuppressWarnings("unused")
                    HttpEntity entity = httpResponse.getEntity();  
                    List<Cookie> cookies = ((AbstractHttpClient) client).getCookieStore().getCookies();
                    PersistentCookieStore myCookieStore = new PersistentCookieStore(context);
                    for (Cookie cookie:cookies){  
                        myCookieStore.addCookie(cookie);
                    }
                    //Gzip解析
                    Header ceheader = entity.getContentEncoding();
                    if (ceheader != null) {
                        for (HeaderElement element : ceheader.getElements()) {
                            if (element.getName().equalsIgnoreCase("gzip")) {
                                httpResponse.setEntity(new GZipDecompressingEntity(httpResponse.getEntity()));
                            }
                        }
                    }

                }
                CommonTools.makeLog("response", responseInfo);
                responseInfo = EntityUtils.toString(httpResponse.getEntity());
                return responseInfo;

            }
        } catch (Exception e) {
            CommonTools.makeLog("post", "PostInfo_Erro");
            return "";
        }
        return "";
    }
發佈了29 篇原創文章 · 獲贊 9 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章