偷懒系列--自动登录网站脚本

背景

最近需要频繁的登录4A系统,可是移动效率如此低下,申请个账号好几个月了还不行。

无奈,好多同事共用同一账号,但是每次都需要将手机号改为自己的。好麻烦。
今天加班,事情不多。写个脚本替我做吧。

过程

1.先了解下原理是怎么样的
简单的抓了个包,看了看源码,发现原理很简单的。3个http请求就搞定了。
2.怎么做
第一个想到的肯定是老本行-JAVA,吃饭的家伙。直接上代码。
public class Modify4ATempMobile{
	private static HttpClient client = null;
	public static void main(String[] args) throws Exception {
		String loginURL = "http://4aportal.bmcc.com.cn/userset/pwdmanage.do?method=getPwdProtectBeforeLoginForTempMobile";
		String checkURL = "http://4aportal.bmcc.com.cn/userset/pwdmanage.do?method=checkPwdProtectBeforeLoginForTempMobile";
		String updateURL = "http://4aportal.bmcc.com.cn/userset/pwdmanage.do?method=setTempMobile";
		Map<String, String> param = new HashMap<String, String>();
		param.put("userId", "crm_xxx");
		param.put("authCode", "xxx12345");
		System.out.println(doPost(loginURL, param));
		param.clear();
		param.put("question", "xx");
		param.put("answer", "xx");
		System.out.println(doPost(checkURL, param));
		param.clear();
		param.put("tempmobile", "13814027174");
		System.out.println(doPost(updateURL, param));
	}
	
	public static String doPost(String url, Map<String, String> parameters) throws IOException {
		//使用同一个client,保持session
		if(client == null){
			client = getHttpClient();
		}
		PostMethod method = new PostMethod(url);
    	if(parameters != null && !parameters.isEmpty()){
    		NameValuePair[] list = new NameValuePair[parameters.size()];
            int i = 0;
            Entry<String, String> entry = null;
            for(Iterator<Entry<String, String>> iterator = parameters.entrySet().iterator(); iterator.hasNext(); ){
            	entry = (Entry<String, String>) iterator.next();
             	list[i++] = new NameValuePair(entry.getKey(), entry.getValue());
            }
 			method.setRequestBody(list);
         }
		client.executeMethod(method);
		return method.getResponseBodyAsString();
	}
	
	public static HttpClient getHttpClient(){
		HttpClient client = new HttpClient();
		
		//代理设置,不用可以去掉
		client.getHostConfiguration().setProxy("proxy.bmcc.com.cn", 8080);    
		client.getParams().setAuthenticationPreemptive(true);    
		//如果代理需要密码验证,这里设置用户名密码    
		client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials("ngcrm_xxx","xxx"));  
		//end 代理
		
		client.getHttpConnectionManager().getParams().setConnectionTimeout(0);
		client.getHttpConnectionManager().getParams().setSoTimeout(0);
		return client;
	}
}
 
跑了下,没有问题,完美解决问题。
3.做成小工具
本着学雷锋的精神,准备分享给其他的同事。可是发现,跑代码改手机号,是不是有点小题大做啊。
既然是小工具,那就有个小工具的样子。于是拷出class,拷出jar包,写个脚本,这才像个小工具嘛
 
@echo off
setlocal enabledelayedexpansion
set work_path=D:\Work-bj\crm_bj\lib
d:
cd %work_path%
for %%F in (%work_path%\apache\*.jar) do call :addcp %%F

for %%F in (%work_path%\appframe\*.jar) do call :addcp %%F

for %%F in (%work_path%\other\*.jar) do call :addcp %%F

for %%F in (%work_path%\runtime\*.jar) do call :addcp %%F

set classpath= .;D:/Work-bj/crm_bj/html/WEB-INF/classes;%CLASSPATH%

java com.asiainfo.test.Modify4ATempMobile

pause

:addcp
echo %1
SET CLASSPATH=%CLASSPATH%;%1
 
4.改用VBS试试
做完后觉得不错,可是想想太土了。能不能简单点呢。突然就想用vbs试试。虽然没写过,不过不就是代码嘛,试试。
 
Const userId="crm_xxx"
Const passwd="xxx12345"
Const question="xxx"
Const answer="xxx"
Const tempmobile="13814027174"

Dim http
Set http = CreateObject("Msxml2.XMLHTTP")
http.open "POST", "http://4aportal.bmcc.com.cn/userset/pwdmanage.do?method=getPwdProtectBeforeLoginForTempMobile", False
http.setRequestHeader "CONTENT-TYPE","application/x-www-form-urlencoded"
http.send "authCode=" & passwd & "&userId=" & userId

http.open "POST", "http://4aportal.bmcc.com.cn/userset/pwdmanage.do?method=checkPwdProtectBeforeLoginForTempMobile", False
http.setRequestHeader "CONTENT-TYPE","application/x-www-form-urlencoded"
http.send "question=" & question & "&answer=" & answer

http.open "POST", "http://4aportal.bmcc.com.cn/userset/pwdmanage.do?method=setTempMobile", False
http.setRequestHeader "CONTENT-TYPE","application/x-www-form-urlencoded"
http.send "tempmobile=" & tempmobile
 
5.继续完善下
很早就知道,VBS能模拟操作。趁着这个机会,我也试试。于是加了如下代码可以自动打开浏览器并登陆了。
 
Set ie = CreateObject("InternetExplorer.Application")
ie.visible=true
ie.Navigate "http://4aportal.bmcc.com.cn/"
do until 4=ie.readyState
	do until 4=ie.readyState
	waittime  = waittime + 200
    if waittime > 15000 then exit do
	Loop
Loop

if 4<>ie.readyState then
    ie.quit
    WScript.quit
end if

set dom = ie.document
set form = dom.getElementById("logonForm")
form.all("name").value = userId
form.all("pwd").value = passwd
'短信登录
'form.all("button4").click()
'语音登录
form.all("button5").click()

 
自动登陆完成后,我就意识到,能不能将整个过程都做成可视化的,自动的呢。毕竟后台改数据,出问题也不好定位。经过研究,最终结果如下。
'请修改信息为需要的信息。

'用户名
Const userId = "crm_xxx"
'密码
Const passwd = "xxx12345"
'密保问题
Const question = "xxx"
'密保答案
Const answer = "xxx"
'临时手机号 
Const tempmobile = "13814027174"
'短信登录:1 语音登录:2
Const logintype = 2

' 如果打开默认的ie呢 	
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = true
ie.Top = 0
ie.Left = 0
ie.Navigate "http://4aportal.bmcc.com.cn/iam/belogin/login_findpwdproduct_tempmobile.jsp"

wait4ieready()

'发送alt+空格+x最大化
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys "%+(' 'x)"

ie.document.getelementByid("userId").value = userId
ie.document.getelementByid("authCode").value = passwd
ie.document.getelementByid("confirm").click()

wait4ieready()

ie.document.getElementById("answer").value = answer
ie.document.getElementById("confirm").click()

wait4ieready()

ie.document.getElementById("tempmobile").value = tempmobile
ie.document.getElementById("confirm").click()

wait4ieready()

ie.document.getElementById("skip").click()

wait4ieready()

ie.document.getElementById("name").value = userId
ie.document.getElementById("pwd").value = passwd

if logintype = 1 then
	'短信登录
	form.all("button4").click()
else
	'语音登录
	ie.document.getElementById("button5").click()
end if

sub wait4ieready()
	do while ie.ReadyState <> 4 or ie.busy   
		wscript.sleep 200                  
	loop
end sub
  
总结
这样好多了,不得不说,看起来好玩多了。

猜你喜欢

转载自z1041950008.iteye.com/blog/2187989