【转载请注明原作者和原文链接】
转自:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=199811&fromuid=568472
用户名密码登录验证后保存Session 01 | // 你的URL |
02 | HttpPost httppost = new HttpPost(Constants.ServerUrl.WEB_URL + url); |
03 | try { |
04 |
05 | List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>( 2 ); |
06 |
07 | // Your DATA |
08 | nameValuePairs.add( new BasicNameValuePair( "UserName" , map |
09 | .get( "UserName" ))); |
10 |
11 | nameValuePairs.add( new BasicNameValuePair( "Password" , map |
12 | .get( "Password" ))); |
13 |
14 | httppost.setEntity( new UrlEncodedFormEntity(nameValuePairs)); |
15 |
16 | HttpResponse response; |
17 |
18 | response = httpclient.execute(httppost); |
19 | if (response.getStatusLine().getStatusCode() == 200 ) { |
20 | // 获得响应 |
21 | String result = EntityUtils.toString(response.getEntity()); |
22 | // SharedPreferences preferences = context.getSharedPreferences( |
23 | // "login", Context.MODE_PRIVATE); |
24 | // Editor editor = preferences.edit(); |
25 | // String handlers = ""; |
26 | String cookie = "" ; |
27 | Date sessionTime = new Date(); |
28 | List<Cookie> cookies = httpclient.getCookieStore().getCookies(); |
29 | if (!cookies.isEmpty()) { |
30 | for ( int i = 0 ; i < cookies.size(); i++) { |
31 | cookie = cookies.get(i).getName() + "=" |
32 | + cookies.get(i).getValue() + ";domain=" |
33 | + cookies.get(i).getDomain(); |
34 | ; |
35 | sessionTime = cookies.get(i).getExpiryDate(); |
36 | } |
37 | } |
38 | //保存session |
39 | PersistentConfig config = new PersistentConfig(context); |
40 | config.setCookie(cookie); |
41 |
42 | config.setSessionTime(sessionTime.getTime()); |
43 | // for (int i = 0; i < response.getAllHeaders().length; i++) { |
44 | // if (response.getAllHeaders()<i>.toString().contains( |
45 | // "set-cookie")) { |
46 | // handlers += response.getAllHeaders()<i>.toString(); |
47 | // break; |
48 | // } |
49 | // } |
50 | // |
51 | // editor.putString("cookies", |
52 | // response.getHeaders("Set-Cookie")[0].toString()); |
53 | // editor.commit(); |
54 | return result; |
55 | } |
56 |
57 | } catch (ClientProtocolException e) { |
58 |
59 | // TODO Auto-generated catch block |
60 |
61 | e.printStackTrace(); |
62 |
63 | } catch (IOException e) { |
64 |
65 | // TODO Auto-generated catch block |
66 |
67 | e.printStackTrace(); |
68 |
69 | }</i></i> |
如何在Webview请求网站时使用Session
01 | //webview初始化的时候调用 |
02 | main_view = (WebView) findViewById(R.id.main_webview); |
03 | new WebViewTask().execute(); |
04 | |
05 | private class WebViewTask extends AsyncTask<Void, Void, Boolean> { |
06 | String sessionCookie; |
07 | CookieManager cookieManager; |
08 |
09 | @Override |
10 | protected void onPreExecute() { |
11 | CookieSyncManager.createInstance(MainActivity. this ); |
12 | cookieManager = CookieManager.getInstance(); |
13 |
14 | sessionCookie = new PersistentConfig(getApplicationContext()) |
15 | .getCookieString(); |
16 | if (sessionCookie != null ) { |
17 | // delete old cookies |
18 | cookieManager.removeSessionCookie(); |
19 | } |
20 | super .onPreExecute(); |
21 | } |
22 |
23 | protected Boolean doInBackground(Void... param) { |
24 | // this is very important - THIS IS THE HACK |
25 | SystemClock.sleep( 1000 ); |
26 | return false ; |
27 | } |
28 |
29 | @Override |
30 | protected void onPostExecute(Boolean result) { |
31 | if (sessionCookie != null ) { |
32 | cookieManager.setCookie(Constants.ServerUrl.WEB_URL, |
33 | sessionCookie); |
34 | CookieSyncManager.getInstance().sync(); |
35 | } |
36 | main_view.setScrollbarFadingEnabled( true ); |
37 | main_view.getSettings().setPluginsEnabled( true ); |
38 | main_view.getSettings().setPluginState(PluginState.ON); |
39 | main_view.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); |
40 | WebSettings webSettings = main_view.getSettings(); |
41 | webSettings.setJavaScriptEnabled( true ); |
42 | // 设置可以访问文件 |
43 | webSettings.setAllowFileAccess( true ); |
44 | // 设置支持缩放 |
45 | webSettings.setBuiltInZoomControls( true ); |
46 | main_view.setWebViewClient( new WebViewClient() { |
47 | @Override |
48 | public void onPageStarted(WebView view, String url, |
49 | Bitmap favicon) { |
50 | // TODO Auto-generated method stub |
51 | super .onPageStarted(view, url, favicon); |
52 | } |
53 |
54 | @Override |
55 | public void onLoadResource(WebView view, String url) { |
56 | // history(); |
57 | } |
58 | }); |
59 | main_view.setWebChromeClient( new WebChromeClient() { |
60 | public void onProgressChanged(WebView view, int progress) { |
61 | //Change |
62 | } |
63 | }); |
64 | ShowUrlView(Constants.ServerUrl.INDEX); |
65 | } |
66 | } |
这样就可以每次刷新页面的时候带着session一起请求
如果想保存用户信息,还是老方法SharedPreferences