我从Login WS调用中得到一个包含Session Token的cookie?

时间:2012-12-18 13:18:00

标签: android web-services post cookies web

它总是给我登录页面作为回复,因为它没有看到我登录。 这是我的POST方法的代码(我在登录和其他Web服务上都称它):

protected WebEntity sendPOST(WebServiceTask.InputParam param) throws Exception {
    WebEntity result = null;
    httppost = new HttpPost(param.url);

    if ((param.nameValuePairs != null) && !param.nameValuePairs.isEmpty()) {
        httppost.setEntity(new UrlEncodedFormEntity(param.nameValuePairs, "UTF-8"));
        LogService.log("sendPOST", "======params========" + param.nameValuePairs);
    }
    System.out.println("----blabla_session : "+blabla_session);
    if (blabla_session != null) {
        CookieStore cookieStore = httpclient.getCookieStore();
        BasicClientCookie cookie = new BasicClientCookie("blabla_session", blabla_session);
        cookie.setDomain("http://blbla.kz");
        cookieStore.addCookie(cookie);
        httpclient.setCookieStore(cookieStore);
        httppost.setHeader("Set-Cookie", blabla_session);


    }
    LogService.log("WEB-SERVER-CLIENT POST : ", httppost.getURI().toString());
    HttpResponse response = httpclient.execute(httppost);

    if (response.getStatusLine().getStatusCode() >= 400) {
        String maybeThereIsSomeExtraMessageInBody = "";
        try {
            maybeThereIsSomeExtraMessageInBody = (String) getResponseStream(response, response.getEntity(), String.class);
        } catch (Exception e) {
            e.printStackTrace();
            // DO NOTHING
        }
        return new WebEntity(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase() + "\n" + maybeThereIsSomeExtraMessageInBody);
    }
    Header cp = response.getFirstHeader("Set-Cookie");
    if(cp!=null){
        HeaderElement[] he = cp.getElements();
        for (int i = 0; i < he.length; i++) {
            if(he[i].getName().equalsIgnoreCase("blabla_session")){
                blabla_session = he[i].getValue();
                break;
            }
        }
    }
    HttpEntity responseEntity = response.getEntity();
    if (responseEntity != null) {
        if (result != null) {
            result.success = true;
        } else {
            result = new WebEntity(false);
        }
    }
    return result;
}

现在我做的第一个WS调用是Login WS,此时,blabla_session为null,因此部分代码不会启动。从第二个WS调用开始,blabla_session(它是静态的)是使用必要的session_code启动的,我从会话中获取。这就是为什么它进入代码的(blabla_session!= null)部分,它应该创建cookie,用WS调用发送

这是他们用于WebService的代码,我打电话:

r = requests.post('http://blabla.kz/api/login/',data={'username':'USERNAME','password':'123'})
rr = requests.get('http://blabla.kz/api/validate_coupon/bsdfsdfsd/', cookies=r.cookies)
rr.text

1 个答案:

答案 0 :(得分:0)

当我调用webservice调用时,我在nameValuePairs中有一个不应该存在的参数,这就是它崩溃的原因