2016-09-05 126 views
1

如何獲取餅乾enter image description here如何從WebView獲取Cookie?

我的代碼,而不是從WebView

CookieManager cookieManager = CookieManager.getInstance(); 
cookieManager.setAcceptCookie(true); 
cookieManager.setCookie("http://xx.xxx.xxx.com","mid=GO ; Domain=.xxx.com"); 

String cookie = cookieManager.getCookie("http://xx.xxx.xxx.com"); 

Log.d("VOGA", "cookie ------>"+cookie); 
w.getSettings().setJavaScriptEnabled(true); 
w.setWebViewClient(new WebViewClient()); 
w.loadUrl("http://xx.xx.xxx.com"); 
setContentView(w); 

Thenks如需幫助

回答

2

,而不是漫長而枯燥的方法,我想你可以直接得到從web視圖餅乾(例如,在用戶登錄)由:

@Override 
public void onPageFinished(WebView view, String url){ 
    String myCookies = CookieManager.getInstance().getCookie(url); 
} 
2

你可以用下面的代碼讀取cookies:

public String getCookie(String siteName,String CookieName){  
    String CookieValue = null; 

    CookieManager cookieManager = CookieManager.getInstance(); 
    String cookies = cookieManager.getCookie(siteName); 
    if(cookies != null){ 
     String[] temp=cookies.split(";"); 
     for (String ar1 : temp){ 
      if(ar1.contains(CookieName)){ 
       String[] temp1=ar1.split("="); 
       CookieValue = temp1[1]; 
      } 
     }    
    } 
    return CookieValue;  
} 

而且請參考Android - extracting cookies after login in webview