首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >DingTalk机器人C#代码

DingTalk机器人C#代码

作者头像
@坤的
发布2018-06-04 12:38:41
8740
发布2018-06-04 12:38:41
举报
文章被收录于专栏:*坤的Blog*坤的Blog

        前面已经介绍了机器人的事情,今天直接贴一下代码。

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

钉钉 机器人接入 自定义webhook - 蓝辰进击者 - 博客园 http://www.cnblogs.com/left69/p/6483153.html

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-03-31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档