关于http chunk

实际上,不需要造这个轮子。
chunk编码介绍如下:
[http://www.cnblogs.com/ribavnu/p/5084458.html]

httpclient已经封装了这个算法了,只需要:

            CloseableHttpResponse httpResponse = httpclient.execute(httpget);
                HttpEntity entity = httpResponse.getEntity();

返回String:

return EntityUtils.toString(entity);

简写如下:

public String httpGetHTML(String url) {
        CloseableHttpClient httpclient = HttpClient();
        try {
            HttpGet httpget = new HttpGet(url);
            CloseableHttpResponse httpResponse = httpclient.execute(httpget);
            try {
                HttpEntity entity = httpResponse.getEntity();
                if (entity != null) {
                    logger.debug("Response From: " + url);
                    return EntityUtils.toString(entity);
                }
            } finally {
                httpResponse.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

精细化,用StringBuffer:

private StringBuffer getBufferData(InputStream inputstream, String charset) throws IOException {
    BufferedReader bufferedreader;
    try {
        bufferedreader = new BufferedReader(new InputStreamReader(inputstream, charset), 2048);
        StringBuffer stringbuffer = new StringBuffer();
        String line = null;
        while ((line = bufferedreader.readLine()) != null) {
            stringbuffer.append(line);
        }
        return stringbuffer;
    } catch (UnsupportedEncodingException e) {
        logger.error("Wrong CharSet: " + charset);
        e.printStackTrace();
    }
    return null;
}

public StringBuffer httpGetBuffer(String url, Map<String, String> headers) {
    CloseableHttpClient httpclient = HttpClient();
    HttpGet httpGet = new HttpGet(url);
    InputStream inputstream = null;
    String charset = null;
    String encoding="";
    Header Encoding, ContentType;
    int inputstreamsize = 0;
    byte[] chunkbyte = new byte[130];
    StringBuffer chunkbuffer = new StringBuffer();
    try {
        if (headers != null) {
            for (String name : headers.keySet())
                httpGet.setHeader(name, headers.get(name));
            HttpGet httpget = new HttpGet(url);
            CloseableHttpResponse httpResponse = httpclient.execute(httpget);
            try {
                HttpEntity httpentity = httpResponse.getEntity();
                inputstream = httpentity.getContent();
                int status = httpResponse.getStatusLine().getStatusCode();
                Encoding = httpResponse.getFirstHeader("Transfer-Encoding");
                ContentType = httpResponse.getFirstHeader("Content-Type");
                if(Encoding!=null){
                    encoding=Encoding.getValue();
                }
                if (ContentType.getValue().indexOf("charset") > 0) {
                    charset = ContentType.getValue().split("=")[1].trim();
                    logger.debug("Use the charset to parse response: " + charset);
                }
                if (httpentity != null && status == 200) {
                    if (encoding.equalsIgnoreCase("chunked")) {
                        while ((inputstreamsize = inputstream.read(chunkbyte, 0, chunkbyte.length)) != -1) {
                            chunkbuffer.append(new String(chunkbyte, 0, inputstreamsize, charset));
                        }
                        logger.debug("chunk length: " + chunkbuffer.length());
                        return chunkbuffer;
                    } else {
                        return getBufferData(inputstream, charset);
                    }
                } else if (status != 200) {
                    logger.error("HttpResponse Error: " + status);
                }
            } finally {
                httpResponse.close();
            }
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            httpclient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值