android与Web服务器交互时的cookie使用-兼谈大众点评数据获得(原创)

简介: android与Web服务器交互时的cookie使用-兼谈大众点评数据获得(原创)

Android应用程序访问WEB服务器的时候,我们为了与服务器保持同一会话,也就是说当前登录用户与服务器的交互是在同一个SessionId下。

当我们登录成功的时候,可以通过HTTP请求获取到Cookie信息,其中包括会话的SessionId,同时也可以自己将SessionId放入Json中返回。Session我们可以用一个静态变量来存放,每次向服务器发送请求的时候将SessionId带过去,服务器会自动检验这个SessionId有没有失效。

同样下面是针对大众点评的城市切换的时候,android为了获得这个城市的信息,必须获得它的cookie数据,同时进行保存。

下面是具体的代码

/**
* 向网站发送get请求,url需按照api要求写,返回取得的信息。
* //这个专门给大众点评传入cookie参数用,目的是为了获得用户选择的城市信息
* @param url
* @param client
* @return String
* @author lvqiyong
*/
public static String getRequest1(String url, DefaultHttpClient client,
String charset) throws Exception {
String result = null;
int statusCode = 0;
HttpGet getMethod = new HttpGet(url);
Log.d(TAG, "do the getRequest,url=" + url + "");
try {
getMethod.setHeader("User-Agent", USER_AGENT);
getMethod.setHeader("Cookie", "cy=" + value);//这个专门给大众点评传入cookie参数用,目的是为了获得用户选择的城市信息
// 添加用户密码验证信息
// client.getCredentialsProvider().setCredentials(
// new AuthScope(null, -1),
// new UsernamePasswordCredentials(mUsername, mPassword));
HttpResponse httpResponse = client.execute(getMethod);
// statusCode == 200 正常
statusCode = httpResponse.getStatusLine().getStatusCode();
Log.d(TAG, "statuscode = " + statusCode);
// 处理返回的httpResponse信息
if (statusCode == 200) {
result = retrieveInputStream(httpResponse.getEntity(), charset);
Cookie cookie;
String cookname,cookvalue;
List<Cookie> cookies = client.getCookieStore().getCookies();
if (cookies.isEmpty()) {
Log.i(TAG, "-------Cookie NONE---------");
} else {
for (int i = 0; i < cookies.size(); i++) {
// 保存cookie
cookie = cookies.get(i);
cookname = cookie.getName().trim();
cookvalue = cookie.getValue().trim();
if(cookname.equals("cy")){
name = cookname;
value = cookvalue;
}
}
}
} else
result = "networkerror";
} catch (ConnectTimeoutException e) {// 超时或网络连接出错
result = "timeouterror";
// e.printStackTrace();
} catch (ClientProtocolException e) {
result = "networkerror";
// e.printStackTrace();
} catch (Exception e) {
result = "readerror";
Log.e(TAG, e.getMessage());
throw new Exception(e);
} finally {
getMethod.abort();
}
return result;
}
相关文章
|
4天前
|
Android开发
Android WindowFeature小探究,Android客户端Web页面通用性能优化实践
Android WindowFeature小探究,Android客户端Web页面通用性能优化实践
|
4天前
|
存储 Java Android开发
Android轻量级数据SparseArray详解
Android轻量级数据SparseArray详解
7 0
|
5天前
|
Android开发 数据库管理
Android如何在Activity和Service之间传递数据
Android如何在Activity和Service之间传递数据
12 3
|
5天前
|
XML JSON API
转Android上基于JSON的数据交互应用
转Android上基于JSON的数据交互应用
11 1
|
5天前
|
弹性计算 运维 监控
解密阿里云弹性计算:探索云服务器ECS的核心功能
阿里云ECS是核心计算服务,提供弹性云服务器资源,支持实例按需配置、集群管理和监控,集成安全防护,确保服务稳定、安全,助力高效业务运营。
94 0
|
3天前
|
弹性计算 运维 安全
阿里云ecs使用体验
整了台服务器部署项目上线
|
5天前
|
弹性计算
阿里云ECS的使用心得
本文主要讲述了我是如何了解到ECS,使用ECS的一些经验,以及自己的感悟心得
|
5天前
|
弹性计算
阿里云ECS使用体验
在申请高校学生免费体验阿里云ECS云服务器后的一些使用体验和感受。
|
5天前
|
存储 弹性计算 固态存储
阿里云服务器租用价格参考,云服务器收费标准与实时活动价格整理
阿里云服务器租用价格参考,本文更新了阿里云服务器最新的租赁费用,包括云服务器实时的活动价格与云服务器收费标准。经济型e实例云服务器4核16G10M带宽配置30.00元/1个月、90.00元/3个月,独享型通用算力型u1实例2核4G服务器仅需199元1年,轻量云服务器2核2G新用户专享价格61元/1年,计算型c7a实例2核4G配置特惠价625.68元/1年。更多阿里云服务器热门配置活动价格及云服务器租赁费用及活动价格见下文。
阿里云服务器租用价格参考,云服务器收费标准与实时活动价格整理
|
5天前
|
弹性计算 运维 安全
阿里云ecs使用体验
整了台服务器部署项目上线