您的位置:首页 > 移动开发 > Android开发

Android通过session与服务端保持一致

2015-11-29 05:06 513 查看
用浏览器访问服务端的时候,服务端都会随机生成一个session并发送给浏览器,浏览器获得这个session,之后每次访问都会把这个session添加到请求的头部信息中,服务端通过session来判断是否为同一用户。

而Android与服务端进行数据交换的时候有时候会用到session,下面就说一下关于Android如何获取并发送session的方法,另外也讲一下如果加上验证码该怎么请求信息。废话不多说,直接上代码,一看就懂。

1、带参数的请求方法。

public static HttpEntity getHttpEntity(String uri, JSONObject json) {
HttpEntity entity = null;
//创建DefaultHttpClient请求
DefaultHttpClient client = new DefaultHttpClient();
//设置参数
client.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);
HttpPost req = null;
try {
//创建http请求,用来发送参数
req = new HttpPost(uri);

//设置编码格式
StringEntity se = new StringEntity(json.toString(), "UTF-8");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));

req.setEntity(se);

//如果GlobalData.getSessionid()不为空,就以键值对的形式,发送session
if (GlobalData.getSessionid() != null) {
Log.i("session", "send:" + GlobalData.getSessionid());
req.setHeader("Cookie",
"JSESSIONID=" + GlobalData.getSessionid());
}

} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
try {
HttpResponse res = client.execute(req);
if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//如果GlobalData.getSessionid()为空,就获取并保存session
if (GlobalData.getSessionid() == null) {
// 获取sessionid
GlobalData.setSessionid(getsessionid(client));
Log.i("session", "get:" + GlobalData.getSessionid());
}

return entity = res.getEntity();

}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return entity;
}
接下来是从cookie中获取session的方法,以下都用到了

private static String getsessionid(DefaultHttpClient client) {
// 获取cookie
CookieStore cookieStore = (CookieStore) client.getCookieStore();
List<Cookie> list = cookieStore.getCookies();
Cookie cookie = list.get(0);
String sessionid = cookie.getValue();
return sessionid;
}


</pre><pre name="code" class="java">


2、带验证码的请求方式。

public static HttpEntity getHttpEntity(String uri,JSONObject json,String verifyCode) {
HttpEntity entity = null;
DefaultHttpClient client = new DefaultHttpClient();

client.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);
HttpPost req = null;
try {
req = new HttpPost(uri);

//封装请求参数,设置编码格式
StringEntity se = new StringEntity(json.toString(), "UTF-8");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
//发送参数
req.setEntity(se);

if (GlobalData.getSessionid() != null) {
req.setHeader("Cookie",
"JSESSIONID=" + GlobalData.getSessionid());
//添加验证码到头部信息
req.setHeader("verifyCode", "verifyCode=" + verifyCode);</span>
}

} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
try {
HttpResponse res = client.execute(req);
if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

if (GlobalData.getSessionid() == null) {
// 获取sessionid
GlobalData.setSessionid(getsessionid(client));
}

return entity = res.getEntity();

}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return entity;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: