java抓取數據+破解屏蔽ip訪問 html
今天就講解一下,怎麼破解 服務器 屏蔽ip的請求。 java
如今大多網站採起 ip訪問次數達到必定次數就屏蔽ip的功能。 apache
那麼要破解服務器的屏蔽。 就只有改變ip, 或者代理ip。 服務器
若是用代理,哪裏去找那麼多ip呢。 用adsl 獲取動態ip不是很簡單嗎。 多線程
轉載註明出處:http://blog.csdn.net/column/details/threadgrab.html app
那麼如今就貼上adsl獲取動態ip的方案實例源碼 工具
一、抓取網頁數據的時候 catch異常以後 , 就採起撥號程序 網站
- //門票瀏覽 url參數 http://www.lvmama.com/dest/lantiancheng
- public static DataBean getWebData1(String url){
- DataBean data = null;
- try {
- Document docdata = Jsoup.connect(url).timeout(20000).get();
-
- } catch (Exception e) {
- e.printStackTrace();
- //撥號一下
- ConnectAdslNet.reconnectAdsl("寬帶",Main.adslname,Main.adslpass);
- }
- return data;
- }
二、撥號獲取動態ip的 工具類 ui
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
-
- import org.apache.log4j.Logger;
-
- /**
- *
- * ADSL撥號上網
- * Windwos操做系統須要是GBK編碼
- * @author yijianfeng
- *
- */
-
- public class ConnectAdslNet {
- static Logger logger = Logger.getLogger(ConnectAdslNet.class);
-
- /**
- * 執行CMD命令,並返回String字符串
- */
- public static String executeCmd(String strCmd) throws Exception {
- Process p = Runtime.getRuntime().exec("cmd /c " + strCmd);
- StringBuilder sbCmd = new StringBuilder();
-
- //注意編碼 GBK
- BufferedReader br = new BufferedReader(new InputStreamReader(p
- .getInputStream(),"GBK"));
- String line;
- while ((line = br.readLine()) != null) {
- sbCmd.append(line + "\n");
- }
- return sbCmd.toString();
- }
-
- /**
- * 鏈接ADSL
- */
- public static boolean connectAdsl(String adslTitle, String adslName, String adslPass) throws Exception {
- System.out.println("正在創建鏈接.");
- String adslCmd = "rasdial " + adslTitle + " " + adslName + " "
- + adslPass;
- String tempCmd = executeCmd(adslCmd);
-
- // 判斷是否鏈接成功
- if (tempCmd.indexOf("已鏈接") > 0) {
- System.out.println("已成功創建鏈接.");
- return true;
- } else {
- System.out.println(tempCmd);
- System.out.println("創建鏈接失敗");
- return false;
- }
- }
-
- /**
- * 斷開ADSL
- */
- public static boolean disconnectAdsl(String adslTitle) throws Exception {
- String disconnectAdsl = "rasdial " + adslTitle + " /disconnect";
- String result = executeCmd(disconnectAdsl);
-
- if (result.indexOf("沒有鏈接")!=-1){
- System.out.println(adslTitle + "鏈接不存在!");
- return false;
- } else {
- System.out.println("鏈接已斷開");
- return true;
- }
- }
-
- /**
- * adsl從新撥號,支持失敗不斷重撥
- * @param args
- * @throws Exception
- */
- public static boolean reconnectAdsl(String adslTitle, String adslName, String adslPass){
- boolean bAdsl = false;
- try {
- disconnectAdsl(adslTitle);
- Thread.sleep(3000);
- bAdsl = connectAdsl(adslTitle,adslName,adslPass);
- Thread.sleep(3000);
- int i = 0;
- while (!bAdsl){
- disconnectAdsl(adslTitle);
- Thread.sleep(3000);
- bAdsl = connectAdsl(adslTitle,adslName,adslPass);
- Thread.sleep(3000);
- if(i>5){
- break;
- }
- }
- }catch(Exception e){
- logger.error("ADSL撥號異常:", e);
- }
-
- return bAdsl;
- }
-
- public static void main(String[] args) throws Exception {
- // reconnectAdsl("寬帶","adsl帳號","密碼");
- }
-
- }
採用上述辦法,基本上就能夠解決撥號的問題了。 編碼
若是程序加入了多線程。 那麼就必須考慮多線程,撥號同步,以及數據同步問題。 提升效率和避免重複操做。
到此,破解屏蔽ip訪問就搞定了!