【问题标题】:Get cookie from website using volley使用 volley 从网站获取 cookie
【发布时间】:2016-05-25 06:05:17
【问题描述】:

我正在尝试获取从网站发送的识别 cookie,为此我使用Volley library。

首先,是的,在过去的几天里,我已经广泛搜索了这个主题,并使用了我设法收集到这段代码的所有答案:

public class Login extends AppCompatActivity {
StringRequest stringRequest = null;
RequestQueue queue;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    queue = Volley.newRequestQueue(this);
    Button b = (Button) findViewById(R.id.bauth);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String url = "http://daviddurand.info/D228/pizza/?action=login&user=david";
            stringRequest = new StringRequest(Request.Method.GET, url,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            CookieManager manager = new CookieManager();
                            CookieHandler.setDefault(manager);
                            try {
                                Log.d("URI 1", manager.getCookieStore().get(new URI("http://daviddurand.info/D228/pizza/?action=login&user=david")).toString());
                            } catch (URISyntaxException e) {
                                e.printStackTrace();
                            }
                            Log.d("URIS", manager.getCookieStore().getURIs().toString());
                            Log.d("COOKIE", manager.getCookieStore().getCookies().toString());
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    //error
                }
            });
            // Add the request to the RequestQueue.
            queue.add(stringRequest);
        }
    });

}

}

它只返回空括号,所以我猜错了.. 首先,我在这里走对路了吗?以及我需要添加什么才能使其正常工作?

【问题讨论】:

  • 哎呀。我认为您必须创建一个自定义的Request 子类,该子类将返回服务器数据和cookie。我不相信 CookieManager 在我请求它们的确切时间拥有更新的 cookie。

标签: android cookies android-volley


【解决方案1】:

你不需要把这个:

CookieManager manager = new CookieManager();
CookieHandler.setDefault(manager);

在您的回复中。但在您开始拨打电话之前只有一次。

使用 CookieHandler HttpURLConnection,这显然是您使用的 http 堆栈 :),负责处理 cookie,因此您需要在 Volley 本身中处理它。

这样你就可以了

CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));

就在queue = Volley.newRequestQueue(this);之前

【讨论】:

    猜你喜欢
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-31
    • 1970-01-01
    • 2012-10-15
    • 1970-01-01
    • 2015-08-27
    相关资源
    最近更新 更多