C# Winfrom小黄鸡功能调用
最近研究微信公众平台,搭建了一个微信聊天机器人,调用小黄鸡的公众接口,实现在线和小黄鸡聊天的功能。
接口调用不是很麻烦,不过是php版本,所以研究了一下C#的功能模块,
Winfrom版
后台界面代码为
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading; namespace 小贱鸡
{
public partial class Form1 : Form
{
private static string cookie = null;
private string msg = "";
private Thread th2 = null;
private Thread th3 = null;
private bool changed = false; public Form1()
{
InitializeComponent();
simsimi.SetAllowUnsafeHeaderParsing20();
Thread th = new Thread(new ThreadStart(Cookie_Thread));
th2 = new Thread(new ThreadStart(Hi_Thread));
th3 = new Thread(new ThreadStart(PowerOn_Thread));
th.Start();
th2.Start();
th3.Start();
} private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
changed = true;
msg = textBox1.Text;
textBox1.Text = "";
richTextBox1.Text += String.Format("Me:{0}\n", msg);
changed = false;
}
} private static void Cookie_Thread()
{
cookie = simsimi.GetCookie();
}
private void PowerOn_Thread()
{
while (cookie == null)
{
this.Invoke(new Action(Update_PowerText), null);
Thread.Sleep();
}
this.Invoke(new Action(Update_PowerFinalText), null);
}
private void Update_PowerText()
{
richTextBox1.Text += "正在开鸡中。。。\n";
}
private void Update_PowerFinalText()
{
richTextBox1.Text += "唔,终于开鸡了。\n小贱鸡:你好,我是小贱鸡。o(∩_∩)o\n";
}
private void Hi_Thread()
{
while (true)
{
while (changed)
{
this.Invoke(new Action(Update_Text), null);
break;
}
}
}
private void Update_Text()
{
richTextBox1.Text += String.Format("小贱鸡:{0}\n", simsimi.Hi_Simsimi(msg,cookie));
} private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyValue == )
{
if (textBox1.Text != "")
{
changed = true;
msg = textBox1.Text;
textBox1.Text = "";
richTextBox1.Text += String.Format("Me:{0}\n", msg);
changed = false;
}
}
} private void richTextBox1_TextChanged(object sender, EventArgs e)
{
//设定光标所在位置
this.richTextBox1.SelectionStart = richTextBox1.TextLength-;
//滚动到当前光标处
this.richTextBox1.ScrollToCaret();
} }
}
应用的类代码:应用接口用小黄鸡那边传回来的是一个json形式的内容,所以应用到json的解析问题,我应用的是网上通用的方法Newtonsoft.Json,可以从网上下载对应的DLL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.IO.Compression;
using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq; namespace 小贱鸡
{
class simsimi
{
/// <summary>
/// Cookie
/// </summary>
/// <returns></returns>
public static string GetCookie()
{
string Cookiesstr = null;
CookieCollection cookies = new CookieCollection();
HttpWebRequest request = null;
request = (HttpWebRequest)WebRequest.Create("http://www.simsimi.com/talk.htm");
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
//Get the response from the server and save the cookies from the request..
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Cookiesstr = request.CookieContainer.GetCookieHeader(request.RequestUri); return Cookiesstr;
} public static string Hi_Simsimi(string que, string cookies)
{
string ans = "我们换个话题吧";
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);
HttpWebRequest hi_request = null;
try
{
hi_request = (HttpWebRequest)WebRequest.Create(url);
hi_request.Method = "GET";
hi_request.KeepAlive = true;
hi_request.ServicePoint.Expect100Continue = false; hi_request.AllowWriteStreamBuffering = false;
//终端信息
hi_request.Accept = "application/json,text/javascript,*/*;q=0.01";
hi_request.Headers.Add("Accept-Language", "zh-cn");
hi_request.Headers.Add("Accept-Encoding", "gzip,deflate");
hi_request.Headers.Add("Cookie", cookies + ";simsimi_uid=1;");
hi_request.Referer = "http://www.simsimi.com/talk.htm";
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)";
hi_request.ContentType = "application/json;charset=utf-8"; hi_request.AllowAutoRedirect = false;
HttpWebResponse hi_response = (HttpWebResponse)hi_request.GetResponse();
StreamReader sr = new StreamReader(hi_response.GetResponseStream(), Encoding.UTF8);
ans = sr.ReadToEnd();
if (ans != "{}")
{ JObject jo = JObject.Parse(ans);
string[] ArrayList = jo.Properties().Select(item => item.Value.ToString()).ToArray();
if (ArrayList[] == "")
{
ans = ArrayList[];
}
else
{
ans = "没有听懂,可以再说一遍吗?";
}
}
hi_request.Abort();
hi_response.Close();
}
catch (System.Exception e)
{ Console.WriteLine(e.ToString());
//return e.ToString();
return "不好意思死鸡了⊙︿⊙重启下程序吧~";
} return ans;
} public static bool SetAllowUnsafeHeaderParsing20()
{
//Get the assembly that contains the internal class
Assembly aNetAssembly = Assembly.GetAssembly(typeof(System.Net.Configuration.SettingsSection));
if (aNetAssembly != null)
{
//Use the assembly in order to get the internal type for the internal class
Type aSettingsType = aNetAssembly.GetType("System.Net.Configuration.SettingsSectionInternal");
if (aSettingsType != null)
{
//Use the internal static property to get an instance of the internal settings class.
//If the static instance isn't created allready the property will create it for us.
object anInstance = aSettingsType.InvokeMember("Section",
BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.NonPublic, null, null, new object[] { }); if (anInstance != null)
{
//Locate the private bool field that tells the framework is unsafe header parsing should be allowed or not
FieldInfo aUseUnsafeHeaderParsing = aSettingsType.GetField("useUnsafeHeaderParsing", BindingFlags.NonPublic | BindingFlags.Instance);
if (aUseUnsafeHeaderParsing != null)
{
aUseUnsafeHeaderParsing.SetValue(anInstance, true);
return true;
}
}
}
}
return false;
}
}
}
C# Winfrom小黄鸡功能调用的更多相关文章
- 用java实现Simsimi小黄鸡接口
package com.iask.webchat.chatmachine; import java.io.BufferedReader; import java.io.InputStream; imp ...
- 小黄鸡机器人和小I机器人的调用
<?php //---------------------------------聊天小机器人类---------------------------------------------- ...
- Simsimi 小黄鸡机器人最新无限制接口api simsimi机器人接口api 微信公众号
一.什么是Simsimi? simsimi公司是提供智能服务,其中一个服务是simsimi聊天机器人服务,每天有超过百万的用户聊天,国内最大的搜索引擎——百度的产品siri使用的就是simsimi提供 ...
- PIGCMS 关闭聊天机器人(小黄鸡)
无脑操作举例 1.找到 WeixinAction.class.php 文件,路径: 你的版本\PigCms\Lib\Action\Home 2.查询 function chat ,在 chat() 函 ...
- qt调用simsimi api实现小黄鸡
项目地址:https://github.com/racaljk/xiaojianji 好吧我把它命名为小贱鸡.,下面说一说他的实现. 由于官方的simsimi api需要收费,免费试用版有各种限制,所 ...
- 微信小程序wx.showActionSheet调用客服信息功能
微信小程序wx.showActionSheet调用客服消息功能 官方文档的代码: wx.showActionSheet({ itemList: ['A', 'B', 'C'], success (re ...
- 微信小程序ofo小黄车+thinkphp5.0打造全栈应用
原文地址:https://www.imooc.com/article/20310 ofo至今还没有微信小程序(很费解),每次用ofo都得去支付宝,很不方便,我用微信用的比较多,无意间在简书上面看到某人 ...
- 我的代码真的没有bug,稍等,先试试小黄鸭调试法
今天测试同学为了赶进度,加班去测试我的功能. 因为我的代码都写完了,也没有陪测的必要,所以就没去了~ 下午第一个问题提过来,根据经验,这个应该是测试的逻辑问题,最后他自己也发现了. 过了一会,提了第二 ...
- x01.os.5: DOS 功能调用
DOS 功能调用(INT 21)-------------------------------AH = 0-2E 适用 DOS 1.0 以上版本AH = 2F-57 适用 DOS 2.0 以上版本AH ...
随机推荐
- Android中“再按一次返回键退出程序”实现
private long exitTime = 0; @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyC ...
- 关于Django中的表单验证
ModelForm 和 普通的Form 都可以做表单验证 对于ModelForm如果只是想验证其中一部分model中的field,可以指定:内部类Meta的fields元素: fields = ('x ...
- spring + Quartz定时任务配置
<bean id="exportBatchFileTask" class="com.ydcn.pts.task.ExportBatchFileTask"& ...
- jquery plugins —— datatables ajax post更新数据
通过下面语句,可以定义datatables插件通过ajax post方法从服务器段获取JSON格式的数据. 错误写法(这样写再执行ajax.reload()方法时,ID参数还是初始时,不会更新): v ...
- hg(Mercurial)版本库迁移到git版本库
这几天没事干净搞迁移了,迁移完MVC又迁移版本库,还把工作电脑迁移了一下,开始用Win8.1了.这个迁移主要是因为实在不想在工作电脑上又装git又装hg了,点个右键出来一大堆菜单,况且现在git已经成 ...
- 【转】ConcurrentHashMap完全解析(JDK6/7、JDK8)
转自http://my.oschina.net/hosee/blog/675884 并发编程实践中,ConcurrentHashMap是一个经常被使用的数据结构,相比于Hashtable以及Colle ...
- ReactNative学习-webView
在软件内部打开一个网页--不喜欢它没有办法返回,还需要再添加返回按钮== import React from 'react'; import { AppRegistry, Component, Scr ...
- hdu 4494 最小费用流
思路:这题我在下午重现的时候就用的费用流做,可是各种悲催的超时,只是我一开始的那种建图方式多了一个二分查找. 戏剧性的是,求距离的返回值写成int型了,CodeBlock编译器又没有警告,然后就WA啊 ...
- [wordpress]根据自定义字段排序并根据自定义字段查询
Wordpress中,根据根据自定义字段排序和查询是通过WP_Query()方法 如根据 一个自定义的sort的数字字段从小到大进行排序 $args = array( 'post_type' => ...
- 1172 Hankson 的趣味题[数论]
1172 Hankson 的趣味题 2009年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Descrip ...