通过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()); } }
时间: 2024-10-10 12:57:20