获取网络下载请求工具类
2015-09-22 17:08
288 查看
网络下载数据
<span style="font-family:Comic Sans MS;font-size:18px;">package com.example.week2_day3_downloadjson.utils; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; //获取网络资源 public class Utils { public static String getJson(String path){ ByteArrayOutputStream out=new ByteArrayOutputStream(); URL url; String json="";//接收json数据 try { url=new URL(path); HttpURLConnection connection=(HttpURLConnection) url.openConnection();//打开网络连接 connection.setReadTimeout(5000);//设置连接请求时间 connection.setDoInput(true);//设置连接 connection.connect();//连接 if(connection.getResponseCode()==200){ InputStream input = connection.getInputStream(); byte[] buffer=new byte[1024]; int temp=0; while ((temp=input.read(buffer))!=-1) { out.write(buffer, 0, temp); out.flush(); } } return out.toString(); } catch (Exception e) { e.printStackTrace(); } return json; } } </span>
相关文章推荐
- 关于HttpServletResponse在前端的缓存及细节
- https证书申请
- HTTP Status 500 - javax.servlet.ServletException: File [/head.jsp] not found
- PHP 使用TCPDF插件生成pdf以及pdf的中文处理
- Shell特殊变量:Shell $0, $#, $*, $@, $?, $$和命令行参数 http://c.biancheng.net/cpp/view/2739.html
- 从jquery里的$.ajax()到angularjs的$http
- Java EE 6.0 的 Cookie 类已经有设置 HttpOnly 的方法
- http://smilejay.com/2011/12/eclipse_tab_size/
- ASIHTTPRequest "A connection failure occurred" error
- 一些网络设备的学习笔记
- 一只简单的网络爬虫(基于linux C/C++)————支持动态模块加载
- 网络安全书籍推荐
- HTTP Status 500 - /online.jsp (line: 495, column: 2) File [/foot.jsp] not found
- linux内核之网络协议栈
- 新东西007--Http Cookie分析
- socket,tcp IP
- HttpClientPostActivity
- HttpClientGetActivity
- commons-HttpClient相关笔记
- 关于使用Python3进行网络爬虫的字符问题