在android中维护会话

时间:2014-01-13 13:15:39

标签: android asp.net session

我已经看到了该主题的各种答案并实现了它们但没有任何效果。我在这里发布我的实现代码。我需要知道代码无法正常工作

我有loginhttpPost方法,它将用户名,密码发送到asp服务器进行身份验证,并将响应中的会话ID存储在“cookieId”中

cookieId就像“erzt ......”

public String loginhttpPost(String username, String password)
        throws ConnectTimeoutException {
        try {
            // --------post


            HttpPost httppost = new HttpPost(loginurl);



            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();


            nameValuePairs.add(new BasicNameValuePair("__EVENTTARGET", ""));
            nameValuePairs.add(new BasicNameValuePair("__EVENTARGUMENT", ""));

            nameValuePairs.add(new BasicNameValuePair("__VIEWSTATE",viewstateValue));
            nameValuePairs.add(new BasicNameValuePair("__EVENTVALIDATION",eventvalidateValue));

            nameValuePairs.add(new BasicNameValuePair("ctl00$cphBody$txtUserName",username));
            nameValuePairs.add(new BasicNameValuePair("ctl00$cphBody$txtPassword", password));

            nameValuePairs.add(new BasicNameValuePair("ctl00$cphBody$btnLogin","Login"));


            httppost.addHeader("Content-type", "application/x-www-form-urlencoded");

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // perform post

            httpResponse = httpClient.execute(httppost);


            // get session id and store in cookieId

            cookieId =""; 
            List<Cookie> cookies = ((AbstractHttpClient) httpClient).getCookieStore().getCookies();
            for (Cookie cookie: cookies){
                if (cookie.getName().equals("ASP.NET_SessionId")){
                    cookieId = cookie.getValue();
                }
            }


            Log.d("COOKIE",cookieId);


            response = EntityUtils.toString(httpResponse.getEntity());

            Log.d("CHECK", "INSIDE METHOD");

        } 
        catch (ClientProtocolException e) {
        } 
        catch (IOException e) {
        }

    return response;
}

现在我使用defaultHttpGet方法通过发送带有请求的会话ID来对服务器执行后续的get请求

public String defaulthttpGet(String url) throws ConnectTimeoutException{

    try {


        HttpGet httpget = new HttpGet(url);
        httpget.addHeader("Cookie","ASP.NET_SessionId="+cookieId);

        httpResponse = httpClient.execute(httpget);
        response = EntityUtils.toString(httpResponse.getEntity());


    } 
    catch (ClientProtocolException e) {
    } 
    catch (IOException e) {
    }

     return response;
}

但我正在获取登录页面而不是预期页面

注意:我在后续请求中使用相同的HttpClient

0 个答案:

没有答案