1、通过JDK网络类Java.net.URLConnection
(1)api
(2)使用:
GET:
1、创建远程连接
URL url = new URL(httpUrl);
connection = (HttpURLConnection) url.openConnection();
2、设置连接方式(get、post、put。。。)和通用属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("Content-Type","application/json;charset=utf-8");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.setRequestMethod("GET");
3、设置连接超时时间
connection.setReadTimeout(15000);
4、设置响应读取时间
5、发起请求
connection.connect()
6、获取请求数据
if (connection.getResponseCode() == 200) {
is = connection.getInputStream()