Android 访问WebService登录通过Cookie保存登录状态
2015-01-08 17:29
465 查看
当我们App的一些操作需要用户登录之后才能操作的时候,如果不记录用户登录时的Cookie,用户进行其他需要登录之后才能进行操作的业务时候会显示如下:
一下是我个人对HttpClient 进行Cookie的添加,希望撸友给指点指点。
方法中的getCookie()如下
{"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()); }以上是在开发过程中遇见的,希望能帮助到有需要的撸有,也希望撸友给兄弟我推荐更好的。
相关文章推荐
- python通过本地保存的cookie文件实现登录并访问相关网页
- Android使用Xutils保存cookie实现登录后访问
- Android使用Xutils保存cookie实现登录后访问
- Android WebView请求,保存cookie,判断登录状态
- 通过cookie保存并读取用户登录信息实例
- 07-通过cookie保存并读取用户登录信息(jsp内置对象)
- Android 网页登录 POST 请求 保存 COOKIE
- 解决内网通过代理服务器访问外网部署WebService报错问题“请求因 HTTP 状态 407 失败” - 转载
- 解决内网通过代理服务器访问外网部署WebService报错问题“请求因 HTTP 状态 407 失败”
- Android客户端写Cookie和内嵌的网页实现登录状态的同步
- Android通过WebService访问服务器
- Android使用Asynchronous Http Client完成登录保存cookie的问题
- Android 网页登录 POST 请求 保存 COOKIE
- Android Cookie共享到WebView避免再次登录(保持登录状态)
- phonegap android app登录状态保存释疑
- Android Cookie共享到WebView避免再次登录(保持登录状态)
- 解决内网通过代理服务器访问外网部署WebService报错问题“请求因 HTTP 状态 407 失败”
- XE5 ANDROID通过webservice访问操作MSSQL数据库
- 解决内网通过代理服务器访问外网部署WebService报错问题“请求因 HTTP 状态 407 失败”
- android通过ksoap访问webservice方法传递一个复杂对象参数