android WebView 与 HttpClient Cookie

转载自:http://blog.sina.com.cn/s/blog_694448320101gftv.html
我的测试案例:

public class MainActivity extends ActionBarActivity {

    Button btnCookie;
    WebView webView;
    String url;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(MainActivity.this);
        cookieSyncManager.sync();
        btnCookie=(Button)findViewById(R.id.btnCookie);
        webView=(WebView)findViewById(R.id.webView);
        textView=(TextView)findViewById(R.id.textView);
        url="https://login.taobao.com/";
        webView.loadUrl(url);
        btnCookie.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CookieSyncManager.getInstance().sync();
                CookieManager cookieManager = CookieManager.getInstance();
                String cookieStr = cookieManager.getCookie(url);
                textView.setText("cookie:"+cookieStr);
            }
        });
    }
}




1.WebView中的Cookie信息,或者WebView访问某网络地址前设置其Cookie

CookieSyncManager  cookieSyncManager  =  CookieSyncManager.createInstance(MainActivity.this);
cookieSyncManager.sync();
CookieManager  cookieManager  =  CookieManager.getInstance();
System.out.println(cookieManager.getCookie("http://xxx.xxx.xxx"));

在webView加载完成某网页之后,可以同步cookie,从而得到想要的Cookie(得到的Cookie可以传入HttpClient中,也可以同步给其它的webview)


CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(MainActivity.this);

cookieSyncManager.sync();

CookieManager cookieManager = CookieManager.getInstance();

cookieManager.setCookie("http://weibo.com", "session_id=\"xxx==|xxxx|xxx\"");

CookieSyncManager.getInstance().sync();


WebViewSubView webViewSubView = newWebViewSubView();

myWebView= (WebView)findViewById(R.id.webview);

myWebView.getSettings().setJavaScriptEnabled(true);

myWebView.setWebViewClient(webViewSubView);

myWebView.loadUrl("http://xxx.xxx.xxx");


在WebView加载页面之前,可以选同步Cookie,然后再加载网页。


2.HttpClient访问某网络地址时,设置Cookie.

getMethod.setHeader("Cookie""session_id=\"xxx==|xxxxx|xxxx\"");
当然,也可以在PostMethod中设置

protected  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         textView = (TextView)findViewById(R.id.showhtml);
         webView = (WebView)findViewById(R.id.showhtmlinwebview);    
         String baseUrl =  "http://xxxx.xxx.com" ;
         HttpGet getMethod =  new  HttpGet(baseUrl);
         getMethod.setHeader("Cookie""session_id=\"xxx==|xxxxx|xxxx\"");
         HttpClient httpClient =  new  DefaultHttpClient();
         try  {
             HttpResponse response = httpClient.execute(getMethod);  //发起GET请求
             Log.i(TAG,  "resCode = "  + response.getStatusLine().getStatusCode());  //获取响应码
             String htmlResponse = EntityUtils.toString(response.getEntity(),  "utf-8" );          
                     System.out.println(htmlResponse);
             webView.getSettings().setJavaScriptEnabled( true );
             webView.loadData(htmlResponse,  "text/html" "utf-8" );
         catch  (ClientProtocolException e) {
             e.printStackTrace();
         catch  (IOException e) {
             e.printStackTrace();
         }
     }

你可能感兴趣的:(android WebView 与 HttpClient Cookie)