自动更改IP地址反爬虫封锁

转载地址 https://github.com/ysc/superword/blob/master/src/main/java/org/apdplat/superword/tools/DynamicIp.java


/**
 *
 * APDPlat - Application Product Development Platform Copyright (c) 2013, 杨尚川,
 * yang-shangchuan@qq.com
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */
  
 package org.apdplat.superword.tools;
  
 import org.jsoup.Connection;
 import org.jsoup.Jsoup;
 import org.jsoup.nodes.Document;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import java.util.*;
  
 /**
 *
 * 自动更改IP地址反爬虫封锁
 *
 * ADSL拨号上网使用动态IP地址,每一次拨号得到的IP都不一样
 *
 * @author 杨尚川
 */
 public class DynamicIp {
 private DynamicIp(){}
 private static final Logger LOGGER = LoggerFactory.getLogger(DynamicIp.class);
 private static final String ACCEPT = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
 private static final String ENCODING = "gzip, deflate";
 private static final String LANGUAGE = "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3";
 private static final String CONNECTION = "keep-alive";
 private static final String HOST = "192.168.0.1";
 private static final String REFERER = "http://192.168.0.1/login.asp";
 private static final String USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:36.0) Gecko/20100101 Firefox/36.0";
  
 public static void main(String[] args) {
 toNewIp();
 }
 public static boolean toNewIp() {
 Map<String, String> cookies = login("username***", "password***", "phonenumber***");
 if("true".equals(cookies.get("success"))) {
 LOGGER.info("登陆成功");
 cookies.remove("success");
 while (!disConnect(cookies)) {
 LOGGER.info("断开连接失败,重试!");
 }
 LOGGER.info("断开连接成功");
 while (!connect(cookies)) {
 LOGGER.info("建立连接失败,重试!");
 }
 LOGGER.info("建立连接成功");
 LOGGER.info("自动更改IP地址成功!");
 return true;
 }
 return false;
 }
 public static boolean connect(Map<String, String> cookies){
 return execute(cookies, "3");
 }
 public static boolean disConnect(Map<String, String> cookies){
 return execute(cookies, "4");
 }
 public static boolean execute(Map<String, String> cookies, String action){
 String url = "http://192.168.0.1/goform/SysStatusHandle";
 Map<String, String> map = new HashMap<>();
 map.put("action", action);
 map.put("CMD", "WAN_CON");
 map.put("GO", "system_status.asp");
 Connection conn = Jsoup.connect(url)
 .header("Accept", ACCEPT)
 .header("Accept-Encoding", ENCODING)
 .header("Accept-Language", LANGUAGE)
 .header("Connection", CONNECTION)
 .header("Host", HOST)
 .header("Referer", REFERER)
 .header("User-Agent", USER_AGENT)
 .ignoreContentType(true)
 .timeout(30000);
 for(String cookie : cookies.keySet()){
 conn.cookie(cookie, cookies.get(cookie));
 }
  
 String title = null;
 try {
 Connection.Response response = conn.method(Connection.Method.POST).data(map).execute();
 String html = response.body();
 Document doc = Jsoup.parse(html);
 title = doc.title();
 LOGGER.info("操作连接页面标题:"+title);
 }catch (Exception e){
 LOGGER.error(e.getMessage(), e);
 }
 if("LAN | LAN Settings".equals(title)){
 //发出命令5秒之后再检查网络状态
 try{Thread.sleep(5000);}catch (Exception e){LOGGER.error(e.getMessage(), e);}
 if(("3".equals(action) && isConnected())
 || ("4".equals(action) && !isConnected())){
 return true;
 }
 }
 return false;
 }
 public static boolean isConnected(){
 try {
 Document doc = Jsoup.connect("http://www.baidu.com/s?wd=杨尚川&t=" + System.currentTimeMillis())
 .header("Accept", ACCEPT)
 .header("Accept-Encoding", ENCODING)
 .header("Accept-Language", LANGUAGE)
 .header("Connection", CONNECTION)
 .header("Referer", "https://www.baidu.com")
 .header("Host", "www.baidu.com")
 .header("User-Agent", USER_AGENT)
 .ignoreContentType(true)
 .timeout(30000)
 .get();
 LOGGER.info("搜索结果页面标题:"+doc.title());
 if(doc.title() != null && doc.title().contains("杨尚川")){
 return true;
 }
 }catch (Exception e){
 if("Network is unreachable".equals(e.getMessage())){
 return false;
 }else{
 LOGGER.error("状态检查失败:"+e.getMessage(), e);
 }
 }
 return false;
 }
 public static Map<String, String> login(String userName, String password, String verify){
 try {
 Map<String, String> map = new HashMap<>();
 map.put("Username", userName);
 map.put("Password", password);
 map.put("checkEn", "0");
 Connection conn = Jsoup.connect("http://192.168.0.1/LoginCheck")
 .header("Accept", ACCEPT)
 .header("Accept-Encoding", ENCODING)
 .header("Accept-Language", LANGUAGE)
 .header("Connection", CONNECTION)
 .header("Referer", REFERER)
 .header("Host", HOST)
 .header("User-Agent", USER_AGENT)
 .ignoreContentType(true)
 .timeout(30000);
  
 Connection.Response response = conn.method(Connection.Method.POST).data(map).execute();
 String html = response.body();
 Document doc = Jsoup.parse(html);
 LOGGER.info("登陆页面标题:"+doc.title());
 Map<String, String> cookies = response.cookies();
 if(html.contains(verify)){
 cookies.put("success", Boolean.TRUE.toString());
 }
 LOGGER.info("*******************************************************cookies start:");
 cookies.keySet().stream().forEach((cookie) -> {
 LOGGER.info(cookie + ":" + cookies.get(cookie));
 });
 LOGGER.info("*******************************************************cookies end:");
 return cookies;
 }catch (Exception e){
 e.printStackTrace();
 }
 return Collections.emptyMap();
 }
 }
https://github.com/ysc/superword/blob/master/src/main/java/org/apdplat/superword/tools/DynamicIp.java

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值