c# 调用接口 post 获取token, json格式

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;
using System.Text;

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://www.bekingai.com/login";
        LoginInfo user = new LoginInfo();
        user.account = "feek";
        //MD5加密后的密码
        user.password = MD5Encrypt32("13434r");
        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_t
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现C#中的HTTPS接口调用,您可以遵循以下步骤: 1. 在项目中添加System.Net命名空间,这个命名空间提供了HTTP和HTTPS协议的类库。 2. 创建一个HttpWebRequest对象,用于请求HTTPS的URL地址。您可以使用WebRequest.Create方法来创建HttpWebRequest对象。 3. 配置HttpWebRequest对象的属性。例如,您需要设置请求的URL地址、请求方法、请求头等信息。您可以使用HttpWebRequest对象提供的相关属性来进行设置。 4. 向服务器发送请求并接收响应。您可以使用HttpWebRequest对象的GetResponse或者GetResponseStream方法来发送请求并接收响应。GetResponse方法将会阻塞当前线程,直到服务器返回响应。 5. 处理响应数据。您可以使用HttpWebResponse对象来获取响应数据,并将其转换为需要的格式(例如JSON或XML)。 下面是一个简单的C#代码示例,用于实现HTTPS接口调用: ```csharp using System; using System.IO; using System.Net; using System.Text; public class HttpsExample { public static void Main(string[] args) { // 创建HttpWebRequest对象 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://example.com/api"); // 配置HttpWebRequest对象的属性 request.Method = "POST"; request.ContentType = "application/json"; request.Headers.Add("Authorization", "Bearer your_access_token"); // 发送请求并接收响应 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); // 处理响应数据 StreamReader reader = new StreamReader(responseStream, Encoding.UTF8); string responseText = reader.ReadToEnd(); Console.WriteLine(responseText); // 关闭响应流和HttpWebResponse对象 reader.Close(); responseStream.Close(); response.Close(); } } ``` 在实际开发中,您可能需要根据自己的需求进行更加复杂的HTTPS接口调用。但是,以上的步骤是基本的步骤,您可以根据这些步骤进行开发。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值