c# 微信开发 《获取用户的信息》

public const string WeiXin_User_GetInfoUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang=zh_CN";

/// <summary>
        /// 根据OpenID 获取用户在微信的基本信息(需关注公众号)
        /// </summary>
        /// <param name="openId"></param>
        public static WeiXinUserInfo GetUserInfo(string openId)
        {
            WeiXinUserInfo info = new WeiXinUserInfo();
            try {
                var token = GetAccsss_token();
                string url = string.Format(WeiXin_User_GetInfoUrl, token, openId);
                string result = GetData(url);
                if (string.IsNullOrEmpty(result))
                    return null;
                info = JsonConvert.DeserializeObject<WeiXinUserInfo>(result);
            }catch(Exception ex){
                LogHelper.WriteFileLog("getUserWeixinInfoError", ex.Message);
            }
            
            return info;
        }
     

/// <summary>
        /// 保存用户信息
        /// </summary>
        public void SaveUserInfo(string FromUserName) {
            //第一次关注
            WeiXinCommom.WeiXinUserInfo info = WeiXinCommom.GetUserInfo(FromUserName);
            string SaveUserWeiXinInfurl = RequestUrl.ServiceUrl + RequestUrl.SaveUserWeiXinInfo;
            Entity SaveUserWeiXinInfdata = new Entity(new PropertyCollection());
            string WeixinQrCodePath = ConfigurationManager.AppSettings["UserHeadUploadPath"].TryString();
            string rootpath = System.AppDomain.CurrentDomain.BaseDirectory;//程序运行地址
            string path = rootpath + WeixinQrCodePath; //文件夹绝对路径
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            string Guidcode = Guid.NewGuid().TryString();
            string HeadImgPath = path + "/" + Guidcode + ".jpg";//用户头像的绝对路径
            string SaveImg = "/" + WeixinQrCodePath + "/" + Guidcode + ".jpg";
            WeiXinCommom.SaveUrlImage(info.HeadImgUrl, HeadImgPath);
            SaveUserWeiXinInfdata.AddSimple("WeiXinOpenID", FromUserName, typeof(string));
            SaveUserWeiXinInfdata.AddSimple("NickName", (info.NickName == null ? "" : info.NickName), typeof(string));
            SaveUserWeiXinInfdata.AddSimple("HeadImgUrl", SaveImg, typeof(string));
            SaveUserWeiXinInfdata.AddSimple("Subscribe", 1, typeof(int));
            RequestUrl.RequestWebByPost(SaveUserWeiXinInfurl, SaveUserWeiXinInfdata);//更新用户关注状态
        }     

/// <summary>
        /// 保存网络图片
        /// </summary>
        /// <param name="stUrl"></param>
        /// <param name="imgPath"></param>
        /// <returns></returns>
        public static void SaveUrlImage(string stUrl, string imgPath)
        {
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(stUrl);

            req.Method = "GET";

            using (WebResponse wr = req.GetResponse())
            {
                HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();
                string strpath = myResponse.ResponseUri.ToString();
                WebClient mywebclient = new WebClient();

                try
                {
                    mywebclient.DownloadFile(strpath, imgPath);
                }
                catch (Exception ex)
                {
                    LogHelper.WriteFileLog("WebResponse", ex.Message);
                }
            }
        }

  

转载于:https://www.cnblogs.com/yyyuguo/p/8426804.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值