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()); } }
知识只有共享才能传播,才能推崇出新的知识,才能学到更多,这里写的每一篇文字/博客,基本都是从网上查询了一下资料然后记录下来,也有些是原滋原味搬了过来,也有时加了一些自己的想法