我构建了一个小的laravel应用程序,它只有一个表单并将数据保存到数据库(phpmyadmin)。现在我试图在我的android应用程序中显示这些数据,我试图通过ip获取这些数据。
主要活动如下:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void onConnect(View view) {
        new Thread(){
            public void run(){
                HttpClient myClient = new DefaultHttpClient();
                HttpPost post = new HttpPost("http://192.168.145.33:8000/gaIndex");
                try {
                    List<NameValuePair> myArgs = new ArrayList<NameValuePair>();
                    myArgs.add(new BasicNameValuePair("email", "[email protected]"));
                    myArgs.add(new BasicNameValuePair("password", "test"));
                    post.setEntity(new UrlEncodedFormEntity(myArgs));
                    HttpResponse myResponse = myClient.execute(post);
                    BufferedReader br = new BufferedReader( new InputStreamReader(myResponse.getEntity().getContent()));
                    String line = "";
                    while ((line = br.readLine()) != null)
                    {
                        Log.d("mytag", line);

                    }
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }
}

如果我启动应用程序并按下按钮连接控制台,给我一堆HTML…
我在控制台输出中也得到了这个:
令牌不匹配异常
在verifycsrftoken.php中
第53行:
有人知道我做错了什么吗?

最佳答案

laravel有一个verifycsrf中间件,它应用于所有传入的请求,以保护应用程序不受Cross Site Request Forgery
通过将路由添加到verifycsrftoken类中的$except数组中,可以使路由绕过此项:
在app/http/middleware/verifycsrftoken.php中

<?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;

class VerifyCsrfToken extends BaseVerifier
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        'gaIndex'
    ];
}

10-05 17:54