关于http chunk

来源:互联网 发布:优酷网络连接失败 编辑:程序博客网 时间:2024/06/05 23:23

实际上,不需要造这个轮子。
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
原创粉丝点击