コード例 #1
0
        /// <summary>
        /// GET方式请求URL,并返回T类型
        /// </summary>
        /// <typeparam name="T">接收JSON的数据类型</typeparam>
        /// <param name="url"></param>
        /// <param name="encoding"></param>
        /// <param name="maxJsonLength">允许最大JSON长度</param>
        /// <returns></returns>
        public static T GetJson <T>(string url, Encoding encoding = null, int?maxJsonLength = null)
        {
            string returnText = RequestUtility.HttpGet(url, encoding);

            WeixinTrace.SendApiLog(url, returnText);

            JavaScriptSerializer js = new JavaScriptSerializer();

            if (maxJsonLength.HasValue)
            {
                js.MaxJsonLength = maxJsonLength.Value;
            }

            if (returnText.Contains("errcode"))
            {
                //可能发生错误
                WxJsonResult errorResult = js.Deserialize <WxJsonResult>(returnText);
                if (errorResult.errcode != ReturnCode.请求成功)
                {
                    //发生错误
                    throw new ErrorJsonResultException(
                              string.Format("微信请求发生错误!错误代码:{0},说明:{1}",
                                            (int)errorResult.errcode, errorResult.errmsg), null, errorResult, url);
                }
            }

            T result = js.Deserialize <T>(returnText);

            return(result);
        }
コード例 #2
0
ファイル: OAuth2Controller.cs プロジェクト: sayno-lk/WXTest
        /// <summary>
        /// 授权页面API
        /// </summary>
        /// <param name="returnUrl">用户尝试进入的需要登录的页面</param>
        /// <returns></returns>
        public ActionResult Index(string returnUrl, int?oauthScope)
        {
            if (returnUrl == null)
            {
                returnUrl = "http://" + Request.Url.Authority + "/OAuth2/UserInfoCallback1";
            }
            var state = "JeffreySu-" + DateTime.Now.Millisecond; //随机数,用于识别请求可靠性

            Session["State"] = state;                            //储存随机数到Session

            ViewData["returnUrl"] = returnUrl;
            WeixinTrace.SendApiLog(oauthScope.ToString(), "请求微信oauthScope");
            //此页面引导用户点击授权
            string url = "";

            if (oauthScope == (int)OAuthScope.snsapi_userinfo)
            {
                url =
                    OAuthApi.GetAuthorizeUrl(appId,
                                             "http://" + Request.Url.Authority + "/oauth2/UserInfoCallback?returnUrl=" + returnUrl.UrlEncode(),
                                             state, OAuthScope.snsapi_userinfo);
            }
            else
            {
                url =
                    OAuthApi.GetAuthorizeUrl(appId,
                                             "http://" + Request.Url.Authority + "/oauth2/BaseCallback?returnUrl=" + returnUrl.UrlEncode(),
                                             state, OAuthScope.snsapi_base);
            }
            return(Redirect(url));
            //ViewData["UrlUserInfo"] = url;
            //return View();
        }
コード例 #3
0
        /// <summary>
        /// 【异步方法】PostGetJson的异步版本
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <param name="cookieContainer"></param>
        /// <param name="fileStream"></param>
        /// <param name="encoding"></param>
        /// <param name="cer">证书,如果不需要则保留null</param>
        /// <param name="useAjax">是否使用Ajax请求</param>
        /// <param name="timeOut"></param>
        /// <param name="checkValidationResult"></param>
        /// <returns></returns>
        public static async Task <T> PostGetJsonAsync <T>(string url, CookieContainer cookieContainer = null, Stream fileStream = null, Encoding encoding = null, X509Certificate2 cer = null, bool useAjax = false, int timeOut = Config.TIME_OUT, bool checkValidationResult = false)
        {
            string returnText = await RequestUtility.HttpPostAsync(url, cookieContainer, fileStream, null, null, encoding, cer, useAjax, timeOut, checkValidationResult);

            WeixinTrace.SendApiLog(url, returnText);

            var result = GetResult <T>(returnText);

            return(result);
        }