在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1.获取微信访问凭证 public static string GetAccessToken() { string _AccessToken = "";//存储微信访问凭证 DateTime _lastGetTimeOfAccessToken;//ACCESS_TOKEN最后一次更新时间 string corpId = "XXXXX";// 填入企业的corpId string corpsecret = "XXXX";// 填入Secret try { string sqlstring = "SELECT count(*) FROM [Booking].[dbo].[WX_GetAccessCredentials] " + " WHERE WX_LastGetTimeOfAccessToken> GETDATE(); "; DataTable dt = DBUtility.DbHelperSQL.Query(sqlstring); if (dt.Rows[0][0].ToString() == "0") //_lastGetTimeOfAccessToken < DateTime.Now { string serviceAddress = string.Format("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}", corpId, corpsecret); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress); request.Method = "GET"; request.ContentType = "text/html;charset=UTF-8"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); var rsEntity = new { access_token = "", expires_in = 0, errcode = 0, errmsg = "" }; dynamic en = Newtonsoft.Json.JsonConvert.DeserializeAnonymousType<object>(retString, rsEntity); // Newtonsoft.Json提供的匿名类反序列化 _lastGetTimeOfAccessToken = DateTime.Now.AddSeconds((double)en.expires_in - 300); _AccessToken = en.access_token;//GetAccessToken string sqlstring1 = "insert into [Booking].[dbo].[WX_GetAccessCredentials] ([WX_LastGetTimeOfAccessToken],[WX_AccessToken],[Create_Date])" + "VALUES('" + _lastGetTimeOfAccessToken + "','" + _AccessToken + "',GETDATE())"; var result = DBUtility.DbHelperSQL.ExecuteSql(sqlstring1); } string sqlstring3 = "SELECT [WX_LastGetTimeOfAccessToken],[WX_AccessToken],[Create_Date] FROM [Booking].[dbo].[WX_GetAccessCredentials] " + " WHERE WX_LastGetTimeOfAccessToken> GETDATE(); "; DataTable dt3 = DBUtility.DbHelperSQL.Query(sqlstring3); _AccessToken = dt3.Rows[0][1].ToString(); return _AccessToken; } catch (Exception ex) { throw ex; } } 2.将某成员信息用GET请求方式导出,抓取avatar属性值——头像url,图片保存在项目下 //获取图片路径 var t1 = retString.IndexOf("http://"); var t2 = retString.IndexOf("\",\"status\""); retString = retString.Substring(t1, t2 - t1); //下载图片 string newname = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg"; string datePath = System.Web.HttpContext.Current.Server.MapPath("/") + "Upload/Test/" + newname; using (System.Net.WebClient wc = new System.Net.WebClient()) { wc.Headers.Add("User-Agent", "Chrome"); wc.DownloadFile(retString, @"" + datePath + ""); //wc.DownloadFile(retString, @"d:\mobile.gif");//保存到本地的文件名和路径,请自行更改 } 3.获取图片路径,使用WebClient类来上传文件,并读取返回值media_id //图片处理 string PhotoName = "1.jpg"; string filepath= System.Web.HttpContext.Current.Server.MapPath("/") + "Upload/Test/" + PhotoName; MediaType mt = MediaType.image; string media_id = WxUpLoad(filepath, mt); 4.using的三种用法: //1.在文件顶部引用命名空间 //2.为命名空间或类型定义别名 //3.使用using,定义范围,在该范围结束时回收资源
备注:代码暂不完美,只是学习记录一下,欢迎大佬提意见,请勿喷,谢谢!
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论