国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

C# Winfrom小黃雞功能調(diào)用

2019-11-17 01:46:23
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

C# Winfrom小黃雞功能調(diào)用

最近研究微信公眾平臺(tái),搭建了一個(gè)微信聊天機(jī)器人,調(diào)用小黃雞的公眾接口,實(shí)現(xiàn)在線和小黃雞聊天的功能。

接口調(diào)用不是很麻煩,不過(guò)是php版本,所以研究了一下C#的功能模塊,

Winfrom版

后臺(tái)界面代碼為

  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             //設(shè)定光標(biāo)所在位置 100             this.richTextBox1.SelectionStart = richTextBox1.TextLength-1;101             //滾動(dòng)到當(dāng)前光標(biāo)處 102             this.richTextBox1.ScrollToCaret();103         } 104 105     }106 }
View Code

應(yīng)用的類代碼:應(yīng)用接口用小黃雞那邊傳回來(lái)的是一個(gè)json形式的內(nèi)容,所以應(yīng)用到j(luò)son的解析問(wèn)題,我應(yīng)用的是網(wǎng)上通用的方法Newtonsoft.Json,可以從網(wǎng)上下載對(duì)應(yīng)的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 = "我們換個(gè)話題吧"; 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
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 承德县| 蒙阴县| 凉山| 封开县| 辛集市| 东城区| 淳化县| 上思县| 司法| 洛阳市| 东港市| 彭山县| 鹤岗市| 南澳县| 灵川县| 丽江市| 宽城| 千阳县| 信丰县| 通州市| 五原县| 临洮县| 东阳市| 库尔勒市| 奎屯市| 建平县| 义马市| 武邑县| 广饶县| 台湾省| 巨鹿县| 灵璧县| 新蔡县| 杨浦区| 苍南县| 灵石县| 清新县| 榆社县| 鱼台县| 边坝县| 阿鲁科尔沁旗|