最近研究微信公众平台,搭建了一个微信聊天机器人,调用小黄鸡的公众接口,实现在线和小黄鸡聊天的功能。

接口调用不是很麻烦,不过是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小黄鸡功能调用的更多相关文章

  1. 用java实现Simsimi小黄鸡接口

    package com.iask.webchat.chatmachine; import java.io.BufferedReader; import java.io.InputStream; imp ...

  2. 小黄鸡机器人和小I机器人的调用

    <?php    //---------------------------------聊天小机器人类---------------------------------------------- ...

  3. Simsimi 小黄鸡机器人最新无限制接口api simsimi机器人接口api 微信公众号

    一.什么是Simsimi? simsimi公司是提供智能服务,其中一个服务是simsimi聊天机器人服务,每天有超过百万的用户聊天,国内最大的搜索引擎——百度的产品siri使用的就是simsimi提供 ...

  4. PIGCMS 关闭聊天机器人(小黄鸡)

    无脑操作举例 1.找到 WeixinAction.class.php 文件,路径: 你的版本\PigCms\Lib\Action\Home 2.查询 function chat ,在 chat() 函 ...

  5. qt调用simsimi api实现小黄鸡

    项目地址:https://github.com/racaljk/xiaojianji 好吧我把它命名为小贱鸡.,下面说一说他的实现. 由于官方的simsimi api需要收费,免费试用版有各种限制,所 ...

  6. 微信小程序wx.showActionSheet调用客服信息功能

    微信小程序wx.showActionSheet调用客服消息功能 官方文档的代码: wx.showActionSheet({ itemList: ['A', 'B', 'C'], success (re ...

  7. 微信小程序ofo小黄车+thinkphp5.0打造全栈应用

    原文地址:https://www.imooc.com/article/20310 ofo至今还没有微信小程序(很费解),每次用ofo都得去支付宝,很不方便,我用微信用的比较多,无意间在简书上面看到某人 ...

  8. 我的代码真的没有bug,稍等,先试试小黄鸭调试法

    今天测试同学为了赶进度,加班去测试我的功能. 因为我的代码都写完了,也没有陪测的必要,所以就没去了~ 下午第一个问题提过来,根据经验,这个应该是测试的逻辑问题,最后他自己也发现了. 过了一会,提了第二 ...

  9. x01.os.5: DOS 功能调用

    DOS 功能调用(INT 21)-------------------------------AH = 0-2E 适用 DOS 1.0 以上版本AH = 2F-57 适用 DOS 2.0 以上版本AH ...

随机推荐

  1. 怒刷DP之 HDU 1069

    Monkey and Banana Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  2. 剑指Offer44 扑克牌的顺子

    /************************************************************************* > File Name: 44_Contin ...

  3. hdu 4081 最小生成树+树形dp

    思路:直接先求一下最小生成树,然后用树形dp来求最优值.也就是两遍dfs. #include<iostream> #include<algorithm> #include< ...

  4. QTREE2 spoj 913. Query on a tree II 经典的倍增思想

    QTREE2 经典的倍增思想 题目: 给出一棵树,求: 1.两点之间距离. 2.从节点x到节点y最短路径上第k个节点的编号. 分析: 第一问的话,随便以一个节点为根,求得其他节点到根的距离,然后对于每 ...

  5. Java计算机网络

    计算机网络: 分布在不同的地域的计算机通过外接设备(路由器),实现共享和数据传输网络编程:网络编程主要是用来解决计算机和计算机之间的数据传输网络编程:不需要基于HTML就可以达到数据之间的传输,比如Q ...

  6. JavaScript原型(链)学习笔记

    javascript是基于原型的一门脚本语言,那究竟原型是什么? 本文将从以下几个方面重点阐述原型 构造函数是什么? 构造函数和我们常见的Array String有什么关系? 原型的使用? __pro ...

  7. Table of Contents - Quartz Scheduler

    Getting Started Hello World Integration with Spring Quartz Scheduler Developer Guide Usage of JobDat ...

  8. iOS 获取设备的ip地址

    导入以下头文件 #include <ifaddrs.h> #include <arpa/inet.h>   通过下面方法即可获取ip地址+ (NSString *)getIpA ...

  9. CSS之text-stroke

    啧啧啧( ̄︶ ̄),国庆人太多,所以假期还没结束就提前几天回来了.今天也是挤火车赶回来的,被夹在门里好尴尬啊~~ 回家的这几天在外婆家招待过的好爽啊,又是鱼又是肉,馋的我都不想走了. 然而自己在家只能“ ...

  10. cocos2dx2.2.2弹出框的实现

    在上一篇文章中,我们利用CCEditBox实现了输入框功能,使我们在注册时可以输入用户名和密码.但是当用户名和密码的输入不符合规范时,我们应该怎样给与用户提示呢?下面我们就来介绍弹出框的实现方式. 我 ...