zoukankan      html  css  js  c++  java
  • DingTalk机器人C#代码

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

    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 + "
    ";
                }
    
                //添加关闭相应
                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

  • 相关阅读:
    51keil编译器printf函数
    asp.net里登陆记住密码
    Asp.net GridView分页
    DataTable拆分分页
    ASP.NET MVC 窗体身份验证及角色权限治理示例
    asp.net获取IP地址
    Asp.net Ajax框架教程
    将页面的ViewState放在Session
    20个Jquery表单插件
    前端下载图片的N种方法
  • 原文地址:https://www.cnblogs.com/wangkun1993/p/6653792.html
Copyright © 2011-2022 走看看