private static HttpWebRequest CreateRequest(DeviceAuthorizationInfo device, long sentDate, byte[] message) { var url = $"https://akka-iot.azure-devices.net/devices/{device.DeviceId}/messages/events?api-version=2016-11-14"; var request = (HttpWebRequest)WebRequest.Create(url); request.Headers.Add("Authorization", device.SharedAccessKeySignature); request.Headers.Add("iothub-app-SentAt", sentDate.ToString()); request.Headers.Add("iothub-app-PayloadVersion", "1"); request.Method = "POST"; request.ContentType = "application/json"; request.ContentLength = message.Length; using (var stream = request.GetRequestStream()) { stream.Write(message, 0, message.Length); } return(request); }
private async Task SendMessage() { try { var random = new Random(); var message = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new { temperature = random.Next(50, 80), humidity = random.Next(40, 70) })); var now = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds; var request = CreateRequest(DeviceAuthorizationInfo.For(_deviceId), now, message); using (var response = (HttpWebResponse)await request.GetResponseAsync()) { Console.WriteLine($"Message sent from {_deviceId}, result: {response.StatusCode}"); } } catch (Exception e) { Console.WriteLine(e); } }