Android 访问WebService登录通过Cookie保存登录状态

当我们App的一些操作需要用户登录之后才能操作的时候,如果不记录用户登录时的Cookie,用户进行其他需要登录之后才能进行操作的业务时候会显示如下:

{"successful":"false","code":"ETS-5BP9951","message":"您未登录或长时间未操作,请先登录!","details":""}
当出现以上情况的时候就是没有记录Cookie,两次请求的不是同一个Cookie的缘故。

一下是我个人对HttpClient 进行Cookie的添加,希望撸友给指点指点。

/**
	 * 从服务器通过post方式提交数据
	 * 
	 * @param path
	 *            路径
	 * @param param
	 *            参数
	 * @return String
	 * @throws Exception
	 */
	public static String postClient(String path, Map<String, String> paramMap)
			throws Exception {
		  String string = null;
		  Log.e("url",path);
		DefaultHttpClient client = new DefaultHttpClient();
		HttpPost request = new HttpPost(path);
		if (!MyAppaction.isLogin) {//当进行的操作不是登录的时候,给请求设置Cookie
			request.setHeader("Cookie",
					MyAppaction.sharePreferences.readString("cookie", ""));
		}
		System.out.println(paramMap.toString());
		List<NameValuePair> nvps = new ArrayList<NameValuePair>(  
                paramMap.size());  
		 if (paramMap != null) {  
             for (Map.Entry<String, String> entry : paramMap.entrySet()) {  
                 NameValuePair nvp = new BasicNameValuePair(entry.getKey(),  
                         entry.getValue());  
                 nvps.add(nvp);  
             } 
             UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nvps,
     				"utf-8");
             request.setEntity(entity);
		 }
		HttpResponse response;
		try {
			response = client.execute(request);
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				is = response.getEntity().getContent();
				long length = response.getEntity().getContentLength();
				string = InputStreamToString.MyIsToString(is);
				if (MyAppaction.isLogin) {// 只有当为登录的时候才用
					getCookie(client);//当操作是用户登录的时候记录Cookie
				}
				Log.e("response",string);
				return string;
			} else if (response.getStatusLine().getStatusCode() == HttpStatus.SC_GATEWAY_TIMEOUT) {
				return "请求超时";
			} else {
				return "链接错误";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (client != null) {
				client.getConnectionManager().shutdown();
			}
		}
		return string;
	}

方法中的getCookie()如下

/**
	 * 存储Cookie
	 * @param httpClient
	 */
	private static void getCookie(DefaultHttpClient httpClient) {
		List<Cookie> cookies = httpClient.getCookieStore().getCookies();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < cookies.size(); i++) {
			Cookie cookie = cookies.get(i);
			String cookieName = cookie.getName();
			String cookieValue = cookie.getValue();
			if (!TextUtils.isEmpty(cookieName)
					&& !TextUtils.isEmpty(cookieValue)) {
				sb.append(cookieName + "=");
				sb.append(cookieValue + ";");
			}
		}
		Log.e("cookie", sb.toString());
		MyAppaction.sharePreferences.writeString("cookie", sb.toString());
	}
以上是在开发过程中遇见的,希望能帮助到有需要的撸有,也希望撸友给兄弟我推荐更好的。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值