using AsbCloudApp.Data; using System; using System.Collections.Generic; using System.Net; using System.Text; namespace SaubPanelOnlineSender { public class CloudDataSender { public CloudDataSender() { } public string OnlineUrl => "http://localhost:5000/api/telemetry/0A:00:27:00:11:12/data"; public void Send(DataSaubBaseDto data) { var request = MakeRequest(); using var streamWriter = request.GetRequestStream(); var requestBodyJson = MakeRequestBody(data); var dataBytes = Encoding.UTF8.GetBytes(requestBodyJson); streamWriter.Write(dataBytes, 0, dataBytes.Length); var response = request.GetResponse(); streamWriter.Close(); } private HttpWebRequest MakeRequest() { var request = WebRequest.CreateHttp(OnlineUrl); request.Method = "POST"; request.Timeout = 4900; request.ContentType = "application/json"; request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip; request.ServicePoint.Expect100Continue = false; return request; } private string MakeRequestBody(DataSaubBaseDto dataSaub) { var telemetry = new TelemetryDataDto { Date = DateTime.Now, DataSaub = new List { dataSaub }, HmiVersion = "3.1", UserName = "online sender", }; return Newtonsoft.Json.JsonConvert.SerializeObject(telemetry); } } }