/// <summary> /// 设置编辑或者开发者模式。 /// </summary> /// <param name="type">类型,1代表编辑模式,2代表开发者模式。</param> /// <param name="enable">是否开启对应的模式。</param> private bool SetEditOrDevelopMode(int type, bool enable) { using (var client = new LastingWebClient(_cookieContainer)) { client.Headers.Add("ContentType", "application/x-www-form-urlencoded"); client.Headers.Add("Referer", "https://mp.weixin.qq.com/"); var data = client.UploadData("https://mp.weixin.qq.com/misc/skeyform?form=advancedswitchform&lang=zh_CN", "POST", Encoding.UTF8.GetBytes(string.Format("flag={0}&type={1}&token={2}", enable ? 1 : 0, type, LoginResult.Token))); var json = Encoding.UTF8.GetString(data); return(json.StartsWith("{\"base_resp\":{\"ret\":0,\"err_msg\":\"ok\"}")); } }
/// <summary> /// 设置OAuth回调域名。 /// </summary> /// <param name="domain"></param> /// <returns></returns> public bool SetOAuthDomain(string domain) { using (var client = new LastingWebClient(_cookieContainer)) { client.Headers.Add("Referer", "https://mp.weixin.qq.com/"); var data = client.UploadData("https://mp.weixin.qq.com/merchant/myservice?action=set_oauth_domain&f=json", Encoding.UTF8.GetBytes( string.Format("token={0}&lang=zh_CN&f=json&ajax=1&random=0.18229547117417194&domain={1}", _loginResult.Token, domain))); var content = Encoding.UTF8.GetString(data); return(content.Contains("ok")); } }
/// <summary> /// 登录微信。 /// </summary> /// <param name="userName">用户名称。</param> /// <param name="password">用户密码。</param> /// <returns>登录结果。</returns> public LoginResult Login(string userName, string password) { _isLogin = true; _cookieContainer = new CookieContainer(); using (var client = new LastingWebClient(_cookieContainer)) { client.Headers.Add("Referer", "https://mp.weixin.qq.com/"); client.Headers.Add("ContentType", "application/x-www-form-urlencoded"); var data = client.UploadData("https://mp.weixin.qq.com/cgi-bin/login", "POST", Encoding.UTF8.GetBytes("username="******"&pwd=" + Md5Hash(password) + "&imgcode=&f=json")); var token = GetToken(Encoding.UTF8.GetString(data)); return _loginResult = string.IsNullOrWhiteSpace(token) ? null : new LoginResult { Token = token }; } }
/// <summary> /// 登录微信。 /// </summary> /// <param name="userName">用户名称。</param> /// <param name="password">用户密码。</param> /// <returns>登录结果。</returns> public LoginResult Login(string userName, string password) { _isLogin = true; _cookieContainer = new CookieContainer(); using (var client = new LastingWebClient(_cookieContainer)) { client.Headers.Add("Referer", "https://mp.weixin.qq.com/"); client.Headers.Add("ContentType", "application/x-www-form-urlencoded"); var data = client.UploadData("https://mp.weixin.qq.com/cgi-bin/login", "POST", Encoding.UTF8.GetBytes("username="******"&pwd=" + Md5Hash(password) + "&imgcode=&f=json")); var token = GetToken(Encoding.UTF8.GetString(data)); return(_loginResult = string.IsNullOrWhiteSpace(token) ? null : new LoginResult { Token = token }); } }
/// <summary> /// 设置开发者接口信息(会自动开启开发者模式)。 /// </summary> /// <param name="url">Url地址。</param> /// <param name="token">令牌。</param> /// <param name="aeskey">EncodingAESKey。</param> /// <param name="encryptMode">加密模式。</param> /// <returns>是否更新成功。</returns> public bool SetDevelopInterface(string url, string token, string aeskey, EncryptMode encryptMode) { //开启开发者模式。 if (!SetDevelopMode(true)) { throw new WeiXinWebApiException("更新开发者接口信息失败,因为开启开发者模式失败。"); } using (var client = new LastingWebClient(_cookieContainer)) { client.Headers.Add("Referer", "https://mp.weixin.qq.com/"); client.Headers.Add("ContentType", "application/x-www-form-urlencoded"); var data = client.UploadData( string.Format("https://mp.weixin.qq.com/advanced/callbackprofile?t=ajax-response&token={0}&lang=zh_CN", LoginResult.Token), "POST", Encoding.UTF8.GetBytes(string.Format("url={0}&callback_token={1}&encoding_aeskey={2}&callback_encrypt_mode={3}", HttpUtility.UrlEncode(url), token, aeskey, (int)encryptMode))); var json = Encoding.UTF8.GetString(data); return(json.Contains("\"err_msg\":\"ok\"")); } }
/// <summary> /// 设置编辑或者开发者模式。 /// </summary> /// <param name="type">类型,1代表编辑模式,2代表开发者模式。</param> /// <param name="enable">是否开启对应的模式。</param> private bool SetEditOrDevelopMode(int type, bool enable) { using (var client = new LastingWebClient(_cookieContainer)) { client.Headers.Add("ContentType", "application/x-www-form-urlencoded"); client.Headers.Add("Referer", "https://mp.weixin.qq.com/"); var data = client.UploadData("https://mp.weixin.qq.com/misc/skeyform?form=advancedswitchform&lang=zh_CN", "POST", Encoding.UTF8.GetBytes(string.Format("flag={0}&type={1}&token={2}", enable ? 1 : 0, type, LoginResult.Token))); var json = Encoding.UTF8.GetString(data); return json.StartsWith("{\"base_resp\":{\"ret\":0,\"err_msg\":\"ok\"}"); } }
/// <summary> /// 设置OAuth回调域名。 /// </summary> /// <param name="domain"></param> /// <returns></returns> public bool SetOAuthDomain(string domain) { using (var client = new LastingWebClient(_cookieContainer)) { client.Headers.Add("Referer", "https://mp.weixin.qq.com/"); var data = client.UploadData("https://mp.weixin.qq.com/merchant/myservice?action=set_oauth_domain&f=json", Encoding.UTF8.GetBytes( string.Format("token={0}&lang=zh_CN&f=json&ajax=1&random=0.18229547117417194&domain={1}", _loginResult.Token, domain))); var content = Encoding.UTF8.GetString(data); return content.Contains("ok"); } }
/// <summary> /// 设置开发者接口信息(会自动开启开发者模式)。 /// </summary> /// <param name="url">Url地址。</param> /// <param name="token">令牌。</param> /// <param name="aeskey">EncodingAESKey。</param> /// <param name="encryptMode">加密模式。</param> /// <returns>是否更新成功。</returns> public bool SetDevelopInterface(string url, string token, string aeskey, EncryptMode encryptMode) { //开启开发者模式。 if (!SetDevelopMode(true)) throw new WeiXinWebApiException("更新开发者接口信息失败,因为开启开发者模式失败。"); using (var client = new LastingWebClient(_cookieContainer)) { client.Headers.Add("Referer", "https://mp.weixin.qq.com/"); client.Headers.Add("ContentType", "application/x-www-form-urlencoded"); var data = client.UploadData( string.Format("https://mp.weixin.qq.com/advanced/callbackprofile?t=ajax-response&token={0}&lang=zh_CN", LoginResult.Token), "POST", Encoding.UTF8.GetBytes(string.Format("url={0}&callback_token={1}&encoding_aeskey={2}&callback_encrypt_mode={3}", HttpUtility.UrlEncode(url), token, aeskey, (int)encryptMode))); var json = Encoding.UTF8.GetString(data); return json.Contains("\"err_msg\":\"ok\""); } }