首页 / C# / DingTalk机器人C#代码
DingTalk机器人C#代码
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了DingTalk机器人C#代码,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2646字,纯文字阅读大概需要4分钟。
内容图文
前面已经介绍了机器人的事情,今天直接贴一下代码。
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
原文:http://www.cnblogs.com/wangkun1993/p/6653792.html
内容总结
以上是互联网集市为您收集整理的DingTalk机器人C#代码全部内容,希望文章能够帮你解决DingTalk机器人C#代码所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。