码迷,mamicode.com
首页 > 编程语言 > 详细

java-URLConnection网络数据收取

时间:2015-10-13 10:39:15      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:

通过url创建connection方式收取

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

@SuppressWarnings("all")
public class Test {
    public static void main(String[] args)throws Exception {
        URL url = new  URL("http://192.168.0.215:8080/tsportal/web/system/userOnlineCount");
        URLConnection connection =  url.openConnection();
        connection.setConnectTimeout(20000);
        connection.setReadTimeout(20000);
        connection.setUseCaches(false);
        connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11");
        InputStream is = connection.getInputStream(); 
        InputStreamReader inputStreamReader = new InputStreamReader(is, "utf-8");
        BufferedReader bis = new BufferedReader(inputStreamReader); 
        String line = null; 
        StringBuffer result = new StringBuffer(); 
        while ((line = bis.readLine()) != null) { 
            result.append(line); 
        } 
        System.out.println(result.toString()); 
    }

}

 

java-URLConnection网络数据收取

标签:

原文地址:http://www.cnblogs.com/hwaggLee/p/4873567.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!