Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android(安卓)WebView設置cookie

Android(安卓)WebView設置cookie

編輯:關於Android編程

最近兩天一直想用安卓模擬登陸,利用新的WebView顯示登陸後可以訪問的頁面,但是不管怎麼訪問需要登陸後才能訪問的頁面,還是跳回到登陸頁,後來網上看了下是cookie沒有設置,找了半天才到到合適的設置方法:


登陸方法:

private Cookie cookie;
	public static  HttpContext localContext;
	public static  List cookies;//此為保存的cookie
	public  String cookieStr;

	// public static Cookie cookie = null; 
	/**
	 * 登陸時保存cookie
	 * @param url
	 * @param data
	 * @return
	 */
	public String login(String url,String data[]){
		HttpClient client = null;
		String html = null;
		try {
			client = new DefaultHttpClient();
			// 創建cookie store的本地實例  
			CookieStore cookieStore = new BasicCookieStore();  
			localContext = new BasicHttpContext();  
			// 綁定定制的cookie store到本地內容中  
			localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); 
			HttpPost post = new HttpPost(url);
			List parameters = new ArrayList();
			parameters.add(new BasicNameValuePair("username", data[0]));
			parameters.add(new BasicNameValuePair("passwd", data[1]));
			parameters.add(new BasicNameValuePair("login", "%B5%C7%A1%A1%C2%BC"));
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,"utf-8");
			post.setEntity(entity);
			
			HttpResponse response = client.execute(post,localContext);
			if(response.getStatusLine().getStatusCode() == 200){
				InputStream content = response.getEntity().getContent();
				BufferedReader buffer = new BufferedReader(new InputStreamReader(content,"gbk"));
				String line = null;
				while((line=buffer.readLine())!=null){
					html+=line;
				}
				  cookies = cookieStore.getCookies();
				 System.out.println("cookies.size():"+cookies.size());
				 if (!cookies.isEmpty()) {  
					 for(int i=0;i

Activity中的oncreate()方法:

private WebView wvTempShow;
	private String receiveUrl;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_temp_web_view);
		wvTempShow = (WebView) findViewById(R.id.wv_tempShow);
		Intent intent = this.getIntent();
		receiveUrl = intent.getStringExtra("openUrl");
		//獲得cookie管理者
		CookieManager cookieManager = CookieManager.getInstance();
		//獲取登陸時的cookie
		String oldCookie = 
				UtilsLogin.cookies.get(0).getName()+"="+UtilsLogin.cookies.get(0).getValue()+";"+
				UtilsLogin.cookies.get(1).getName()+"="+UtilsLogin.cookies.get(1).getValue()+";" ;
		System.out.println("oldCookie:"+oldCookie);
		cookieManager.setCookie(receiveUrl, oldCookie);
		wvTempShow.getSettings().setDefaultTextEncodingName("gbk");
		wvTempShow.loadUrl(receiveUrl);
		wvTempShow.setWebViewClient(new MyWebViewClient());
	}

	class MyWebViewClient extends WebViewClient{
		
		
		
		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			super.onPageStarted(view, url, favicon);
			  
		}
		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);
		}
	}

需要注意的是:要看訪問的頁面需要什麼樣的cookie字符串可以用以下方法:

CookieManager cookieManager = CookieManager.getInstance();
			String CookieStr = cookieManager.getCookie("http://bkjw.guet.edu.cn/student/public/menu.asp?menu=mnall.asp");
            System.out.println("TempWebViewonPageFinished = " + CookieStr);


然後自己像以上String oldCookie中一樣自己拼好,再在cookieManager.setCookie(url,cookieString);中設置



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved