为什么 Volley String 请求不能在 x-www-form-urlencoded 中使用 POST 方法发送 Body 参数?

纳杰梅

首先,我应该说我在这里阅读了几个类似的故事,但没有一个解决了我收到400错误代码的问题。我想使用 Post 方法将一些登录信息发送到 android studio 3.3.1 和 volley 库中的服务器。我在 getBody 中将参数作为键值发送,并在 getBodyContentType() 中设置内容类型。服务器只获取 xxx-form-urlencoded 格式的参数。我使用邮递员作为代理来查看正在发送的内容...邮递员以原始格式而不是 xxx-form-urlencoded 格式接收参数,我认为这就是为什么服务器无法获取登录参数并且我收到 400 错误代码(BasicNetwork .performRequest: Unexpected response code 400)- btw 服务器是用 asp.net 和 IIS webserver 编写的。

 private  void PostParams()
        {
             Boolean is_valid_user;
            try {

                String URL = "http://192.168.43.22:5555/AOBServices/login";
                JSONObject jsonBody = new JSONObject();
                jsonBody.put("my_very_special_key","Love");
                jsonBody.put("username", "amir");
                jsonBody.put("password", "1234");
                jsonBody.put("grant_type", "password");
                final String requestBody = jsonBody.toString();
                final StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        Log.i("VOLLEY", response);
                        Toast.makeText(LoginActivity.this, "SUCCESSSSSSSSSSSSS \n response: "+response, Toast.LENGTH_SHORT).show();
                        //update shared prefs.....
                        myResponse=response;
                        shprefs= new sharedPrefsClass(getApplicationContext());
                        shprefs.setDefaults("USER_ID", mEmail);
                        shprefs.setDefaults("IS_SIGNEDIN", true);
                        startActivity(new Intent(LoginActivity.this,MainActivity.class));
                        finish();
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                        // As of f605da3 the following should work
                        NetworkResponse response = error.networkResponse;
                        if (error instanceof ServerError && response != null) {
                            try {
                                String res = new String(response.data,
                                        HttpHeaderParser.parseCharset(response.headers, "utf-8"));

                            } catch (UnsupportedEncodingException e1) {
                                // Couldn't properly decode data to string
                                e1.printStackTrace();
                            }

                        }
                    }


                }) {
                    @Override
                    public String getBodyContentType() {
                        return "application/x-www-form-urlencoded; charset=UTF-8";

                    }
                    @Override
                    protected String getParamsEncoding() {
                        return "utf-8";
                    }


                    @Override
                    public byte[] getBody() throws AuthFailureError {
                        try {
                                 return requestBody == null ? null : requestBody.getBytes("utf-8");

                        } catch (UnsupportedEncodingException uee) {
                            VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
                                 return null;
                        }
                    }

                 @Override
                    protected Response<String> parseNetworkResponse(NetworkResponse response) {
                        String responseString = "";
                        if (response != null) {
                            responseString = String.valueOf(response.statusCode);
                            // can get more details such as response.headers
                        }
                        return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
                    }
                };

                requestQueue.add(stringRequest);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
纳杰梅

对于那些面临类似问题的人:在对 Volley 感到失望之后,我通过 实现了网络内容HttpUrlConnection,并发现了问题所在:我以 json 对象格式发送参数,即 {key:value,...} 而服务器预期 key:value&key :value&... 所以返回到 Volley 并像上面那样制作参数字符串并在 中使用它getBody(),解决了我的问题。:)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Swift 4以x-www-form-urlencoded发送POST请求

当使用字符串作为请求正文时,为什么Axios使用Content-Type application / x-www-form-urlencoded发送我的POST请求?

Swift-如何使用“ x-www-form-urlencoded”内容类型发送POST请求

使用 Jitterbit POST 应用程序/x-www-form-urlencoded Body 到 REST API

PowerShell - Slack API - 房间历史 - 带有 x-www-form-urlencoded 参数的 Post 方法

如何使用带有 loopj-async-http 库的 x-www-form-urlencoded 主体发送 post 请求

如何使用 webclient 发布 body x-www-form-urlencoded?

如何发送内容类型为“ application / x-www-form-urlencoded”的POST请求

如何在webclient的post请求中发送x-www-form-urlencoded?

Javascript:从x-www-form-urlencoded请求获取参数值

PYTHON:requests.post()如何发送编码为application / x-www-form-urlencoded的request_body

使用POST + application / x-www-form-urlencoded发送敏感数据

如何在内容类型 application/x-www-form-urlencoded 中使用自定义对象测试 Post 请求?

如何使用节点请求发送承载令牌和x-www-form-urlencoded数据

Python请求模块发送JSON字符串而不是x-www-form-urlencoded参数字符串

改造+ POST方法+ www-form-urlencoded

使用 Volley 在 POST 请求中发送参数

如何在Go中使用application / x-www-form-urlencoded内容类型执行GET请求?

如何在Rails中使用x-www-form-urlencoded

Spring MVC中内容类型应用程序/ x-www-form-urlencoded的请求参数的顺序

带有application / x-www-form-urlencoded格式的Node.js Axios POST请求?

尝试在 wpf 桌面应用程序中使用 application/x-www-form-urlencoded 进行 API POST

无法使用带有x-www-form-urlencoded参数的JSOUP登录网站

如何使用x-www-form-urlencoded强制Angular2进行POST

使用带有 x-www-form-urlencoded 的 HttpClient 的服务器端 Blazor Post

Akka HTTP 如何使用 Content-Type application/x-www-form-urlencoded POST singleRequest

POST使用cURL和PHP中的x-www-form-urlencoded返回访问被拒绝

如何发送POST请求与X WWW的形式,进行了urlencoded体

解析JSON时出现问题-Ajax请求作为application / x-www-form-urlencoded发送