使用凌空获取Android中的服务器端会话ID [英] get server side session id in android using volley

查看:110
本文介绍了使用凌空获取Android中的服务器端会话ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android的新手,正在创建一个可通过Web服务与服务器通信的应用程序.服务器端代码是用asp.net编写的,服务器端正在维护Session. 我正在使用 Volley 调用Web服务,但问题是我无法在服务器端维护会话,据我所知我必须从标头中获取会话ID在此网站上搜索了配额,但我无法成功.我的问题是要在整个应用中维持会话状态.下面是我在android中调用网络服务的代码.

I am new to android and creating an app that communicate to server through web-service. Server side code is written in asp.net, and server side is maintaining Session. I am Using Volley to call the web-service but the problem i am not able to maintain the session on the server side, as far as i know i have to get the session id from the header i have searched allot on this website but i am not able to succeed.My Problem is to maintain the session throughout the app. Below is my code for calling web-service in android.

_Login_Webservice = new JsonObjectRequest(Request.Method.GET,
            _Login_Url, null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {

            // Log.e(Utils.tag, "response :" + response);
            User _User;
            button.setLoadingState(false);

            try {
                boolean Success = response.getBoolean("Success");

                if (Success == true) {

                    //my code


                } 

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("", "exception in Login User Login_Activity: ");
            Log.e("", "error is : " + error.toString());

        }
    }) {


        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            return params;
        }


    };
    //  _Login_Webservice.setRetryPolicy(new DefaultRetryPolicy(5000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    // Adding request to request queue
    Volley_Controller.getInstance().addToRequestQueue(_Login_Webservice,
            Config.VOLEY_TAG);

,标题中的响应如下.

{X-AspNet-Version=4.0.30319, X-Android-Selected-Transport=http/1.1, Vary=Accept-Encoding, Date=Thu, 07 Apr 2016 08:26:23 GMT, X-Android-Received-Millis=1460017311847, Set-Cookie=ASP.NET_SessionId=obzjppign5twglksjesm24wi; path=/; HttpOnly, Content-Type=text/JSON; charset=utf-8, X-Powered-By=ASP.NET, X-Android-Response-Source=NETWORK 200, Server=Microsoft-IIS/7.5, X-Android-Sent-Millis=1460017311678, Cache-Control=private}

我需要 ASP.NET_SessionId = 的值,并且还需要在标头中的其他Web服务中发送该值. 现在,我收到的回复低于

i need value of ASP.NET_SessionId= and also need to send this in other web-services in header. Right now the response that i am receiving is below

{"Success":false,"Info":"Object reference not set to an instance of an object."}

推荐答案

好,您必须使用Volley Library提供的 parseNetworkResponse 方法,然后才能从标题中的服务器获取响应./p>

well You have to use parseNetworkResponse method provided by Volley Library from there you can get the response coming form server in the header.

@Override
        protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
            try {

                String jsonString = new String(response.data,
                        HttpHeaderParser.parseCharset(response.headers));


                String header_response = String.valueOf(response.headers.values());
               int index1 = header_response.indexOf("ASP.NET_SessionId=");
                int index2 = header_response.indexOf("; path");

                Log.e(Utils.tag, "error is : " + index1 + "::" + index2);

                session_id = header_response.substring(index1, index2);

                // this is your session id put it in the variable and then you can use it any where you want to

                return Response.success(new JSONObject(jsonString),
                        HttpHeaderParser.parseCacheHeaders(response));

            } catch (UnsupportedEncodingException e) {
                return Response.error(new ParseError(e));
            } catch (JSONException je) {
                return Response.error(new ParseError(je));
            }
        }

当您将调用任何其他Web服务时,只需将该sessionId放在标题中,如下所示

when you will call any other web-service just put that sessionId in the header like below

@Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Cookie",SessionId);
            return headers;
        }

请记住,您必须发送 ASP.NET_SessionId = obzjppign5twglksjesm24wi 作为值和键 Cookie

remember you have to send ASP.NET_SessionId=obzjppign5twglksjesm24wi as a value and key Cookie

希望会有所帮助.

这篇关于使用凌空获取Android中的服务器端会话ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆