DingTalk機器人C#代碼

來源:互聯網
上載者:User

標籤:compress   response   sha   發送   www   datetime   .com   源碼   form   

        前面已經介紹了機器人的事情,今天直接貼一下代碼。

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Net;using System.IO;using System.IO.Compression;using System.Text.RegularExpressions;namespace 機器人{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            string paraUrlCoded = "{\"msgtype\":\"text\",\"text\":{\"content\":\"";            paraUrlCoded += textBox2.Text;            paraUrlCoded += "\"}}";            Post(paraUrlCoded);        }        private void button2_Click(object sender, EventArgs e)        {            string paraUrlCoded = "{\"msgtype\": \"link\", \"link\": {\"text\": \"我的部落格:歡迎光臨\", \"title\": \"推廣部落格啦,機器人開發人員\", \"picUrl\": \"\", \"messageUrl\": \"http://www.cnblogs.com/left69/\"}}";            Post(paraUrlCoded);        }        private void Post(string paraUrlCoded)        {            string url = textBox1.Text;            string strURL = url;            System.Net.HttpWebRequest request;            request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);            request.Method = "POST";                        //判斷是否必要性            request.ContentType = "application/json;charset=UTF-8";            //request.ContentType = "application/json;";            //添加cookie測試            //Uri uri = new Uri(url);            //Cookie cookie = new Cookie("Name", DateTime.Now.ToString()); // 設定key、value形式的Cookie            //CookieContainer cookies = new CookieContainer();            //cookies.Add(uri, cookie);            //request.CookieContainer = cookies;            //發送請求的另外形式            //request.Method = "POST";            //request.ContentType = "application/x-www-form-urlencoded";            byte[] payload;            payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);            request.ContentLength = payload.Length;            Stream writer = request.GetRequestStream();            writer.Write(payload, 0, payload.Length);            writer.Close();            System.Net.HttpWebResponse response;            response = (System.Net.HttpWebResponse)request.GetResponse();            System.IO.Stream s;            s = response.GetResponseStream();            string StrDate = "";            string strValue = "";            StreamReader Reader = new StreamReader(s, Encoding.UTF8);            while ((StrDate = Reader.ReadLine()) != null)            {                strValue += StrDate + "\r\n";            }            //添加關閉相應            Reader.Close();            response.Close();            //改變返回結果形式以看全部提示            label3.Text = strValue;           // MessageBox.Show(strValue);        }    }}

  

 程式源碼的位置在這兒。http://files.cnblogs.com/files/wangkun1993/%E6%9C%BA%E5%99%A8%E4%BA%BA.7z

  參考過的代碼有:

發送HTTP請求 - a-dou - 部落格園 http://www.cnblogs.com/a-dou/p/5151043.html

C# 發送Http協議 類比 Post Get請求 - kaikaichao - 部落格園 http://www.cnblogs.com/kaikaichao/p/5912806.html

DingTalk 機器人接入 自訂webhook - 藍辰進擊者 - 部落格園 http://www.cnblogs.com/left69/p/6483153.html

DingTalk機器人C#代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.