1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
| public class WeiXinController : Controller
{
public static readonly string appid =
System.Web.Configuration.WebConfigurationManager.AppSettings["wxappid"];
public static readonly string secret =
System.Web.Configuration.WebConfigurationManager.AppSettings["wxsecret"];
public static readonly bool isDedug =
System.Web.Configuration.WebConfigurationManager.AppSettings["IsDebug"] =="true";
public static string _ticket = "";
public static DateTime _lastTimestamp;
public ActionResult Info(string url,string noncestr)
{
if (string.IsNullOrEmpty(_ticket) ||
_lastTimestamp == null || (_lastTimestamp - DateTime.Now).Milliseconds > 7200)
{
var resultString = HTTPHelper.GetHTMLByURL("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
+ appid + "&secret=" + secret);
dynamic resultValue = JsonConvert.DeserializeObject<dynamic>(resultString);
if (resultValue == null || resultValue.access_token == null
|| resultValue.access_token.Value == null)
{
return Json(new { issuccess = false,
error = "获取token失败" });
}
var token = resultValue.access_token.Value;
resultString = HTTPHelper.GetHTMLByURL
("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" +
token + "&type=jsapi");
dynamic ticketValue = JsonConvert.DeserializeObject<dynamic>(resultString);
if (ticketValue == null || ticketValue.errcode == null
|| ticketValue.errcode.Value != 0 || ticketValue.ticket == null)
return Json(new { issuccess = false,
error = "获取ticketValue失败" });
_ticket = ticketValue.ticket.Value;
_lastTimestamp = DateTime.Now;
var timestamp = GetTimeStamp();
var hexString = string.Format("jsapi_ticket={0}&noncestr={3}×tamp={1}&url={2}",
_ticket, timestamp, url,noncestr);
return Json(new {
issuccess = true,
sha1value = GetSHA1Value(hexString),
timestamp = timestamp,
url = url,
appid = appid,
debug=isDedug,
tiket=_ticket
});
}
else
{
var timestamp = GetTimeStamp();
var hexString = string.Format("jsapi_ticket={0}&noncestr=1234567890123456×tamp={1}&url={2}",
_ticket, timestamp, url);
return Json(new {
issuccess = true, sha1value = GetSHA1Value(hexString),
timestamp = timestamp, url = url,
appid = appid, debug = isDedug,tiket = _ticket
});
}
}
private string GetSHA1Value(string sourceString)
{
var hash = SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(sourceString));
return string.Join("",
hash.Select(b => b.ToString("x2")).ToArray());
}
private static string GetTimeStamp()
{
TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalSeconds).ToString();
}
}
public class HTTPHelper
{
public static string GetHTMLByURL(string url)
{
string htmlCode = string.Empty;
try
{
HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
webRequest.Timeout = 30000;
webRequest.Method = "GET";
webRequest.UserAgent = "Mozilla/4.0";
webRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
HttpWebResponse webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse();
//获取目标网站的编码格式
string contentype = webResponse.Headers["Content-Type"];
Regex regex = new Regex("charset\\s*=\\s*[\\W]?\\s*([\\w-]+)", RegexOptions.IgnoreCase);
if (webResponse.ContentEncoding.ToLower() == "gzip")//如果使用了GZip则先解压
{
using (System.IO.Stream streamReceive = webResponse.GetResponseStream())
{
using (var zipStream = new System.IO.Compression.GZipStream(streamReceive, System.IO.Compression.CompressionMode.Decompress))
{
//匹配编码格式
if (regex.IsMatch(contentype))
{
Encoding ending = Encoding.GetEncoding
(regex.Match(contentype).Groups[1].Value.Trim());
using (StreamReader sr = new System.IO.StreamReader(zipStream, ending))
{
htmlCode = sr.ReadToEnd();
}
}
else
{
using (StreamReader sr = new System.IO.StreamReader(zipStream, Encoding.UTF8))
{
htmlCode = sr.ReadToEnd();
}
}
}
}
}
else
{
using (System.IO.Stream streamReceive = webResponse.GetResponseStream())
{
var encoding = Encoding.Default;
if (contentype.Contains("utf"))
encoding = Encoding.UTF8;
using (System.IO.StreamReader sr = new System.IO.StreamReader(streamReceive, encoding))
{
htmlCode = sr.ReadToEnd();
}
}
}
return htmlCode;
}
catch (Exception ex)
{
return "";
}
}
}
|