android与Web服务器交互时的cookie使用
2012-05-12 23:04
225 查看
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;
}
当我们登录成功的时候,可以通过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;
}
相关文章推荐
- Android APP 使用WebView访问服务器H5页面带上Cookie
- Android通过使用webview实现和js的交互
- 使用java.net进行Android和服务器信息交互
- android与WEB服务器交互时,如何保证在同一个会话Session中通信
- android面试热点:webview使用及与js数据交互
- android使用JSON数据和服务器进行交互
- Android与JavaWeb服务器交互教程(1)-搭建环境
- Android网络应用之使用Scoket的Android客户端与TCP服务器的简单交互
- Android-使用JsBridge来优化js与本地webview的交互
- WebView的使用以及Android与Js的交互
- Android中使用WebView与JS交互全解析
- Android与WEB服务器交互时,如何保证在同一个会话Session中通信
- Android WebView使用方法详解 附js交互调用方法
- Android WebView的使用,与JS的交互
- iOS --- 使用RestKit与RESTful web服务器进行简单交互
- Android基础——WebView使用(二)Java与JS交互
- 【Android基础知识】HttpUrlConnection使用doGet和doPost方式和服务器交互
- Android与web服务器数据交互编程---3网络爬虫项目实现虚拟浏览器的jsp后台运行
- Android之使用HttpPost提交数据到服务器(Android手机客户端和后台服务器交互)