学习记录
2018-11-22 08:44:07来源:博客园 阅读 ()
持续更新
1. cache
2. 获取微信AccessToken
3. 获取微信小程序码
4. 获取微信AccessToken(使用HttpClient)
5. 获取微信小程序码(使用HttpClient)
Cache
推荐博客:细说 ASP.NET Cache 及其高级用法
1 #region 缓存 2 3 /// <summary> 4 /// 设置Cache 5 /// </summary> 6 /// <param name="key">cache名称</param> 7 /// <param name="value">cache值</param> 8 void SetCacheData(string key, string value,DateTime time) 9 { 10 Cache cache = HttpContext.Current.Cache; 11 cache["test"] = 1; 12 cache.Insert(key, value, null, time, Cache.NoSlidingExpiration); 13 } 14 15 /// <summary> 16 /// 获取 cache 17 /// </summary> 18 /// <param name="key">cache值</param> 19 /// <returns></returns> 20 object GetCacheData(string key) 21 { 22 Cache cache = HttpRuntime.Cache; 23 if (cache[key] != null) 24 { 25 return cache[key]; 26 } 27 else 28 { 29 return ""; 30 } 31 } 32 33 /// <summary> 34 /// 判断指定Cache是否存在 35 /// </summary> 36 /// <param name="key"></param> 37 /// <returns></returns> 38 bool IsCacheExits(string key) 39 { 40 Cache s = HttpRuntime.Cache; 41 return s[key] != null; 42 } 43 44 /// <summary> 45 /// 移除指定Cache 46 /// </summary> 47 /// <param name="key">cache名称</param> 48 void RemoveCacheData(string key) 49 { 50 Cache cache = HttpRuntime.Cache; 51 cache.Remove(key); 52 } 53 54 /// <summary> 55 /// 移除所有cache 56 /// </summary> 57 void RemoveAllCacheData() 58 { 59 Cache cache = HttpRuntime.Cache; 60 IDictionaryEnumerator cachenEunm = cache.GetEnumerator(); 61 while (cachenEunm.MoveNext()) 62 { 63 cache.Remove(cachenEunm.Key.ToString()); 64 } 65 } 66 #endregion
获取微信Access_Token
1 class WxResult 2 { 3 public string access_token { get; set; } 4 public string expires_in { get; set; } 5 } 6 7 8 /// <summary> 9 /// 缓存的是整个微信返回的Json,可以转换成对象获取Access_Token 10 /// </summary> 11 /// <returns></returns> 12 string GetAccessToken() 13 { 14 var appid = ""; 15 var appresect = ""; 16 //判断Access_Token是否存在 17 if (IsCacheExits("Access_Token")) 18 { 19 return GetCacheData("Access_Token").ToString(); 20 } 21 else 22 { 23 var url = 24 $"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appid}&secret={appresect}"; 25 var accesstoken = HttpGetValue(url); 26 SetCacheData("Access_Token",accesstoken,DateTime.Now.AddHours(2));//这里缓存的是整个对象 27 return accesstoken; 28 } 29 } 30 31 /// <summary> 32 /// Get方式请求 获取微信返回的json 33 /// </summary> 34 /// <param name="url"></param> 35 /// <returns></returns> 36 string HttpGetValue(string url) 37 { 38 //创建一个HttpWebRequest 39 HttpWebRequest webRequest = WebRequest.CreateHttp(url); 40 //GET 方式传输 41 webRequest.Method = "GET"; 42 //获取返回 43 WebResponse webResponse = webRequest.GetResponse(); 44 //从StramReader中读取返回的流 45 string streamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8).ReadToEnd(); 46 return streamReader; 47 } 48 49 //调用 50 var accesstoken = GetAccessToken(); 51 //需要引用Newtonsoft.Json 52 var wxRes = JsonConvert.DeserializeObject<WxResult>(accesstoken); 53 accesstoken = wxRes.access_token;
获取微信小程序码
1 void HtppPostValue(string accessToken) 2 { 3 var url = $"https://api.weixin.qq.com/wxa/getwxacode?access_token={accessToken}"; 4 HttpWebRequest webRequest = WebRequest.CreateHttp(url); 5 webRequest.Method = "POST"; 6 string str = "{\"path\":\" path\",\"width\":1000}"; 7 byte[] data = Encoding.UTF8.GetBytes(str); 8 webRequest.ContentLength = data.Length; 9 Stream outStream = webRequest.GetRequestStream(); //写入的流 10 outStream.Write(data, 0, data.Length); 11 outStream.Close(); 12 HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse; 13 Stream insStream = response.GetResponseStream(); 14 var imgName = DateTime.Now.Millisecond + ".jpg"; 15 var path = MapPath("/Content/" + imgName); 16 Image img = Image.FromStream(insStream); 17 img.Save(path); 18 }
获取微信Access_Token 使用HttpClient
1 string GetAccessTokenWithHttpClient() 2 { 3 var appid = "自己AppID"; 4 var appresect = "自己Appresect"; 5 //实例化HttpClient 对象 6 HttpClient client = new HttpClient(); 7 var url = 8 $"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appid}&secret={appresect}"; 9 //使用GetAsybc(string url)方法异步获取HttpResponseMessage .Result使用的是同步方法 10 HttpResponseMessage responeseMessage = client.GetAsync(url).Result; 11 client.Dispose(); 12 //返回的是一个Json对象 13 return responeseMessage.Content.ReadAsStringAsync().Result; 14 }
获取微信小程序码 使用HttpClient
1 void PostAccessTokenWithHttpClient(string accessToken) 2 { 3 var url = $"https://api.weixin.qq.com/wxa/getwxacode?access_token={accessToken}"; 4 string str = "{\"path\":\" path\",\"width\":1000}"; 5 //实例化HttpClient 对象 6 HttpClient client = new HttpClient(); 7 // 把需要传输的转换HttpContent 对象 8 HttpContent content = new StringContent(str,Encoding.UTF8); 9 //使用PostAsync(string url, HtppContent content).Result 获取返回的响应消息 10 HttpResponseMessage response = client.PostAsync(url, content).Result; 11 //从响应消息中获取返回的信息 使用Result 同步方法 12 Stream stream = response.Content.ReadAsStreamAsync().Result; 13 //二进制流转换成Img 14 using (Image img = Image.FromStream(stream)) 15 { 16 var imgName ="123哈哈"+ DateTime.Now.Millisecond + ".jpg"; 17 var path = MapPath("/Content/" + imgName); 18 img.Save(path); 19 } 20 21 }
FriendlyUrl 友好Url
推荐博客:如何在Asp.Net WebForm使用FriendsUrls
1 void Application_Start(object sender, EventArgs e) 2 { 3 // 在应用程序启动时运行的代码 4 var settings = new FriendlyUrlSettings(); 5 //执行重定向得模式 枚举 6 settings.AutoRedirectMode = RedirectMode.Temporary; 7 //启用友好Url 8 routes.EnableFriendlyUrls(settings); 9 } 10 11 protected void Page_Load(object sender, EventArgs e) 12 { 13 var path = Request.GetFriendlyUrlFileVirtualPath();//返回虚拟路径 14 var ext = Request.GetFriendlyUrlFileExtension();//返回物理文件扩展名 15 IList<string> str = Request.GetFriendlyUrlSegments();//获取url得参数,一个泛型 16 Response.Write($"虚拟路径:{path};物理文件路径扩展名:{ext}<br />"); 17 if (str.Count > 0) 18 { 19 foreach (string s in str) 20 { 21 Response.Write($"参数:{s}"); 22 } 23 24 } 25 26 string friendUrl = FriendlyUrl.Resolve(path);//解析处理成友好url虚拟路径 27 Response.Write($"友好路径:{friendUrl}<br />"); 28 string s1 = FriendlyUrl.Href("~/RoutePage", "1", "22"); 29 Response.Write(s1); 30 }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:ASPNET npoi帮助类
- asp.net定点执行任务的方法 2020-03-18
- 用继承方法实现页面判断session的方法 2020-03-10
- asp.net基础学习之前端页面布局 2019-11-30
- ASP.NET连接 Access数据库的几种方法 2019-11-29
- asp.net基础学习:控件的使用方法 2019-11-19
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash