HttpURL
2016-03-04 21:10
585 查看
* 步骤:
1. new一个URL对象
2. new一个HttpURLConnection对象
3. connection连接
4. getResponseCode()
5. 读取流
1. new一个URL对象
2. new一个HttpURLConnection对象
3. connection连接
4. getResponseCode()
5. 读取流
// 将网址封装为URL对象 URL url = new URL(path); // 用url里面的openConnection方法打开连接。 HttpURLConnection connection = (HttpURLConnection) url .openConnection(); // 设置HttpURLConnection一些属性。 // 1:设置请求方式。--默认为 get connection.setRequestMethod("GET"); // 2:设置连接时间。--可写不可写 connection.setConnectTimeout(5000); // 3:设置可读。---可写可不写。默认为true connection.setDoInput(true); // 4:设置可以往服务器端写入数据------可写可不写。默认为false connection.setDoOutput(true); // 连接 connection.connect(); // 服务器端接收到客户端的请求,并返回响应码。 int code = connection.getResponseCode(); if (code == 200) { // 读取服务器端发送过来的数据。 InputStream is = connection.getInputStream(); FileOutputStream fos = new FileOutputStream("f:\\logo2.jpeg"); byte[] b = new byte[1024]; int length = -1; while ((length = is.read(b)) != -1) { fos.write(b, 0, length); } is.close(); fos.close(); }
相关文章推荐
- HttpClient
- IOS 开发 之 网络编程
- poj3469 Dual Core CPU 网络流 sap邻接表模板
- HttpClient4.5教程-序言
- ATS http — >https
- http://blog.sina.com.cn/s/blog_5b9b4abe01017638.html
- https通讯原理、加密算法、证书整理总结
- HTTPS科普扫盲帖
- 使用PC的adb工具通过wifi(TCP方式)调试安卓设备
- 找工作-——网络IO
- 获取网络gif时候报错 <Error>: ImageIO: _CGImagePluginInitGIF malformed GIF frame#0
- HTTP Live Streaming直播(iOS直播)技术分析与实现
- HTTP协议的学习
- OC-之AFNetworking与ASIHTTPRequest对比
- IHttpModule与IHttpHandler的区别整理
- Android操作HTTP实现与服务器通信
- 记录--java获取网络资源(图片、音频等)保存本地
- Python---TCP编程
- win7下面VM VirtualBox中linux CenOS 网络的配置
- 小笨狼漫谈多线程:GCD(一) http://www.cocoachina.com/ios/20160225/15422.html