java-URLConnection网络数据收取

通过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()); 
    }

}

 

posted @ 2015-10-13 09:32  243573295  阅读(352)  评论(0编辑  收藏  举报