HttpURL
* 步骤:
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();
}
HttpURL的更多相关文章
- 【读书笔记】iOS网络-HTTP-URL百分号编码
代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, ty ...
- 【读书笔记】iOS网络-HTTP-URL结构
http://user:password@hostname:port/absolute-path?query. http: 协议 user:password@ 认证 hostname: 主机名 ...
- 【读书笔记】iOS-网络-HTTP-URL百分号编码
代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, ty ...
- 【读书笔记】iOS-网络-HTTP-URL结构
http://user:password@hostname:port/absolute-path?query. http: 协议 user:password@ 认证 hostname: 主机名 ...
- java模拟网页http-url访问
package com.iflytek; import java.io.InputStream; import java.net.HttpURLConnection; import java.net. ...
- HttpURL连接远程serverGet和Post方式请求并返回数据
查看原文:http://www.ibloger.net/article/1813.html package cn.gis; import java.io.BufferedReader; import ...
- AFNetworking 3.0 源码解读 总结(干货)(下)
承接上一篇AFNetworking 3.0 源码解读 总结(干货)(上) 21.网络服务类型NSURLRequestNetworkServiceType 示例代码: typedef NS_ENUM(N ...
- AFNetworking 3.0 源码解读(六)之 AFHTTPSessionManager
AFHTTPSessionManager相对来说比较好理解,代码也比较短.但却是我们平时可能使用最多的类. AFNetworking 3.0 源码解读(一)之 AFNetworkReachabilit ...
- Android Retrofit 2.0 使用-补充篇
推荐阅读,猛戳: 1.Android MVP 实例 2.Android Retrofit 2.0使用 3.RxJava 4.RxBus 5.Android MVP+Retrofit+RxJava实践小 ...
随机推荐
- apache配置多个虚拟主机
设置apache 多个虚拟目录记录 #配置第2个虚拟目录<VirtualHost 127.0.0.2:80>ServerName www.xx.comDocumentRoot " ...
- 10 steps to becoming the developer everyone wants
You thought it was all about programming skills. But you were wrong! Great code is fine, yet command ...
- 如何调试R程序(转载)
R语言的调试重要性不言而喻,这段时间准备改进一个R的包,但由于接触R时间不长,中间的很多东西不懂,需要重新打包调试,以对里面的很多程序有深入了解,下面从几个方面分享一下我的收获. 1.准备工作 a)R ...
- Run UliPad 4.1 Under Windows 7 64bit and wxPython 3.0.2
Abstract: UliPad that is developed by limodou is an excellent code editor. It works well with wxPyth ...
- gcc编译C++程序
gcc动态编译和静态编译方法 一.单个源.cpp文件生成可执行程序下面是一个保存在文件 helloworld.cpp 中一个简单的 C++ 程序的代码: /* helloworld.cpp */ #i ...
- 一个很不错的适合PHPER们书单,推荐给大家【转】
来我博客的访客们中,有一些是PHP的初学者,是不是很迷茫PHP应该怎么学?应该买什么样的书?到处问人,到处求助?这下好了. 正好看到黑夜路人在博客上推荐了一个书单,看上去都非常不错,很多我也没有读过, ...
- 【leetcode】Subsets II
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- POJ 1017
http://poj.org/problem?id=1017 题意就是有6种规格的物品,给你一些不同规格的物品,要求你装在盒子里,盒子是固定尺寸的也就是6*6,而物品有1*1,2*2,3*3,4*4, ...
- linux ldconfig
http://blog.csdn.net/dante_k7/article/details/7211868 ldconfig的主要用途: 默认搜寻/lilb和/usr/lib,以及配置文件/etc/l ...
- 1.1 让CPU占用率曲线听你指挥[cpu manager]
[本文链接] http://www.cnblogs.com/hellogiser/p/cpu-manager.html [题目] 写一个程序,让用户来决定Windows任务管理器(Task Manag ...