Topuino_Client_Windows/OnlineConnector.cs

38 lines
1.1 KiB
C#
Raw Normal View History

2022-06-10 18:51:48 +08:00
using System;
using System.Net.Http;
using System.Text;
using Newtonsoft.Json;
2022-07-01 17:18:11 +08:00
using System.Threading.Tasks;
2022-06-10 18:51:48 +08:00
using System.Collections.Generic;
namespace Topuino_Client_Windows
{
2022-06-30 10:22:12 +08:00
internal class OnlineConnector
2022-06-10 18:51:48 +08:00
{
private HttpClient client = new HttpClient();
2022-07-01 17:18:11 +08:00
internal async Task Post(Dictionary<string, string> data)
2022-06-10 18:51:48 +08:00
{
2022-07-01 17:18:11 +08:00
HttpContent content = new FormUrlEncodedContent(data);
HttpResponseMessage response = await client.PostAsync("https://iot.vvzero.com/topuino/putdata", content);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
OnlineConnectorResponse? respData = JsonConvert.DeserializeObject<OnlineConnectorResponse>(responseBody);
if (respData == null || respData.CODE != 0)
2022-06-10 18:51:48 +08:00
{
2022-07-01 17:18:11 +08:00
throw new Exception("网络连接异常");
2022-06-10 18:51:48 +08:00
}
}
2022-06-30 10:22:12 +08:00
internal void Dispose()
{
client.Dispose();
}
2022-06-10 18:51:48 +08:00
}
2022-06-30 10:22:12 +08:00
internal class OnlineConnectorResponse
2022-06-10 18:51:48 +08:00
{
2022-06-30 10:22:12 +08:00
public int CODE = 0;
2022-06-10 18:51:48 +08:00
}
}