自动更改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
自动更改IP地址反爬虫封锁相关推荐
- java 反查域名_爬虫实现:根据IP地址反查域名
域名解析与IP地址 域名解析是把域名指向网站空间IP,让人们通过注册的域名可以方便地访问到网站的一种服务:IP地址是网络上标识站点的数字地址,为了方便记忆,采用域名来代替IP地址标识站点地址.域名解析 ...
- 用Python更改IP地址(转)
用Python更改IP地址(转) 大多数用笔记本电脑的朋友都有一个烦恼,那就是在家里和公司的IP地址不一样,上班和回家后都得来回切换IP地址,两个字"麻烦".最近在写Python, ...
- C++不重起Windows直接更改IP地址
出处:http://www.cppblog.com/lizao2/archive/2012/10/11/193147.aspx 源代码运行效果图如下: 设置IP地址只需要更改注册表中关于适配器的相应设 ...
- 修改sep客户端服务器地址,SEP服务更改IP地址操作手册
<SEP服务更改IP地址操作手册>由会员分享,可在线阅读,更多相关<SEP服务更改IP地址操作手册(6页珍藏版)>请在人人文库网上搜索. 1.SEP服务器更换IP地址操作手册1 ...
- win10计算机ip如何更改,Win10本地连接ip怎么更改_Win10怎么更改ip地址?-192路由网...
问:Win10本地连接IP地址怎么更改? 本人是电脑小白,新买的电脑,是Win10系统:请问Win10电脑中的IP地址在哪里更改? 答:Win10系统IP地址需要在"本地连接"的 ...
- 如何更改IP地址使用代理ip软件
如果您想更改家用计算机上的 IP 地址,有几种方法可能适合您--有些简单,有些则不然.在尝试下面描述的更复杂/技术方法之前,您可以尝试一些非常简单的方法. 只需关闭或拔下调制解调器约五分钟.(您不必关 ...
- 14、DHCP(自动分配IP地址)
DHCP(自动分配IP地址) DHCP ( Dynamic Host Conf iguration Protocol,动态主机配置协议)是一个局域网的网络协议,使用UDP协议工作,主要有两个用途: ...
- html自动获取ip地址,自动获取ip地址怎么设置无线路由器?
自动获取ip地址怎么设置无线路由器?如果用户的宽带类型是:自动获取IP地址上网的,那么在设置无线路由器的时候,正常情况下,只需要在"WAN口连接类型"或者"上网方式&qu ...
- “自动获取IP地址”和“使用固定IP地址”的区别是什么?
自动获取IP地址是动态IP,是通过DHCP来获取IP地址,每次上网这个IP都不一样,在IP地址租期满后就有可能会换IP地址了. 使用固定IP地址是静态IP,是自己指定一个IP地址,ISP随时可以到你家 ...
- 电脑自动获取ip地址在哪里设置
第一 点击电脑左下角->控制面板 第二 点击 网络和Internet 第三 网络和共享中心 第四 更改适配器设置 第五 点击本地链接 第六 点击 Internet 协议版本4(TCP/IPV4 ...
最新文章
- seafile服务端的搭建
- 创造包容的环境和上升空间
- ASP.NET MVC V2 Preview 1 发布 期望VS有更好的表现
- cd linux给u盘安装程序,使用U盘安装CDlinux
- 近千人观看live,晚8点继续安排,2个CPU过高案例+1个文件数据删除案例-Oracle故障分析的方法论+DBA能力提升要领...
- 从CTF比赛真题中学习压缩包伪加密与图片隐写术
- 命令:	LIST 响应:	150 Opening BINARY mode data connection. 错误:	20 秒后无活动,连接超时 错误:	读取目录列表失败
- k8s-kubectl进程源码分析
- R语言学习笔记:简单的回归分析
- 十字路口旁边有一个路口_观察路口观察员
- 基于C#的学生综合教务管理系统
- 企业微信开发服务端报错汇总(手把手教你企业微信开发五)
- 如何让移动端出现横向滚动条_纯css实现移动端横向滑动列表overflow:atuo;隐藏滚动条...
- 微信小程序使用animation动画实现消息从左向右滚动
- 高考早知道:自主招生,能用低分读名校,就别再拼高分挤独木桥
- 相似矩阵和相似对角化
- 文本prompting综述
- 流量劫持是什么?常用方法有哪些?
- 关于实现StarGen的思考记录
- Firm C下的优化算法
热门文章
- 工业相机——点阵相机与线阵相机
- 74ls20设计半加器_组合逻辑电路(半加器全加器及逻辑运算)实验报告
- 苹果手机投屏软件_苹果手机怎样投屏到笔记本?
- python itchat库学习笔记 + 微信防撤回实现详解(超详细)(已上传)
- 除了秀米,微信排版还有什么好用的? ---短网址
- 兽药销售终端如何摆脱客情困局
- 微信小程序 - 方法
- 安装docker提示“Another app is currently holding the yum lock; waiting for it to exit“之解决办法
- codesys工程ST语言学习笔记(六)ST语言读写CVS文件excel格式(文件读写)
- 开发者必看:Google Play应用上架流程(希望你不踩坑!)