【发布时间】:2017-06-19 17:03:15
【问题描述】:
我正在开发一个将数据发送到 Azure 事件中心的应用程序。这和这里的博文类似:http://sreesharp.com/send-events-from-android-app-to-microsoft-azure-event-hubs/
不过,我更新了连接代码以使用 OkHttp:
public void sendMessageOkHttp(String dataPacket, String connectionString, String sasKey){
// Instantiate the OkHttp Client
OkHttpClient client = new OkHttpClient();
// Create the body of the message to be send
RequestBody formBody = new FormBody.Builder()
.add("message", dataPacket)
.build();
// Now create the request and post it
Request request = new Request.Builder()
.header("Authorization", sasKey)
.url(connectionString)
.post(formBody)
.build();
Log.i(TAG,"about to send message");
// Now try to send the message
try {
Log.i(TAG,"sending message....");
Response response = client.newCall(request).execute();
Log.i(TAG,"message sent");
Log.i("Azure Response",String.valueOf(response.message()));
// Do something with the response.
} catch (IOException e) {
e.printStackTrace();
}
}
但是,这会从事件中心“未经授权”返回响应。我使用的 sas 密钥用于我创建的具有发送和侦听权限的共享访问策略。它是主键。
我在这里做错了什么?在这种情况下,Azure 文档并没有真正帮助我,因为它专注于使用与 Android 不兼容的 Azure Java 库(即它们需要 Java 1.8)
【问题讨论】:
标签: java android okhttp azure-eventhub