コード例 #1
1
        //刷新accesstoken
        public static string RefrenshToken(string appid, string appsecret)
        {
            string userAgent = System.Web.HttpContext.Current.Request.UserAgent;
            string url       = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", appid, appsecret);

            HttpWebResponse res    = WechatHttpWebResponseUtility.CreateGetHttpResponse(url, null, userAgent, null);
            Stream          stream = res.GetResponseStream();
            StreamReader    sr     = new StreamReader(stream);
            string          result = sr.ReadToEnd();

            WechatAccessToken token = new WechatAccessToken();

            token = JsonConvert.DeserializeObject <WechatAccessToken>(result);
            string access_token = token.access_token;

            if (string.IsNullOrEmpty(token.access_token))
            {
                WechatError err = new WechatError();
                err          = JsonConvert.DeserializeObject <WechatError>(result);
                access_token = "appid或者appsecret错误,无法获取access_token 微信错误代码:" + err.errcode + "微信错误信息:" + err.errmsg;
                WechatLogHelper.Error(access_token);
            }
            else
            {
                ConfigService.WriteKey("WechatAccessToken", access_token);
                ConfigService.WriteKey("WechatAccessTokenExpiredTime", System.DateTime.Now.AddMinutes(115).ToString("yyyy-MM-dd HH:mm:ss"));
            }
            return(access_token);
        }
コード例 #2
0
        public static WebchatJsUserinfo GetUserInfo(string userAgent, string CODE)
        {
            WechatConfig      wechatconfig = AccessTokenService.GetWechatConfig();
            WebchatJsUserinfo userinfo     = new WebchatJsUserinfo();
            string            url          = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + wechatconfig.Appid + "&secret=" + wechatconfig.AppSecret + "&code=" + CODE + "&grant_type=authorization_code";

            HttpWebResponse response = WechatHttpWebResponseUtility.CreateGetHttpResponse(url, null, userAgent, null);

            Stream       stream = response.GetResponseStream();
            StreamReader sr     = new StreamReader(stream);
            string       result = sr.ReadToEnd();

            WechatJsToken token = JsonConvert.DeserializeObject <WechatJsToken>(result);


            string url2 = "https://api.weixin.qq.com/sns/userinfo?access_token=" + token.access_token + "&openid=" + token.openid + "&lang=zh_CN";

            HttpWebResponse res     = WechatHttpWebResponseUtility.CreateGetHttpResponse(url2, null, userAgent, null);
            Stream          stream2 = res.GetResponseStream();
            StreamReader    sr2     = new StreamReader(stream2);
            string          result2 = sr2.ReadToEnd();

            userinfo = JsonConvert.DeserializeObject <WebchatJsUserinfo>(result2);

            return(userinfo);
        }
コード例 #3
0
        public static WechatJsTicket RefrenshJsapi_ticket(string access_token, string userAgent)
        {
            string url = string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type=jsapi", access_token);

            HttpWebResponse response = WechatHttpWebResponseUtility.CreateGetHttpResponse(url, null, userAgent, null);

            //Stream stream = response.GetResponseStream();
            //StreamReader sr = new StreamReader(stream);
            //string result = sr.ReadToEnd();
            string         result = WechatHttpWebResponseUtility.HttpResponseToString(response);
            WechatJsTicket ticket = JsonConvert.DeserializeObject <WechatJsTicket>(result);

            if (string.IsNullOrEmpty(ticket.ticket))
            {
                WechatError err = new WechatError();
                err = JsonConvert.DeserializeObject <WechatError>(result);
                string error = "appid或者appsecret错误,无法获取access_token 微信错误代码:" + err.errcode + "微信错误信息:" + err.errmsg;
                WechatLogHelper.Error(error);
            }
            else
            {
                string ticketExpiredTime = System.DateTime.Now.AddMinutes(115).ToString("yyyy-MM-dd HH:mm:ss");

                ConfigTools.WriteKey("WechatJsapi_ticket", ticket.ticket);
                ConfigTools.WriteKey("WechatJsapi_ticketExpiredTime", ticketExpiredTime);
            }


            return(ticket);
        }
コード例 #4
0
        public static string GetMenu(string access_token)
        {
            string userAgent = System.Web.HttpContext.Current.Request.UserAgent;
            string url       = string.Format("https://api.weixin.qq.com/cgi-bin/menu/get?access_token={0}", access_token);

            HttpWebResponse res    = WechatHttpWebResponseUtility.CreateGetHttpResponse(url, null, userAgent, null);
            Stream          stream = res.GetResponseStream();
            StreamReader    sr     = new StreamReader(stream);
            string          result = sr.ReadToEnd();

            return(result);
        }
コード例 #5
0
        //public static void GetTempMedia(string savePath, string access_token, string media_id)
        //{
        //    var url = string.Format("http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={0}&media_id={1}", access_token, media_id);
        //    FileStream fs = new FileStream(savePath, FileMode.Create);
        //    WechatCurlFileTools.Download(url, fs);
        //    fs.Close();
        //    fs.Dispose();
        //}


        //刷新accesstoken
        public static ForeverMaterialCount GetForeverMaterialCount(string access_token)
        {
            string userAgent = System.Web.HttpContext.Current.Request.UserAgent;
            string url       = string.Format("https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token={0}", access_token);

            HttpWebResponse      res           = WechatHttpWebResponseUtility.CreateGetHttpResponse(url, null, userAgent, null);
            Stream               stream        = res.GetResponseStream();
            StreamReader         sr            = new StreamReader(stream);
            string               result        = sr.ReadToEnd();
            ForeverMaterialCount materialCount = new ForeverMaterialCount();

            materialCount = JsonConvert.DeserializeObject <ForeverMaterialCount>(result);
            return(materialCount);
        }
コード例 #6
0
ファイル: WechatApi.cs プロジェクト: jorzen2010/PsyCoderPro
        public static WechatUserInfo GetUserInfo(string access_token, string userAgent, string openid)
        {
            WechatUserInfo userinfo = new WechatUserInfo();

            string url = string.Format("https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang=zh_CN", access_token, openid);

            HttpWebResponse response = WechatHttpWebResponseUtility.CreateGetHttpResponse(url, null, userAgent, null);

            string result = WechatHttpWebResponseUtility.HttpResponseToString(response);

            userinfo = JsonConvert.DeserializeObject <WechatUserInfo>(result);

            return(userinfo);
        }