在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
最近研究微信公众平台,搭建了一个微信聊天机器人,调用小黄鸡的公众接口,实现在线和小黄鸡聊天的功能。 接口调用不是很麻烦,不过是php版本,所以研究了一下C#的功能模块, Winfrom版 后台界面代码为 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.Threading; 10 11 namespace 小贱鸡 12 { 13 public partial class Form1 : Form 14 { 15 private static string cookie = null; 16 private string msg = ""; 17 private Thread th2 = null; 18 private Thread th3 = null; 19 private bool changed = false; 20 21 public Form1() 22 { 23 InitializeComponent(); 24 simsimi.SetAllowUnsafeHeaderParsing20(); 25 Thread th = new Thread(new ThreadStart(Cookie_Thread)); 26 th2 = new Thread(new ThreadStart(Hi_Thread)); 27 th3 = new Thread(new ThreadStart(PowerOn_Thread)); 28 th.Start(); 29 th2.Start(); 30 th3.Start(); 31 } 32 33 private void button1_Click(object sender, EventArgs e) 34 { 35 if (textBox1.Text != "") 36 { 37 changed = true; 38 msg = textBox1.Text; 39 textBox1.Text = ""; 40 richTextBox1.Text += String.Format("Me:{0}\n", msg); 41 changed = false; 42 } 43 } 44 45 private static void Cookie_Thread() 46 { 47 cookie = simsimi.GetCookie(); 48 } 49 private void PowerOn_Thread() 50 { 51 while (cookie == null) 52 { 53 this.Invoke(new Action(Update_PowerText), null); 54 Thread.Sleep(1500); 55 } 56 this.Invoke(new Action(Update_PowerFinalText), null); 57 } 58 private void Update_PowerText() 59 { 60 richTextBox1.Text += "正在开鸡中。。。\n"; 61 } 62 private void Update_PowerFinalText() 63 { 64 richTextBox1.Text += "唔,终于开鸡了。\n小贱鸡:你好,我是小贱鸡。o(∩_∩)o\n"; 65 } 66 private void Hi_Thread() 67 { 68 while (true) 69 { 70 while (changed) 71 { 72 this.Invoke(new Action(Update_Text), null); 73 break; 74 } 75 } 76 } 77 private void Update_Text() 78 { 79 richTextBox1.Text += String.Format("小贱鸡:{0}\n", simsimi.Hi_Simsimi(msg,cookie)); 80 } 81 82 private void textBox1_KeyUp(object sender, KeyEventArgs e) 83 { 84 if (e.KeyValue == 13) 85 { 86 if (textBox1.Text != "") 87 { 88 changed = true; 89 msg = textBox1.Text; 90 textBox1.Text = ""; 91 richTextBox1.Text += String.Format("Me:{0}\n", msg); 92 changed = false; 93 } 94 } 95 } 96 97 private void richTextBox1_TextChanged(object sender, EventArgs e) 98 { 99 //设定光标所在位置 100 this.richTextBox1.SelectionStart = richTextBox1.TextLength-1; 101 //滚动到当前光标处 102 this.richTextBox1.ScrollToCaret(); 103 } 104 105 } 106 } 应用的类代码:应用接口用小黄鸡那边传回来的是一个json形式的内容,所以应用到json的解析问题,我应用的是网上通用的方法Newtonsoft.Json,可以从网上下载对应的DLL 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Net; 6 using System.IO; 7 using System.IO.Compression; 8 using System.Reflection; 9 using Newtonsoft.Json; 10 using Newtonsoft.Json.Linq; 11 12 13 namespace 小贱鸡 14 { 15 class simsimi 16 { 17 /// <summary> 18 /// Cookie 19 /// </summary> 20 /// <returns></returns> 21 public static string GetCookie() 22 { 23 string Cookiesstr = null; 24 CookieCollection cookies = new CookieCollection(); 25 HttpWebRequest request = null; 26 request = (HttpWebRequest)WebRequest.Create("http://www.simsimi.com/talk.htm"); 27 request.CookieContainer = new CookieContainer(); 28 request.CookieContainer.Add(cookies); 29 //Get the response from the server and save the cookies from the request.. 30 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 31 Cookiesstr = request.CookieContainer.GetCookieHeader(request.RequestUri); 32 33 return Cookiesstr; 34 } 35 36 37 public static string Hi_Simsimi(string que, string cookies) 38 { 39 string ans = "我们换个话题吧"; 40 string url = String.Format("http://www.simsimi.com/func/reqN?lc=ch&ft=0.0&req={0}&fl=http%3A%2F%2Fwww.simsimi.com%2Ftalk.htm", que); 41 HttpWebRequest hi_request = null; 42 try 43 { 44 hi_request = (HttpWebRequest)WebRequest.Create(url); 45 hi_request.Method = "GET"; 46 hi_request.KeepAlive = true; 47 hi_request.ServicePoint.Expect100Continue = false; 48 49 hi_request.AllowWriteStreamBuffering = false; 50 //终端信息 51 hi_request.Accept = "application/json,text/javascript,*/*;q=0.01"; 52 hi_request.Headers.Add("Accept-Language", "zh-cn"); 53 hi_request.Headers.Add("Accept-Encoding", "gzip,deflate"); 54 hi_request.Headers.Add("Cookie", cookies + ";simsimi_uid=1;"); 55 hi_request.Referer = "http://www.simsimi.com/talk.htm"; 56 hi_request.UserAgent = "Mozilla/4.0(compatible;MSIE 7.0;Windows NT 6.1;Trident/5.0;SLCC2;.NET CLR 2.0.50727;.NET CLR 3.5.30729;.NET CLR 3.0.30729;.NET4.0C;.NET4.0E)"; 57 hi_request.ContentType = "application/json;charset=utf-8"; 58 59 hi_request.AllowAutoRedirect = false; 60 HttpWebResponse hi_response = (HttpWebResponse)hi_request.GetResponse(); 61 StreamReader sr = new StreamReader(hi_response.GetResponseStream(), Encoding.UTF8); 62 ans = sr.ReadToEnd(); 63 if (ans != "{}") 64 { 65 66 JObject jo = JObject.Parse(ans); 67 string[] ArrayList = jo.Properties().Select(item => item.Value.ToString()).ToArray(); 68 if (ArrayList[0] == "200") 69 { 70 ans = ArrayList[6]; 71 } 72 else 73 { 74 ans = "没有听懂,可以再说一遍吗?"; 75 } 76 } 77 hi_request.Abort(); 78 hi_response.Close(); 79 } 80 catch (System.Exception e) 81 { 82 83 Console.WriteLine(e.ToString()); 84 //return e.ToString(); 85 return "不好意思死鸡了⊙︿⊙重启下程序吧~"; 86 } 87 88 return ans; 89 } 90 91 92 93 public static bool SetAllowUnsafeHeaderParsing20() 94 { 95 //Get the assembly that contains the internal class 96 Assembly aNetAssembly = Assembly.GetAssembly(typeof(System.Net.Configuration.SettingsSection)); 97 if (aNetAssembly != null) 98 { 99 //Use the assembly in order to get the internal type for the internal class 100 Type aSettingsType = aNetAssembly.GetType("System.Net.Configuration.SettingsSectionInternal"); 101 if (aSettingsType != null) 102 { 103 //Use the internal static property to get an instance of the internal settings class. 104 //If the static instance isn't created allready the property will create it for us. 105 object anInstance = aSettingsType.InvokeMember("Section", 106 BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.NonPublic, null, null, new object[] { }); 107 108 if (anInstance != null) 109 { 110 //Locate the private bool field that tells the framework is unsafe header parsing should be allowed or not 111 FieldInfo aUseUnsafeHeaderParsing = aSettingsType.GetField("useUnsafeHeaderParsing", BindingFlags.NonPublic | BindingFlags.Instance); 112 if (aUseUnsafeHeaderParsing != null) 113 { 114 aUseUnsafeHeaderParsing.SetValue(anInstance, true); 115 return true; 116 } 117 } 118 } 119 } 120 return false; 121 } 122 } 123 }
|
请发表评论