微信获取OpenId的方法

编程入门 行业动态 更新时间:2024-10-08 20:37:16

微信获取OpenId的<a href=https://www.elefans.com/category/jswz/34/1771314.html style=方法"/>

微信获取OpenId的方法

[System.Web.Http.HttpGet]
public JsonResult GetOpenId(string Code)//传入Code值
{

       string  AppId = ConfigurationManager.AppSettings["AppId"]; //获取数据为string类型且在appsettings[“相关key”]string AppSc = ConfigurationManager.AppSettings["AppSc"]; //获取数据为string类型且在appsettings[“相关key”]string code = Code;string html = string.Empty;string url = $"={AppId}&secret={AppSc}&js_code={code}&grant_type=authorization_code";HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);request.Method = "GET";HttpWebResponse response = request.GetResponse() as HttpWebResponse;Stream ioStream = response.GetResponseStream();StreamReader sr = new StreamReader(ioStream, Encoding.UTF8);html = sr.ReadToEnd();sr.Close();ioStream.Close();response.Close();string key = "\"openid\":\"";string OpenId = "";CodeMsg msg = new CodeMsg();msg.Reson = html;int startIndex = html.IndexOf(key);if (startIndex != -1 ){//int endIndex = html.IndexOf ("\",", startIndex);//OpenId = html.Substring(startIndex + key.Length, endIndex - startIndex - key.Length); string jsonString = html;JObject json = JObject.Parse(jsonString);//推荐使用Jboject来使用。OpenId = json["openid"].ToString();}if (!string.IsNullOrEmpty(OpenId)){AccontMain model = new AccontMain();model = GetModel(OpenId);if (model != null){msg.Code = 1;msg.Count = 1;msg.Data = model;}else{model = new AccontMain();msg.Code = 1;msg.Count = 0;model.WxId = OpenId;msg.Data = model;}}return Json<CodeMsg>(msg);}

更多推荐

微信获取OpenId的方法

本文发布于:2024-02-11 22:36:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1683978.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   OpenId

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!