Simple case of getting web cookies

Original link: https://blog.saky.site/post/web-cookie/

For example the original cookie for a web page is

 uu=hfsjfyesu;

The cookie after successful login is

 userId=558456454; token=wwwdwblogcnwwwdwblogcnwwwdwblogcnwwwdwblogcnwwwdwblogcn; uu=hfsjfyesu; pin=sirhexs

Then we first write an if statement to determine whether the login is successful

 if(webcookie:find"token=")then --上面这个if的意思是判断webcookie里面是否存在“token=”这个字符串--这里是登录成功后要进行的操作,我们在这里写我们的cookie截取规则end

Then we write a rule to intercept the token according to the rules and code api

Code to use:

 str="左中右" --取字符串左边左=str:match("(.+)中") --取字符串中间中=str:match("左(.-)右") --取字符串右边右=str:match("中(.+)")

accomplish:

 if(webcookie:find"token=")then cookie="token="..webcookie:match("token=(.-)uu=") --上面这行的意思是截取“token=”到“uu=”中间的这段字符串,然后拼接到“token=”的后面,“..”这两个点相当于一个加号,用来拼接两端字符串。 mycookietxt.setText(cookie)--设置文本内容cookielay.setVisibility(View.VISIBLE)--显示控件end

This completes the interception of a cookie.

Complete code reference (take Ele.me as an example):

 require "import" import "android.app.*" import "android.os.*" import "android.widget.*" import "android.view.*" import "layout" import "android.webkit.CookieSyncManager" import "android.webkit.CookieManager" import "android.content.Context" import "android.content.Intent" import "android.net.Uri" import "android.view.View" --上面为导入的包,看不懂就不要弄这部分activity.setTheme(R.Theme_Blue) --设置标题activity.setTitle("饿了么Cookie") activity.setContentView(loadlayout(layout)) ---------------------------------------------------- --如果想获取其他网页的cookie也不需要太大的变动,只需更改第loginurl值和相关的if语句。 ---------------------------------------------------- --登录链接loginurl="https://tb.ele.me/wow/msite/act/login/?loginby=sns&redirect=https://h5.ele.me/minisite/pages/my/index?spm=a2ogi.13147251.0.0&spm-pre=a2ogi.13147251.0.0&spm=a2f6g.14291182.ebridge.login" --加载登录页面web.loadUrl(loginurl) --设置网页cookie函数function setCookie(context,url,content) CookieSyncManager.createInstance(context) local cookieManager = CookieManager.getInstance() cookieManager.setAcceptCookie(true) cookieManager.setCookie(url, content) CookieSyncManager.getInstance().sync() end --获取cookie函数function getCookie(url) local cookieManager = CookieManager.getInstance(); return cookieManager.getCookie(url); end --删除网页cookie函数function delCookie() local cookieManager = CookieManager.getInstance() cookieManager.removeSessionCookie() cookieManager.removeAllCookie() end --网页状态监听web.setWebViewClient{ shouldOverrideUrlLoading=function(view,url) --Url即将跳转end, onPageStarted=function(view,url,favicon) --网页加载end, onPageFinished=function(view,url) --网页加载完成--获取当前网页的cookie webcookie=getCookie(url) --判断cookie是否为空if webcookie~=nil then --其他网页cookie的截取只需改下面这个if语句if(webcookie:find"SID=")then cookie=webcookie mycookietxt.setText(cookie)--设置文本内容cookielay.setVisibility(View.VISIBLE)--显示控件end end end} --复制按钮cklaymyck.onClick=function() import "android.content.Context" activity.getSystemService(Context.CLIPBOARD_SERVICE).setText(cookie) print("复制成功") end --重新获取按钮cklaycxhqck.onClick=function() cookielay.setVisibility(View.GONE)--隐藏控件delCookie(web.getUrl()) cookie="" os.execute("rm -rf /data/data/"..this.packageName.."/cache/") os.execute("rm -rf /data/data/"..this.packageName.."/files/data/") -- web.reload()--刷新网页web.loadUrl(weburl)--加载网页end

Several other commonly used software grab the key code of ck :

JD.com

 loginurl="https://plogin.m.jd.com/login/login?appid=445&returnurl=https%3A%2F%2Fm.healthjd.com%2F%3Fyyjdfromflag%3Dlogin" if(webcookie:find"pt_key")then --正则截取ck cookie0=webcookie:match("pt_key=(.-)pt_token") --补全cookie cookie="pt_key="..cookie0 if(cookie:find"pwdt_id") then cookie=cookie:match("(.+)pwdt_id") end mycookietxt.setText(cookie)--设置文本内容cookielay.setVisibility(View.VISIBLE)--显示控件end

Meituan

 loginurl="https://passport.meituan.com/useraccount/ilogin?risk_partner=-1&risk_app=-1&risk_platform=3&backurl=https://i.meituan.com/account" if(webcookie:find"token=")then token="token="..webcookie:match("token=(.-);") userId="userId="..webcookie:match("userId=(.-);") cookie=userId.."&"..token mycookietxt.setText(cookie)--设置文本内容cookielay.setVisibility(View.VISIBLE)--显示控件end

NetEase Cloud

 loginurl="https://y.music.163.com/m/login?targetUrl=/creatorcenter#/phone" if(webcookie:find"MUSIC_U")then --正则截取ck cookie0=webcookie:match("MUSIC_U=(.+)") --补全cookie cookie="MUSIC_U="..cookie0 while cookie:find";" do cookie=cookie:match("(.+);") end mycookietxt.setText(cookie)--设置文本内容cookielay.setVisibility(View.VISIBLE)--显示控件end

Reference article:

https://ift.tt/moZk09D

This article is reprinted from: https://blog.saky.site/post/web-cookie/
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment