最近研究微信公眾平臺,搭建了一個微信聊天機器人,調用小黃雞的公眾接口,實現在線和小黃雞聊天的功能。
接口調用不是很麻煩,不過是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 }View Code
應用的類代碼:應用接口用小黃雞那邊傳回來的是一個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/javascr
新聞熱點
疑難解答