java - Android Volley POST 请求返回错误 400(错误请求)

标签 java android azure iot

我正在尝试使用 Volley 库通过 JAVA android 应用程序向 Azure IoT 中心发出 POST 请求,但收到此错误: BasicNetwork.performRequest: https://elca-iot.azure-devices.net/devices/elca-main-device/messages/events?api-version=2016-02-03 出现意外响应代码 400 )

要访问 IoT 中心,我需要使用 SAS key ,并将其包含在请求的 header 中。 Android应用程序代码如下:

RequestQueue queue = Volley.newRequestQueue(c);
    String url = "https://elca-iot.azure-devices.net/devices/" + deviceId + "/messages/events?api-version=2016-02-03)";
    // Request a string response from the provided URL.
    StringRequest stringRequest = new StringRequest(Request.Method.POST,
            url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            onPostResponse(response, c, true);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("Error", error.getMessage());
            onPostResponse(error.getMessage(), c, false);
        }
    }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            //params.put("Content-Type", "application/xml");
            params.put("Authorization", "[My SAS Key]");
            params.put("Cache-Control", "no-cache");
            return params;
        }
        /*
        @Override
        public Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("api-version","2016-02-03");
            return params;
        }*/
        @Override
        public String getBodyContentType() {
            return "application/text; charset=UTF-8";
        }
        @Override
        public byte[] getBody(){
            return "On".getBytes();
        }
    };
    try {
        Log.d("request", String.valueOf(stringRequest.getMethod()==Request.Method.POST));
    } catch (Exception authFailureError) {
        authFailureError.printStackTrace();
    }
    // Add the request to the RequestQueue.
    queue.add(stringRequest);

我尝试使用 POSTMAN 执行相同的请求并且它有效,但我不知道为什么它不能与 Android 应用程序一起使用。这是使用 POSTMAN 发出的请求的 http 代码:

    POST /devices/elca-main-device/messages/events?api-version=2016-02-03 HTTP/1.1
Host: elca-iot.azure-devices.net
Authorization: [My SAS Key]
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache
Postman-Token: d0aa354a-f79c-e574-893a-c259232aa5cb

on

编辑:

Screenshot of POSTMAN

最佳答案

看来问题出在 map 上的值上。 []无法转义。

params.put("Authorization", "[My SAS Key]");

我所做的是,在创建参数时,我使用 UTF-8 对键和值进行编码。

params.put("Authorization", URLEncoder.encode("[My SAS Key]", "UTF-8"));

我也为这个问题苦苦挣扎了3天。这似乎是一种解决方法,但它对我有用。

关于java - Android Volley POST 请求返回错误 400(错误请求),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43773210/

相关文章:

java - ThreadLocal 与 Tomcat NIO Connector 一起使用是否安全

android - 在设置中保存 Intent 的任何解决方法?

c# - 如果更改 Azure DevOps 代理,为什么我的 MSbuild 命令路径无法访问?

angularjs - 如何在 Azure 网站中拥有测试和生产 AngularJS 应用程序?

azure - 有没有办法根据 Runbook/自动化脚本的标签删除资源组?

java.lang.ClassCastException : Cannot cast com. sun.jersey.core.impl.provider.entity.StringProvider 到 javax.ws.rs.ext.MessageBodyReader

java - Android - 在不打开应用程序的情况下在后台访问位置

java - 如何在 JavaFX 中的 ListView 中缩放标签

android - 安卓版 Chrome : Hide horizontal scrollbar when not scrolling

java - RxJava 将一个 Observable 拆分为两个 subObservable