微信公众号开发客服接口实例代码

2018-10-15 17:58

阅读:478

最近,开发微信公众号,负责开发客服功能,这里简单记录下:

Kf_account.cs代码:

SysVisitor.cs代码:

class SysVisitor { private static SysVisitor visit = null; public static SysVisitor Current { get { if (visit == null) visit = new SysVisitor(); return visit; } } /// <summary> /// 获取access_token /// </summary> ///<param name=appid>appid ///<param name=secret>appsecret /// <returns></returns> public string Get_Access_token(string appid, string appsecret) { string secondappid = INIFile.ContentValue(weixin, secondappid); if (appid.ToLower() == secondappid.ToLower()) { string ls_time = INIFile.ContentValue(weixin, gettime); Decimal ldt; try { ldt = Convert.ToDecimal(ls_time); if (Convert.ToDecimal(DateTime.Now.ToString(yyyyMMddHHmmss)) - ldt < 7100)//每两个小时刷新一次 { return INIFile.ContentValue(weixin, access_token); } } catch { } } string ls_appid = appid.Replace( , ); string ls_secret = appsecret.Replace( , ); string access_token = ; string url = string.Format(获取access_token /// </summary> public string Get_Access_token() { string ls_appid = INIFile.ContentValue(weixin, Appid); string ls_secret = INIFile.ContentValue(weixin, AppSecret); return Get_Access_token(ls_appid, ls_secret); } /// <summary> /// Get方法请求url并接收返回消息 /// </summary> ///<param name=strUrl>Url地址 /// <returns></returns> public string GetPageInfo(string url) { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string ret = string.Empty; Stream s; string StrDate = ; string strValue = ; if (response.StatusCode == HttpStatusCode.OK) { s = response.GetResponseStream(); ////在这儿处理返回的文本 StreamReader Reader = new StreamReader(s, Encoding.UTF8); while ((StrDate = Reader.ReadLine()) != null) { strValue += StrDate + \r\n; } //strValue = Reader.ReadToEnd(); } return strValue; } /// <summary> /// Post方法 /// </summary> ///<param name=posturl>URL ///<param name=postData>Post数据 /// <returns></returns> public string PostPage(string posturl, string postData) { Stream outstream = null; Stream instream = null; StreamReader sr = null; HttpWebResponse response = null; HttpWebRequest request = null; Encoding encoding = Encoding.UTF8; byte[] data = encoding.GetBytes(postData); // 准备请求... try { // 设置参数hod = POST; request.ContentType = application/x-发送请求并获取相应回应数据 response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 instream = response.GetResponseStream(); sr = new StreamReader(instream, encoding); //返回结果网页(html)代码 string content = sr.ReadToEnd(); string err = string.Empty; return content; } catch (Exception ex) { string err = ex.Message; return string.Empty; } } /// <summary> /// 格式化字符串 /// </summary> ///<param name=str> /// <returns></returns> public string GetFormatStr(string str) { if ( == str) return ; else { str = str.Trim(); str = str.Replace(, ); str = str.Replace(〈, <); str = str.Replace(〉, >); str = str.Replace(,, ,); return str; } } string ls_username = ; /// <summary> /// 用户名 /// </summary> public string Wx_username { get { return ls_username; } set { ls_username = value; } } string ls_openid = ; /// <summary> /// Openid /// </summary> public string Wx_openid { get { return ls_openid; } set { ls_openid = value; } } }

INIFile.cs代码:

class INIFile { ///// <summary> ///// 设置INI文件参数 ///// </summary> /////<param name=section>INI文件中的段落 /////<param name=key>INI文件中的关键字 /////<param name=val>INI文件中关键字的数值 /////<param name=filePath>INI文件的完整的路径和名称 ///// <returns></returns> //[DllImport(kernel32)] //private static extern long WritePrivateProfileString( // string section, string key, string val, string filePath); ///// <summary> ///// 获取INI文件参数 ///// </summary> /////<param name=section>INI文件中的段落名称 /////<param name=key>INI文件中的关键字 /////<param name=def>无法读取时候时候的缺省数值 /////<param name=retVal>读取数值 /////<param name=size>数值的大小 /////<param name=filePath>INI文件的完整路径和名称 //[DllImport(kernel32)] //private static extern int GetPrivateProfileString( // string section, string key, string def, StringBuilder retVal, int size, string filePath); //static string gs_FileName = System.AppDomain.CurrentDomain.BaseDirectory + Config.ini; ///// <summary> ///// 获取INI文件参数 ///// </summary> /////<param name=as_section>INI文件中的段落名称 /////<param name=as_key>INI文件中的关键字 /////<param name=as_FileName>INI文件的完整路径和名称 //public static string GetINIString(string as_section, string as_key, string as_FileName) //{ // StringBuilder temp = new StringBuilder(255); // int i = GetPrivateProfileString(as_section, as_key, , temp, 255, as_FileName); // return temp.ToString(); //} ///// <summary> ///// 获取INI文件参数 ///// </summary> /////<param name=as_section>INI文件中的段落名称 /////<param name=as_key>INI文件中的关键字 /////<param name=as_FileName>INI文件的完整路径和名称 //public static string GetINIString(string as_section, string as_key) //{ // return GetINIString(as_section, as_key, gs_FileName); //} ///// <summary> ///// 设置INI文件参数 ///// </summary> /////<param name=as_section>INI文件中的段落 /////<param name=as_key>INI文件中的关键字 /////<param name=as_Value>INI文件中关键字的数值 /////<param name=as_FileName>INI文件的完整路径和名称 //public static long SetINIString(string as_section, string as_key, string as_Value, string as_FileName) //{ // return WritePrivateProfileString(as_section, as_key, as_Value, as_FileName); //} ///// <summary> ///// 设置INI文件参数 ///// </summary> /////<param name=as_section>INI文件中的段落 /////<param name=as_key>INI文件中的关键字 /////<param name=as_Value>INI文件中关键字的数值 //public static long SetINIString(string as_section, string as_key, string as_Value) //{ // return SetINIString(as_section, as_key, as_Value, gs_FileName); //} /// <summary> /// 写入INI文件 /// </summary> ///<param name=section>节点名称[如[TypeName]] ///<param name=key>键 ///<param name=val>值 ///<param name=filepath>文件路径 /// <returns></returns> [DllImport(kernel32)] public static extern long WritePrivateProfileString(string section, string key, string val, string filepath); [DllImport(kernel32.dll)] public extern static int GetPrivateProfileSectionNamesA(byte[] buffer, int iLen, string fileName); /// <summary> /// 写入INI文件(section:节点名称 key:键 val:值) /// </summary> ///<param name=section>节点名称 ///<param name=key>键 ///<param name=val>值 /// <returns></returns> public static long SetINIString(string section, string key, string val, string as_FilePath = ) { if (as_FilePath == ) { return (WritePrivateProfileString(section, key, val, strFilePath)); } else { return (WritePrivateProfileString(section, key, val, as_FilePath)); } } /// <summary> /// 读取INI文件 /// </summary> ///<param name=section>节点名称 ///<param name=key>键 ///<param name=def>值 ///<param name=retval>stringbulider对象 ///<param name=size>字节大小 ///<param name=filePath>文件路径 /// <returns></returns> [DllImport(kernel32)] public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath); public static string strFilePath = Application.StartupPath + \\Config.ini;//获取INI文件默认路径 public static string strSec = ; //INI文件名 /// <summary> /// 读取INI文件中的内容方法 (Section 节点名称;key 键) /// </summary> ///<param name=Section>节点名称 ///<param name=key>键 /// <returns></returns> public static string ContentValue(string Section, string key, string as_FilePath = ) { StringBuilder temp = new StringBuilder(1024); if (as_FilePath == ) { GetPrivateProfileString(Section, key, , temp, 1024, strFilePath); } else { GetPrivateProfileString(Section, key, , temp, 1024, as_FilePath); } return temp.ToString(); } /// <summary> /// 获取指定小节所有项名和值的一个列表 /// </summary> ///<param name=section>节 段,欲获取的小节。注意这个字串不区分大小写 ///<param name=buffer>缓冲区 返回的是一个二进制的串,字符串之间是用\0分隔的 ///<param name=nSize>缓冲区的大小 ///<param name=filePath>初始化文件的名字。如没有指定完整路径名,windows就在Windows目录中查找文件 /// <returns></returns> [DllImport(kernel32)] public static extern int GetPrivateProfileSection(string section, byte[] buffer, int nSize, string filePath); /// <summary> /// 获取指定段section下的所有键值对 返回集合的每一个键形如key=value /// </summary> ///<param name=section>指定的段落 ///<param name=filePath>ini文件的绝对路径 /// <returns></returns> public static List<string> ReadKeyValues(string section, string as_FilePath = ) { byte[] buffer = new byte[32767]; List<string> list = new List<string>(); int length = 0; if (as_FilePath == ) { length = GetPrivateProfileSection(section, buffer, buffer.GetUpperBound(0), strFilePath); } else { length = GetPrivateProfileSection(section, buffer, buffer.GetUpperBound(0), as_FilePath); } string temp; int postion = 0; for (int i = 0; i < length; i++) { if (buffer[i] == 0x00) //以\0来作为分隔 { temp = System.Text.ASCIIEncoding.Default.GetString(buffer, postion, i - postion).Trim(); postion = i + 1; if (temp.Length > 0) { list.Add(temp); } } } return list; } /// <summary> /// 删除指定的key /// </summary> ///<param name=section>要写入的段落名 ///<param name=key>要删除的键 ///<param name=fileName>INI文件的完整路径和文件名 public static void DelKey(string section, string key, string as_FilePath = ) { if (as_FilePath == ) { WritePrivateProfileString(section, key, null, strFilePath); } else { WritePrivateProfileString(section, key, null, as_FilePath); } } /// <summary> /// 返回该配置文件中所有Section名称的集合 /// </summary> public static ArrayList ReadSections() { byte[] buffer = new byte[65535]; int rel = GetPrivateProfileSectionNamesA(buffer, buffer.GetUpperBound(0), strFilePath); int iCnt, iPos; ArrayList arrayList = new ArrayList(); string tmp; if (rel > 0) { iCnt = 0; iPos = 0; for (iCnt = 0; iCnt < rel; iCnt++) { if (buffer[iCnt] == 0x00) { tmp = System.Text.ASCIIEncoding.UTF8.GetString(buffer, iPos, iCnt - iPos).Trim(); iPos = iCnt + 1; if (tmp != ) arrayList.Add(tmp); } } } return arrayList; } }</string></string></string>

运行结果:

这里写图片描述

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


评论


亲,登录后才可以留言!