using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Net; using System.IO; namespace UnitTestProject1 { [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { //var a = JSWX.Areas.Common.Context.AccessToken; //var b = HttpGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx4073bc94413ffb9c&secret=c5ecc639370a0baff93f2126db8f3ede"); var a = "12_S9gAnoYS3bd3jpZLNhm6phPnVh3jYql349cahxD3fq87nQpFNM9Sc3ZpGet15k0ON_4LL0sxgzBwiRR8jBK9Y3IttEM9_K3TbvixGZoAtc5jo06sjPPAm0DML110JULecXkVW80cBQy9ja9wMJQdCCAAXE"; //var jsapi_ticket = HttpGet("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + a + "&type=jsapi"); //var jsapi_ticket = "sM4AOVdWfPE4DxkXGEs8VJCzIh7-MBAfNCnykb_3eigM5ISf8MuW-wxWBmeO4qFovF4-QgLVOf5bien_5LG-Kw"; var jsapi_ticket = "sM4AOVdWfPE4DxkXGEs8VJCzIh7-MBAfNCnykb_3eiiJsrzstXOV1HKc6iUGvMGeq6dQp1PVNtjC8on2ZJvINw"; var k=ConvertDateTimeToInt(DateTime.Now); Dictionary dic = new Dictionary(); //dic.Add("debug", "true"); //dic.Add("appId", "wx4073bc94413ffb9c"); //dic.Add("timestamp", k.ToString().Substring(0,k.ToString().Length - 3)); //dic.Add("nonceStr", "YT" + k.ToString()); //dic.Add("jsApiList", "['checkJsApi', 'scanQRCode']"); dic.Add("noncestr", "YT" + k.ToString()); dic.Add("jsapi_ticket", jsapi_ticket); dic.Add("timestamp", k.ToString()); dic.Add("url", "http://www.63191999.com/Business/Scan.html"); System.Diagnostics.Debug.WriteLine(k.ToString()); //var dicSort = from objDic in dic orderby dic.Keys descending select dic; var dicSort=dic.OrderBy(t => t.Key); var s = ""; foreach (var item in dicSort) { System.Diagnostics.Debug.WriteLine(item.Key); s += item.Key + "=" + item.Value+"&"; } if (s.EndsWith("&")) s = s.Substring(0, s.Length - "&".Length); System.Diagnostics.Debug.WriteLine(s); s = SHA1(s, Encoding.UTF8); System.Diagnostics.Debug.WriteLine(s); dic.Clear(); dic.Add("jsapi_ticket", jsapi_ticket); dic.Add("noncestr", "YT1532926066256"); dic.Add("timestamp", "1532926066"); dic.Add("url", "http://www.63191999.com/Business/Scan.html"); s = ""; foreach (var item in dicSort) { System.Diagnostics.Debug.WriteLine(item.Key); s += item.Key + "=" + item.Value + "&"; } if (s.EndsWith("&")) s = s.Substring(0, s.Length - "&".Length); s = SHA1(s, Encoding.UTF8); System.Diagnostics.Debug.WriteLine(s); } public static long ConvertDateTimeToInt(System.DateTime time) { System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0)); long t = (time.Ticks - startTime.Ticks) / 10000; //除10000调整为13位 return t; } /// /// SHA1 加密,返回大写字符串 /// /// 需要加密字符串 /// 指定加密编码 /// 返回40位大写字符串 public string SHA1(string content, Encoding encode) { try { SHA1 sha1 = new SHA1CryptoServiceProvider(); byte[] bytes_in = encode.GetBytes(content); byte[] bytes_out = sha1.ComputeHash(bytes_in); sha1.Dispose(); string result = BitConverter.ToString(bytes_out); result = result.Replace("-", ""); return result.ToLower(); } catch (Exception ex) { throw new Exception("SHA1加密出错:" + ex.Message); } } [TestMethod] public void GetAcc() { //var ret = HttpGet("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wx4073bc94413ffb9c&corpsecret=Ai3aTpYl5XZL6qp0HwJTeIKlCqPo8GAP4UEApQyn314"); var ret = HttpGet("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wx4073bc94413ffb9c&corpsecret=c5ecc639370a0baff93f2126db8f3ede"); ret = ""; } public static string HttpGet(string url) { string result=string.Empty; try { HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(url); wbRequest.Method = "GET"; HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse(); using (Stream responseStream = wbResponse.GetResponseStream()) { using (StreamReader sReader = new StreamReader(responseStream)) { result = sReader.ReadToEnd(); } } } catch (Exception ex) { } return result; } [TestMethod] public void GetNickName() { try { var b = HttpGet(string.Format("https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}", "", "oYGxtwfgwr23fx3N04H2ZxY7ePqk&lang=zh_CN")); b = ""; } catch (Exception ex) { var s = ex.Message; } } } }