JSOUP 開啟url的方式
阿新 • • 發佈:2018-12-29
一般採用這種方式:
try{ doc = Jsoup.connect(url) .header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:49.0) Gecko/20100101 Firefox/49.0") .header("Connection", "close")//如果是這種方式,這裡務必帶上 .timeout(8000)//超時時間 .get(); }catch (Exception e) {//可以精確處理timeoutException //超時等異常處理 }
而我更建議用 URL
去開啟
//建立請求 URL url = new URL("https://sms.reyo.cn"); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); //預設就是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" ); //開始請求 Document doc = Jsoup.parse(connection.getInputStream(), "GBK", "https://sms.reyo.cn"); //開啟您的瘋狂選擇器模式 doc.select("div.so >div ~ p:eq(10)>:checked"); //TODO ---