日常生活的交流与学习

首页 新随笔 联系 管理

Tornado.Send\Program.cs

new TornadoSend().SendContentFromFive("这是测试消息");

Tornado.Send\Tornado.cs

using System.Net;
using System.Text;

public class TornadoSend
{
    private readonly string url5g = "";

    private readonly Dictionary<string, string> data = new()
    {
        { "userID", "xxx" },
        { "jobCat", "xxx" },
        { "scene", "xxx" },
        { "tag", "xxx" },
        { "param", "" }
    };

    private readonly Dictionary<string, string> header = new()
    {
        { "Content-Type", "application/x-www-form-urlencoded;charset=UTF-8" },
    };

    public bool SendContentFromFive(string content)
    {
        data.Add("desc", content);
#pragma warning disable SYSLIB0014 // Type or member is obsolete
        var request = (HttpWebRequest)WebRequest.Create(url5g);
#pragma warning restore SYSLIB0014 // Type or member is obsolete
        request.Method = "POST";
        foreach (var item in header)
        {
            request.Headers.Add(item.Key, item.Value);
        }
        var postData = GetPostData(data);
        request.ContentLength = postData.Length;
        using (var stream = request.GetRequestStream())
        {
            stream.Write(postData, 0, postData.Length);
        }

        try
        {
            var response = (HttpWebResponse)request.GetResponse();
            var res = GetResponseContent(response);
            if (res != "0000")
            {
                Console.WriteLine("龙卷风发送失败-->");
                Console.WriteLine(res);
                return false;
            }

            Console.WriteLine("龙卷风发送成功!!!");
            return true;
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            return false;
        }
    }

    private static byte[] GetPostData(Dictionary<string, string> data)
    {
        var sb = new StringBuilder();
        foreach (var item in data)
        {
            sb.Append($"{item.Key}={item.Value}&");
        }
        return Encoding.UTF8.GetBytes(sb.ToString().TrimEnd('&'));
    }

    private static string GetResponseContent(HttpWebResponse response)
    {
        using (var stream = response.GetResponseStream())
        {
            using (var reader = new StreamReader(stream))
            {
                return reader.ReadToEnd();
            }
        }
    }
}

posted on 2024-02-29 16:22  lazycookie  阅读(8)  评论(0编辑  收藏  举报