知識點:
1. string.gmatch(Lua正則匹配查找到字符串的迭代器)
=======================================================
t={}
s="from=world, to=Lua"
for k,v in string.gmatch(s,"(%w+)=(%w+)") do
t[k]=v
end
for k, v in pairs(t) do
print(k,v)
end
s = "hello world from Lua"
for w in string.gmatch(s,"%a+") do
print(w)
end
=========================================================
2. string.format(Lua連接字符串)
=========================================================
postDataUserLogin = string.format("%s%s%s%s%s%s%s%s%s", "ctmName=","ctmName","&userName=","username","&password=","password","&checkCode=&oldAccessKey=","oldAccessKey","&langtype=Lang%3D%26Flag%3D1&isRememberMe=false")
=========================================================
3. string.gsub(Lua全局字符串替換)
=========================================================
tokenid = string.gsub("&token=11111111111", "&", "&")
=========================================================
4. 特殊字符:(). % + - * ? [ ^ $ 可以使用 % 作為這些特殊字符的轉義字符。
=========================================================
for v in string.gmatch(resultUserLogin,"
urlPathLoginOut=v
end
=========================================================
示例代碼:
function Login_51(ctmName,username,password)
html = LuaGetHttpResponseMS("http://ehire.51job.com/MainLogin.aspx","");
oldAccessKey=""
for v in string.gmatch(html,"") do -- 正則匹配oldAccessKey
oldAccessKey=v
end
postDataUserLogin = string.format("%s%s%s%s%s%s%s%s%s", "ctmName=",LuaUrlEncode(ctmName),"&userName=",username,"&password=",password,"&checkCode=&oldAccessKey=",oldAccessKey,"&langtype=Lang%3D%26Flag%3D1&isRememberMe=false")
resultUserLogin = LuaCreatePostHttpResponse("https://ehirelogin.51job.com/Member/UserLogin.aspx", postDataUserLogin, "http://ehire.51job.com/MainLogin.aspx");
if string.find(resultUserLogin, "已通過其他瀏覽器登錄到本系統") then
for v in string.gmatch(resultUserLogin,"
urlPathLoginOut=v
end
for v in string.gmatch(resultUserLogin,"") do -- 正則匹配urlPathLoginOut
loginOutVIEWSTATE=v
end
postDataLoginOut = string.format("%s%s", "__EVENTTARGET=gvOnLineUser&__EVENTARGUMENT=KickOut%240&__VIEWSTATE=",LuaUrlEncode(loginOutVIEWSTATE))
-- print(postDataLoginOut)
tokenid = string.format("%s%s", "http://ehire.51job.com/Member/UserOffline.aspx?tokenId=",urlPathLoginOut)
tokenid = string.gsub(tokenid, "&", "&"); -- 字符替換必須將&轉化為&
resultLoginOut = LuaCreatePostHttpResponse(tokenid,postDataLoginOut,"");
-- print(resultLoginOut)
resultUserLogin = LuaCreatePostHttpResponse("https://ehirelogin.51job.com/Member/UserLogin.aspx",postDataUserLogin,"http://ehire.51job.com/MainLogin.aspx");
-- print(resultUserLogin)
end
return resultUserLogin,postDataUserLogin,ctmName,tokenid;
end