Android中Http加载如何得到Cookie和 WebView 加载网页如何得到的Cookie
2017-01-07 00:00
627 查看
摘要: Android开发得到Cookie信息
最近做项目在手机端登录Http请求和 WebView 记载登录获取Cookie信息,可查看Cookie信息。
如图:
Http请求获取Cookie信息:
WebView加载网页获取Cookie
源码点击下载:https://github.com/DickyQie/android-webview
最近做项目在手机端登录Http请求和 WebView 记载登录获取Cookie信息,可查看Cookie信息。
如图:
Http请求获取Cookie信息:
public static String request(String httpUrl, String params, Context context) { BufferedReader reader = null; String result = null; String httpurl = " http://xwwscs.com"; StringBuffer sbf = new StringBuffer(); try { URL url = new URL(httpurl + "/app.php"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Cookie", CookieUtil.getParam(context).toString()); connection.setConnectTimeout(3000); // 是否输入参数 connection.setDoOutput(true); byte[] bypes = params.toString().getBytes(); connection.getOutputStream().write(bypes);// 输入参数 connection.connect(); InputStream is = connection.getInputStream(); reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); String strRead = null; while ((strRead = reader.readLine()) != null) { sbf.append(strRead); sbf.append("\r\n"); } reader.close(); Map<String, List<String>> cookie_map = connection.getHeaderFields(); List<String> cookies = cookie_map.get("Set-Cookie"); if (null != cookies && 0 < cookies.size()) { String s = ""; for (String cookie : cookies) { if (s.isEmpty()) { s = cookie; } else { s += ";" + cookie; } } Log.i("cookie", s); } result = sbf.toString(); } catch (Exception e) { result = "error"; e.printStackTrace(); } return result; } }
WebView加载网页获取Cookie
webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // TODO Auto-generated method stub return super.shouldOverrideUrlLoading(view, url); } @Override public void onPageFinished(WebView view, String url) { CookieManager cookieManager = CookieManager.getInstance(); String CookieStr = cookieManager.getCookie(url); if(CookieStr!=null) { Log.i("cookie", CookieStr); } super.onPageFinished(view, url); } });
源码点击下载:https://github.com/DickyQie/android-webview
相关文章推荐
- Android中Http加载如何得到Cookie和 WebView 加载网页如何得到的Cookie
- Android中Http加载如何得到Cookie和 WebView 加载网页如何得到的Cookie
- Android 编程下 WebView 加载一个网页如何得到网页的 Cookie 值
- Android 编程下 WebView 加载一个网页如何得到网页的 Cookie 值
- 求助: Android 加载 webview, 点击webview 中网页时间, 如何触发 Android 事件
- Android Webview新认识(获取cookie,所加载网页内的链接的url)
- android中webview携带cookie以及webview所加载网页中js调用java方法问题
- Android_WebView加载网页保存信息到Cookie
- 详解android 用webview加载网页(https和http)
- 【Android基础学习】webview加载网页,tel协议不会调出拨号盘?该如何处理
- android 用webview加载网页(https和http)
- 如何解决Android中的WebView直接加载网页出错?
- Android webview加载https网页时http图片无法显示
- android 学习心得---webview使用得到网页加载时间
- Android--当webview加载网页的时候在标题栏上显示加载进度
- android webview 加载网页显示对话框
- Android中使用WebView, WebChromeClient和WebViewClient加载网页
- Android中如何判别WebView加载完毕并使用ProgressDialog进行提示
- WebView加载网页保存信息到Cookie
- Android--当webview加载网页的时候在标题栏上显示加载进度