c# 调用API 获得Token 发送命令到下位机

这篇博客介绍了如何在C#中调用API获取Token,并利用Token向下位机发送命令。通过使用System.Net和Newtonsoft.Json库,实现了HTTP请求与JSON解析,同时涉及了日志记录和Token的有效期管理。
摘要由CSDN通过智能技术生成

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Reflection;
using System.Net;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Security.Cryptography;

namespace user
{
public partial class FormIOT : Form
{
private static log4net.ILog log = log4net.LogManager.GetLogger(typeof(FormIOT));
string strToken = “”;
public static string access_token = “”;
public static DateTime token_last_time;

    public FormIOT()
    {
        InitializeComponent();
    }

    private void bt_sent_Click(object sender, EventArgs e)
    {
        if(!string.IsNullOrEmpty(this.tb_content.Text.Trim()))
        {                
            if (string.IsNullOrEmpty(access_token))
            {
                getToken();                    
            }

            if (!string.IsNullOrEmpty(access_token))
            {
                updateLedContent(this.tb_content.Text.Trim(), access_token);
            }
            else
            {
                MessageBox.Show("Token为空");
            }
        }
        else
        {
            MessageBox.Show("请输入内容!");
            this.tb_content.Focus();
        }
    }

    /// <summary>
    /// 获得Token
    /// </summary>
    public static void getToken()
    {
        string href = "http://localhost/api/v1/login";
        LoginInfo user = new LoginInfo();
        user.account = "admin";
        user.password = "admin";
        string json = JsonConvert.SerializeObject(user);
        string result = GetResponse(href, null, json);
        FirstResult<DataResult> uresult = DeserializeJsonToObject<FirstResult<DataResult>>(result);
        //成功
        if (uresult.status == 0)
        {
            access_token = uresult.data.token;
            token_last_time = DateTime.Now;
        }            
    }

    /// <summary>
    /// 获得Token
    /// </summary>
    public static void updateLedContent(string content,string token)
    {
        string url = "https://www.bekingai.com";
        string result = GetResponse(url, token, null);
        FirstResult<DataResult> uresult = DeserializeJsonToObject<FirstResult<DataResult>>(result);
        //成功
        if (uresult.status == 0)
        {
            //获取推送的url
            url = uresult.data.commandServerAddr;
            string deviceSN = "104238";
            string href = "/v1.0/tranion/" + deviceSN;
            CommandInfo command_content = new CommandInfo();
            byte[] b = GetByteArray(getLEDCommandString(content));
            转成 Base64 形式的 System.String  
            //command_content.data = content;
            string json = JsonConvert.SerializeObject(command_content);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值