0

我正在将一些数据发布到 Yii 框架中的 PHP 服务器。登录工作正常。这意味着我的数据已正确发送到服务器。

但是登录后,我的后续请求被accessRules服务器上的方法拒绝,我得到登录页面作为响应。

这是 PHP 中的 accessRules 函数。工程师是管理员以外的普通用户。

public function accessRules()
    {
        return array(

            array('allow',  // allow all users to perform 'index' and 'view' actions
                'actions'=>array('index','view','AssignedUsers',),
                'roles'=>array('admin', 'engineering'),
                //'users'=>array('*'),
            ),
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions'=>array('create','update','userReport','userNewReport',),
                'roles'=>array('admin'),
                //'users'=>array('@'),
            ),
            array('allow', // allow admin user to perform 'admin' action
                'actions'=>array('admin'),
                'roles'=>array('admin', 'engineering'),
            ),
            array('allow', // allow admin user to perform 'delete' action
                'actions'=>array('delete'),
                'roles'=>array('admin', 'admin'),
            ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }

但我被服务器拒绝了。

这是 JAVA 请求

String content ="user=" + URLEncoder.encode(userId,encoding) +
"&apiKey=" + URLEncoder.encode(apiKey,encoding);  

此内容与 url 一起使用。

        connection = (HttpURLConnection)url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches (false);
        connection.setRequestProperty("Content-length",String.valueOf (content.length())); 
        connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

        DataOutputStream printout = new DataOutputStream (connection.getOutputStream ());

        System.out.println(url+",Content = "+content);

        printout.writeBytes (content);
        printout.flush ();
        printout.close ();
4

1 回答 1

2

发送 cookie。

当您登录时...将在服务器中创建一个会话,并且会话 ID将作为 cookie 在 http 响应标头上发送。

您必须从登录响应中捕获这些 cookie,并在后续请求中继续发送相同的内容。

我刚刚用谷歌搜索并找到了这个例子: 如何从响应中检索 cookie 以及如何在请求中发送它

于 2012-09-28T05:43:48.010 回答