Android中Http加载如何得到Cookie和 WebView 加载网页如何得到的Cookie
最近做项目在手机端登录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的更多相关文章
- 在当前的webview中跳转到新的url 使用WebView组件显示网页
如果希望点击链接由自己处理,而不是新开Android的系统browser中响应该链接.给WebView加一个事件监听对象(WebViewClient)并重写其中的一些方法:shouldOverride ...
- android中的MD5、Base64、DES/3DES/ADES加解密
MD5摘要算法: <span style="font-size:18px;">主要代码: String s = edit.getText().toString(); i ...
- Android开发必知--WebView加载html5实现炫酷引导页面
大多数人都知道,一个APP的引导页面还是挺重要的,不过要想通过原生的Android代码做出一个非常炫酷的引导页相对还是比较复杂的,正巧html5在制作炫酷动画网页方面比较给力,我们不妨先利用html5 ...
- Android中常见的图片加载框架
图片加载涉及到图片的缓存.图片的处理.图片的显示等.而随着市面上手机设备的硬件水平飞速发展,对图片的显示要求越来越高,稍微处理不好就会造成内存溢出等问题.很多软件厂家的通用做法就是借用第三方的框架进行 ...
- Android中ListView动态加载数据
1. 引言: 为了提高ListView的效率和应用程序的性能,在Android应用程序中不应该一次性加载ListView所要显示的全部信息,而是采取分批加载策略,随着用户的滑动,动态的从后台加载所需的 ...
- Android中加载位图的方法
Android中加载位图的关键的代码: AssetManager assets =context.getAssets(); //用一个AssetManager 对象来从应用程序包的已编译资源中为工程加 ...
- Android中ViewPager+Fragment取消(禁止)预加载延迟加载(懒加载)问题解决方案
转载请注明出处:http://blog.csdn.net/linglongxin24/article/details/53205878本文出自[DylanAndroid的博客] Android中Vie ...
- Android中插件开发篇之----动态加载Activity(免安装运行程序)
一.前言 又到周末了,时间过的很快,今天我们来看一下Android中插件开发篇的最后一篇文章的内容:动态加载Activity(免安装运行程序),在上一篇文章中说道了,如何动态加载资源(应用换肤原理解析 ...
- Android中的动态加载机制
在目前的软硬件环境下,Native App与Web App在用户体验上有着明显的优势,但在实际项目中有些会因为业务的频繁变更而频繁的升级客户端,造成较差的用户体验,而这也恰恰是Web App的优势.本 ...
随机推荐
- VS 远程调试之 “The visual studio remote debugger does not support this edition of windows”
The error message "The visual studio remote debugger does not support this edition of windows&q ...
- Django 静态文件配置(static files)
Django version: 1.9 Python versrion: 3.5.2 这几天Django配置静态文件(本例是要加载index.css), 总是不对,最后终于试对了,这里记录下,方便以后 ...
- LVM原理及PV、VG、LV、PE、LE关系图
PV(physical volume):物理卷在逻辑卷管理系统最底层,可为整个物理硬盘或实际物理硬盘上的分区.VG(volume group):卷组建立在物理卷上,一卷组中至少要包括一物理卷,卷组建立 ...
- ZAM 3D 制作简单的3D字幕 流程(一)
本文原地址-> http://www.cnblogs.com/yk250/p/5663048.html 效果参考图:请查阅 http://www.cnblogs.com/yk250/p/5662 ...
- nginx 使用
1.下载nginx包http://files.cnblogs.com/files/jyjin/nginx.zip 2.解压后找到nginx.conf文件进行配置 3.配置server选项: serve ...
- 在C#中如何将多个rtf文件内容组合在一起用一个rtf文件保存?
//重点为是使用 SelectedRtf 属性 private void button1_Click( object sender, EventArgs e ) { //mergeRTF为并内容后的 ...
- 调用百度地图API出现 error inflating class com.baidu.mapapi.map.mapview
问题如下 本来以为解决了,但后来重新运行了一下,又坏了,然后改成原来的样子,又好了. 结果就是:对不住了各位看官,没找到解决办法,不过经测试有个地方,可能在程序运行时,出现error inflatin ...
- vi、vim 查找替换
vi/vim 中可以使用 :s 命令来替换字符串.该命令有很多种不同细节使用方法,可以实现复杂的功能,记录几种在此,方便以后查询. :s/vivian/sky/ 替换当前行第一个 vivian ...
- Delphi日期函数、日期加减
Delphi里有现成的函数可以实现日期加减,是在DateUtils单元里的. function IncYear(const AValue: TDateTime; const ANumberOfYear ...
- tomcat gzip compression not working for large js files
solution 1: <Connector port="8080" protocol="HTTP/1.1" connectionTimeout=&quo ...