Android截取码400

Android Volley code 400

我正在尝试 post 数据到网站。是登录操作。我检查表单数据隐藏标签等。在表单数据中有一个隐藏的输入_RequestVerificationToken。这就是为什么首先,我提出了解析 _RequestVerificationToken 和 headers 的请求。

StringRequest _StringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                       //Parsing Process
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                }
           )
            {
            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {

                headers=response.headers;
                return super.parseNetworkResponse(response);
            }
        };

毕竟我用参数发出了 post 请求并且 headers:

@Override
            public Map<String, String> getParams()
            {
                Map<String, String>  params = new HashMap<>();
                // the POST parameters:
                params.put("Password", password);
                params.put("RememberMe", "false");
                params.put("ReturnUrl", url_post);
                params.put("UserName",username);
                params.put(name,_RequestVerificationToken);
                return params;
            }
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                              return headers;
            }

我在 chrome 中使用 fiddler 扩展检查表单数据。我反复检查参数,但它返回代码 400。你能告诉我问题出在哪里吗?

Request.Method.GET更改为Request.Method.POST

  StringRequest _StringRequest = new StringRequest(Request.Method.POST, url,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                           //Parsing Process
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            error.printStackTrace();
                        }
                    }
               )
                {
                @Override
                protected Response<String> parseNetworkResponse(NetworkResponse response) {

                    headers=response.headers;
                    return super.parseNetworkResponse(response);
                }
            };

或者您可以使用此代码:-

StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {

                                returnValue = response;
                                try {
                                    parseFromUrl(response);
                                    System.out.println(returnValue);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                 Toast.makeText(app,error.toString(),Toast.LENGTH_SHORT).show();

                            }
                        }) {
                    @Override
                    protected Map<String, String> getParams() {
                        Map<String, String> params = new HashMap<String, String>();
                        params.put("Password", password);
                        params.put("RememberMe", "false");
                        params.put("ReturnUrl", url_post);
                        params.put("UserName",username);
                        params.put(name,_RequestVerificationToken);
                        return params;
                    }

                };

                RequestQueue requestQueue = Volley.newRequestQueue(app);
                requestQueue.add(stringRequest);

我用这个 class 解决了这个问题,我添加了这个:

 CookieManager cookieManager = new CookieManager(new PersistentCookieStore(this), CookiePolicy.ACCEPT_ORIGINAL_SERVER);
            CookieHandler.setDefault(cookieManager);

在将请求添加到队列之前