HttpURLConnection
URL url = new URL(targetUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36"); connection.setRequestProperty("accept-language","zh-CN,zh;q=0.9"); connection.setRequestProperty("sec-ch-ua-platform","Windows"); connection.setRequestProperty("sec-ch-ua-platform-version","10.0.0"); connection.setRequestProperty("sec-ch-ua","'Google Chrome';v='95', 'Chromium';v='95', ';Not A Brand';v='99'"); connection.setRequestProperty("cookie",""); // 默认就是Get,可以采用post,大小写都行,因为源码里都toUpperCase了。 connection.setRequestMethod("GET"); // 是否允许缓存,默认true。 connection.setUseCaches(Boolean.FALSE); // 是否开启输出输入,如果是post使用true。默认是false // connection.setDoOutput(Boolean.TRUE); // connection.setDoInput(Boolean.TRUE); // 设置请求头信息 connection.addRequestProperty("Connection", "close"); // 设置连接主机超时(单位:毫秒) connection.setConnectTimeout(8000); // 设置从主机读取数据超时(单位:毫秒) connection.setReadTimeout(8000); // 设置Cookie // connection.addRequestProperty("Cookie", "你的Cookies"); // 开始请求 int index = targetUrl.indexOf("://") + 3; String baseUri = targetUrl.substring(0, index) + url.getHost(); document2 = Jsoup.parse(connection.getInputStream(), "GBK", baseUri);
#####################