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访问就搞定了!