android studio中使用x5 webview来读写cookies的问题
本人新手,刚接触AS也没有多久,记录下两个问题.
1. 怎么在android studio中写入cookies
把写入cookies的动作放在了主界面的onCreate事件中了,看了腾讯的说明,说是要放在X5初始化完成以后,在这里应该已经初始化完成了.
String url="http://wx.xxxxxxx.com.cn"; com.tencent.smtt.sdk.CookieSyncManager.createInstance(this); com.tencent.smtt.sdk.CookieManager cm=com.tencent.smtt.sdk.CookieManager.getInstance(); cm.setAcceptCookie(true); cm.setCookie(url,"uid=134xxxxxxxx"); cm.setCookie(url,"pwd=wwwwwxxxxx"); if (Build.VERSION.SDK_INT < 21) { com.tencent.smtt.sdk.CookieSyncManager.getInstance().sync(); } else { com.tencent.smtt.sdk. CookieManager.getInstance().flush(); }
然后在使用了X5 webview中的fargment中在页面加载完成时,也能读取到cookies的信息
public void onPageFinished(WebView view, String url) { com.tencent.smtt.sdk.CookieManager cookieManager = com.tencent.smtt.sdk.CookieManager.getInstance(); String CookieStr = cookieManager.getCookie(url); Log.e("uid", "Cookies = " + CookieStr); super.onPageFinished(view, url); }
虽然代码很乱,但是基本上能用了
2. 怎么在asp.net的页面中把cookies读取出来的问题
发现读取由app客户端生成的cookies和网页端生成两者还是有区别的,主要是app客户端生成的cookies有一堆cookies组成,读取不方便,我笨也不知怎么组装一个有名称的cookies出来,只好把cookie都读一次,能读取到我设置的值,就认为它是可以用的,就像下边,有好办法的给回复一下.
Public Function ReadCookie() As String Dim username As String = "" Dim pass As String = "" For i = 0 To Request.Cookies.Count - 1 Select Case Request.Cookies(i).Name Case "uid" username = Request.Cookies(i).Values(0) Case "pwd" pass = Request.Cookies(i).Values(0) End Select Next username = username & "--" & pass Return username End Function
这样就读取到了, 放在自己的网页中使用了.