关于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
- 关于http chunk
- http chunk
- http chunk
- Http协议中关于Content-Length和Chunk
- http chunk编码 C++
- http协议chunk 编码
- Http chunk介绍
- http协议之chunk编码
- http协议之chunk编码
- http协议之chunk编码
- http chunk 动态长度传输
- HTTP协议之chunk编码
- 我们为什么需要Http Chunk
- Http chunk方式传输数据
- HTTP协议之chunk介绍
- HTTP协议中的chunk编码
- chunk
- zlib对http中chunk数据解压
- IE6不支持小于12px的盒子问题
- HDUoj 1905 Pseudoprime numbers (快速幂)
- Android Studio发布项目到Maven和Jcenter
- 项目管理工具-Maven
- 第四章
- 关于http chunk
- 下拉刷新组件 PullToRefreshListView的使用
- 《Java编程思想》阅读笔记之第一章对象导论
- 第五章
- 京东首页之页面主体、配送方式
- 线程SuspendThread() ResumeThread()的使用
- 变量和数据类型
- 树链剖分
- Ubuntu14.10中为Android Studio设置桌面快捷方式