public BoolResult<string> ChangeInterfaceMode(WeiXinMode mode, WeiXinModeStatus status) { if (LoginInfo == null) { return new BoolResult<string>(false, "未登陆!"); } string refererUrl = string.Format("https://mp.weixin.qq.com/cgi-bin/advanced?action=edit&t=advanced/edit&token={0}&lang=zh_CN", LoginInfo.Token); if (mode == WeiXinMode.开发者模式) { refererUrl = string.Format("https://mp.weixin.qq.com/cgi-bin/advanced?action=dev&t=advanced/dev&token={0}&lang=zh_CN", LoginInfo.Token); } string postData = string.Format("flag={0}&token={1}&type={2}", (int)status, LoginInfo.Token, (int)mode); try { HttpClient client = new HttpClient(LoginInfo.Cookies); client.Headers.Add("Referer", refererUrl); string retMsg = string.Empty; string retJson = client.PostData(WeixinHttpUrl.WEIXIN_SWITCHADVANCEMODEL, postData, Encoding.UTF8, Encoding.UTF8, out retMsg); Dictionary<string, object> retDic = JSON.GetObject<Dictionary<string, object>>(retJson); Dictionary<string, object> base_resp = retDic["base_resp"] as Dictionary<string, object>; if (base_resp != null && base_resp["ret"].ToString() == "0") { return new BoolResult<string>(true, "模式改变成功"); } } catch (Exception ex) { throw; } return new BoolResult<string>(false, "模式开启失败!"); }
/// <summary> /// 绑定接口 /// </summary> /// <param name="url"></param> /// <param name="token"></param> /// <returns></returns> public BoolResult<string> BindingInterface(string url, string token, string encoding_aeskey, int callback_encrypt_mode) { if (LoginInfo == null) { return new BoolResult<string>(false, "绑定失败,未登陆!"); } ChangeInterfaceMode(WeiXinMode.开发者模式, WeiXinModeStatus.开启); string refererUrl = string.Format("https://mp.weixin.qq.com/cgi-bin/advanced?action=interface&t=advanced/interface&token={0}&lang=zh_CN", LoginInfo.Token); string postData = string.Format("url={0}&callback_token={1}&encoding_aeskey={2}&callback_encrypt_mode={3}", System.Web.HttpUtility.UrlEncode(url, Encoding.UTF8), System.Web.HttpUtility.UrlEncode(token, Encoding.UTF8), System.Web.HttpUtility.UrlEncode(encoding_aeskey, Encoding.UTF8), callback_encrypt_mode.ToString() ); //Log.Default.Debug("postData:" + postData); try { string msg = string.Empty; HttpClient client = new HttpClient(LoginInfo.Cookies); client.Headers.Add("Referer", refererUrl); string retJson = client.PostData(string.Format(WeixinHttpUrl.WEIXIN_BINDGINFERFACE, LoginInfo.Token), postData, Encoding.UTF8, Encoding.UTF8, out msg); //Dictionary<string, object> dic = JSON.GetObject<Dictionary<string, object>>(retJson); JObject jobj = JObject.Parse(retJson); int code = jobj["base_resp"]["ret"].Value<int>(); //int code = int.Parse(dic["ret"].ToString()); if (code == 0) { return new BoolResult<string>(true, "绑定成功!"); } else if (code == -302) { return new BoolResult<string>(false, jobj["base_resp"]["err_msg"].Value<string>()); } else { return new BoolResult<string>(false, string.Format("绑定失败,失败原因:{0},错误代码:{1}!", jobj["base_resp"]["err_msg"].Value<string>(), code)); } } catch (Exception ex) { Log.Default.Error(ex); } return new BoolResult<string>(false, "绑定失败,请重试"); //绑定接口 }
public static OAuthUserInfo GetUserInfo(string accessToken, string openId) { var url = string.Format("https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}", accessToken, openId); HttpClient client = new HttpClient(); client.Encoding = Encoding.UTF8; string str = client.DownloadString(url); Log.Default.Debug("GetUserInfourl:" + url); Log.Default.Debug("GetUserInfoRes:" + str); return JSON.GetObject<OAuthUserInfo>(str); }