微信公众平台模拟登录:
public static WeiXinRetInfo Login(string userName, string password, out CookieContainer cookieContainer) { try { string pwd = GetMd5Str32(password).ToLower(); string post_data = "username=" + userName + "&pwd=" + pwd + "&imgcode=&f=json"; byte[] byteArray = Encoding.UTF8.GetBytes(post_data); // 转化 HttpWebRequest web_request = (HttpWebRequest)WebRequest.Create("https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN"); var cc = new CookieContainer(); web_request.CookieContainer = cc; web_request.AllowAutoRedirect = false; web_request.KeepAlive = true; web_request.Method = "POST"; web_request.ContentType = "application/x-www-form-urlencoded"; web_request.ContentLength = byteArray.Length; web_request.Referer = "https://mp.weixin.qq.com/"; Stream newStream = web_request.GetRequestStream(); newStream.Write(byteArray, 0, byteArray.Length); newStream.Close(); HttpWebResponse web_response = (HttpWebResponse)web_request.GetResponse(); StreamReader sr = new StreamReader(web_response.GetResponseStream(), Encoding.UTF8); string loginResult = sr.ReadToEnd(); var js = new JavaScriptSerializer(); var wxRet = js.Deserialize<WeiXinRetInfo>(loginResult); cookieContainer = cc; return wxRet; } catch (Exception syse) { cookieContainer = null; return new WeiXinRetInfo() { ErrCode = "999", ErrMsg = string.Empty, Ret = string.Empty, ShowVerifyCode = string.Empty }; } } private static string GetMd5Str32(string str) { MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider(); char[] temp = str.ToCharArray(); byte[] buf = new byte[temp.Length]; for (int i = 0; i < temp.Length; i++) { buf[i] = (byte)temp[i]; } byte[] data = md5Hasher.ComputeHash(buf); StringBuilder sBuilder = new StringBuilder(); for (int i = 0; i < data.Length; i++) { sBuilder.Append(data[i].ToString("X2")); } return sBuilder.ToString(); }
微信群发消息:
/// <summary> /// 微信群发消息 /// </summary> /// <param name="Message">发送内容</param> /// <param name="cookie">登录时所得Cookie</param> /// <param name="token">登录时所得token</param> /// <param name="errorMsg">返回错误信息</param> /// <returns></returns> public static bool SendGroupMessage(string Message, CookieContainer cookie, string token, out string errorMsg) { bool result = false; errorMsg = "fail"; string strMsg = System.Web.HttpUtility.UrlEncode(Message); //对传递过来的信息进行url编码 string padate = "type=1&content=" + strMsg + "&error=false&groupid=-1&ajax=1&token=" + token; string url = "https://mp.weixin.qq.com/cgi-bin/masssend?t=ajax-response"; byte[] byteArray = Encoding.UTF8.GetBytes(padate); // 转化 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); webRequest.CookieContainer = cookie; //登录时得到的缓存 webRequest.Referer = "https://mp.weixin.qq.com/cgi-bin/masssendpage?t=wxm-send&token=" + token + "&lang=zh_CN"; // webRequest.Referer = "https://mp.weixin.qq.com"; webRequest.Method = "POST"; webRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0"; webRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; webRequest.ContentLength = byteArray.Length; Stream newStream = webRequest.GetRequestStream(); newStream.Write(byteArray, 0, byteArray.Length); //写入参数 newStream.Close(); HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default); string text = sr.ReadToEnd(); if (text.Contains("not have masssend quota today")) { result = false; errorMsg = "微信平台控制:订阅号每天仅能群发一条消息,服务号每月仅能群发一条。"; } else if (text.Contains("ok")) { result = true; } return result; }
验证微博账户的状态是否验签成功,即在公众平台提交Url是否成功:
/// <summary> /// 验证微博账户的状态 /// </summary> /// <param name="input_url">验证所要输入的Url</param> /// <param name="input_token">验证所要输入的Token</param> /// <param name="loginName">登录用户名</param> /// <param name="loginPassword">登录密码</param> /// <returns></returns> public static bool CheckWXAccountStatus(string input_url, string input_token, string loginName, string loginPassword) { bool result = false; try { CookieContainer cookieCon = new CookieContainer(); var token = ""; WeiXinRetInfo wxRet = WeiXinHelper.Login(loginName, loginPassword, out cookieCon); if (wxRet.ErrCode == "0") token = wxRet.ErrMsg.Split('=')[3]; string padate = "callback_token=" + input_token + "&url=" + input_url; string requrl = "https://mp.weixin.qq.com/cgi-bin/callbackprofile?t=ajax-response&token=" + token + "&lang=zh_CN"; byte[] byteArray = Encoding.UTF8.GetBytes(padate); // 转化 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(requrl); webRequest.CookieContainer = cookieCon; //登录时得到的缓存 webRequest.Referer = "https://mp.weixin.qq.com/cgi-bin/advanced?action=interface&t=advanced/interface&token=" + token + "&lang=zh_CN"; webRequest.Method = "POST"; webRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0"; webRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; webRequest.ContentLength = byteArray.Length; Stream newStream = webRequest.GetRequestStream(); newStream.Write(byteArray, 0, byteArray.Length); //写入参数 newStream.Close(); HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default); string text = sr.ReadToEnd(); var js = new JavaScriptSerializer(); var resultwxRet = js.Deserialize<WeiXinRetInfo>(text); if (resultwxRet.Ret == "0") result = true; } catch { result = false; } return result; }
发布自定义菜单:
//发布微信自定义菜单 public static bool PublishMenu(string appId, string appSecret, int shopID, string accessToken) { bool result = false; try { var firstArray = new ArrayList(); var firstList = MenuRepository.GetFirstMenus(shopID); foreach (var fmenu in firstList) { var fChilf = new ArrayList(); var secondList = MenuRepository.GetSecondMenus(fmenu.ID); if (secondList.Count != 0) { foreach (var secondMenu in secondList) { if (secondMenu.Type == "外链") fChilf.Add(new { type = "view", name = secondMenu.Name, url = secondMenu.ReplyMsg }); else fChilf.Add(new { type = "click", name = secondMenu.Name, key = secondMenu.Key }); } if (fmenu.Type == "外链") firstArray.Add(new { type = "view", name = fmenu.Name, url = fmenu.ReplyMsg, sub_button = fChilf }); else firstArray.Add(new { type = "click", name = fmenu.Name, key = fmenu.Key, sub_button = fChilf }); } else { if (fmenu.Type == "外链") firstArray.Add(new { type = "view", name = fmenu.Name, url = fmenu.ReplyMsg }); else firstArray.Add(new { type = "click", name = fmenu.Name, key = fmenu.Key }); } } var menuObj = new { button = firstArray }; var menuJson = Newtonsoft.Json.JsonConvert.SerializeObject(menuObj); string url = "http://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + accessToken; byte[] byteArray = Encoding.UTF8.GetBytes(menuJson); // 转化 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); webRequest.Method = "POST"; webRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; webRequest.ContentLength = byteArray.Length; Stream newStream = webRequest.GetRequestStream(); newStream.Write(byteArray, 0, byteArray.Length); //写入参数 newStream.Close(); HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default); string text = sr.ReadToEnd(); var js = new JavaScriptSerializer(); var WXJSONReback = js.Deserialize<WXJSONReback>(text); if (WXJSONReback.errcode == "0") { result = true; } return result; } catch { result = false; } return result; }
获取access_token:
/// <summary> /// 获取access_token /// </summary> /// <param name="appid"></param> /// <param name="appsecret"></param> /// <returns></returns> public static string GetAccessToken(string appid, string appsecret) { string accesstokenStr = string.Empty; try { string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + appsecret; HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); webRequest.Method = "GET"; HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default); string text = sr.ReadToEnd(); var js = new JavaScriptSerializer(); var accesstoken = js.Deserialize<WXAccessToken>(text); accesstokenStr = accesstoken.access_token; return accesstokenStr; } catch { return accesstokenStr; } }
取消自定义菜单:
public static bool CancelPublishMenu(string accessToken) { bool result = false; try { string url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + accessToken; HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); webRequest.Method = "GET"; webRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default); string text = sr.ReadToEnd(); var js = new JavaScriptSerializer(); var WXJSONReback = js.Deserialize<WXJSONReback>(text); if (WXJSONReback.errcode == "0") { result = true; } } catch { result = false; } return result; }