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

用浏览器访问服务端的时候,服务端都会随机生成一个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;
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值