Android排球码400

时间:2016-03-09 13:50:00

标签: android parameters header android-volley http-status-code-400

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

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);
            }
        };

毕竟我用参数和标题做了帖子请求:

@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.你能告诉我这是什么问题吗?

2 个答案:

答案 0 :(得分:0)

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);

答案 1 :(得分:0)

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

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

在向队列添加请求之前