HttpHandler.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Web;
  8. using System.Windows.Forms;
  9. namespace UAS_MES_NEW.PublicMethod
  10. {
  11. public class HttpHandler : IHttpHandler
  12. {
  13. /// <summary>
  14. /// 您将需要在网站的 Web.config 文件中配置此处理程序
  15. /// 并向 IIS 注册它,然后才能使用它。有关详细信息,
  16. /// 请参见下面的链接: http://go.microsoft.com/?linkid=8101007
  17. /// </summary>
  18. public bool IsReusable
  19. {
  20. // 如果无法为其他请求重用托管处理程序,则返回 false。
  21. // 如果按请求保留某些状态信息,则通常这将为 false。
  22. get { return true; }
  23. }
  24. public void ProcessRequest(HttpContext context)
  25. {
  26. }
  27. public static bool CheckUserLogin(string Sn, out string oMsg)
  28. {
  29. oMsg = "";
  30. try
  31. {
  32. string url = "http://yncharge.cn/charge-web/a/cp/chargePile/orderByLT";//html调用的地址
  33. HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
  34. if (webrequest == null)
  35. {
  36. return false;
  37. }
  38. webrequest.Method = "POST";
  39. webrequest.Timeout = 1000;
  40. webrequest.ContentType = "application/x-www-form-urlencoded";
  41. System.Collections.Hashtable pars = new System.Collections.Hashtable();
  42. pars.Add("chargePointId", "2e4b20e728f74db3a8e1455d088909d7");
  43. pars.Add("chargePileCode", Sn.Substring(Sn.Length - 9));
  44. string buffer = "";
  45. //发送POST数据
  46. if (!(pars == null || pars.Count == 0))
  47. {
  48. foreach (string key in pars.Keys)
  49. {
  50. buffer = buffer + "&" + key + "=" + pars[key].ToString();
  51. }
  52. byte[] data = Encoding.UTF8.GetBytes(buffer);
  53. using (Stream stream = webrequest.GetRequestStream())
  54. {
  55. stream.Write(data, 0, data.Length);
  56. }
  57. }
  58. string[] values = webrequest.Headers.GetValues("Content-Type");
  59. WebResponse myResponse = webrequest.GetResponse();
  60. using (Stream resStream = myResponse.GetResponseStream())//得到回写的流
  61. {
  62. StreamReader newReader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
  63. string Content = newReader.ReadToEnd();
  64. Dictionary<string, object> dic = new Dictionary<string, object>();
  65. dic = ToDictionary(Content);
  66. if (dic.ContainsKey("result"))
  67. {
  68. if (dic["result"].ToString().Contains("0"))
  69. {
  70. oMsg = "SN未测试";
  71. return false;
  72. }
  73. else if (dic["result"].ToString().Contains("2"))
  74. {
  75. oMsg = "SN测试失败";
  76. return false;
  77. }
  78. }
  79. else
  80. {
  81. oMsg = "SN不符合规范";
  82. return false;
  83. }
  84. newReader.Close();
  85. }
  86. }
  87. catch (Exception ex)
  88. {
  89. oMsg = ex.Message.ToString();
  90. return false;
  91. }
  92. return true;
  93. }
  94. public static Dictionary<string, object> ToDictionary(string JsonData)
  95. {
  96. object Data = null;
  97. Dictionary<string, object> Dic = new Dictionary<string, object>();
  98. if (JsonData.StartsWith("["))
  99. {
  100. //如果目标直接就为数组类型,则将会直接输出一个Key为List的List<Dictionary<string, object>>集合
  101. //使用示例List<Dictionary<string, object>> ListDic = (List<Dictionary<string, object>>)Dic["List"];
  102. List<Dictionary<string, object>> List = new List<Dictionary<string, object>>();
  103. MatchCollection ListMatch = Regex.Matches(JsonData, @"{[\s\S]+?}");//使用正则表达式匹配出JSON数组
  104. foreach (Match ListItem in ListMatch)
  105. {
  106. List.Add(ToDictionary(ListItem.ToString()));//递归调用
  107. }
  108. Data = List;
  109. Dic.Add("List", Data);
  110. }
  111. else
  112. {
  113. MatchCollection Match = Regex.Matches(JsonData, @"""(.+?)"": {0,1}(\[[\s\S]+?\]|null|"".+?""|-{0,1}\d*)");//使用正则表达式匹配出JSON数据中的键与值
  114. foreach (Match item in Match)
  115. {
  116. try
  117. {
  118. if (item.Groups[2].ToString().StartsWith("["))
  119. {
  120. //如果目标是数组,将会输出一个Key为当前Json的List<Dictionary<string, object>>集合
  121. //使用示例List<Dictionary<string, object>> ListDic = (List<Dictionary<string, object>>)Dic["Json中的Key"];
  122. List<Dictionary<string, object>> List = new List<Dictionary<string, object>>();
  123. MatchCollection ListMatch = Regex.Matches(item.Groups[2].ToString(), @"{[\s\S]+?}");//使用正则表达式匹配出JSON数组
  124. foreach (Match ListItem in ListMatch)
  125. {
  126. List.Add(ToDictionary(ListItem.ToString()));//递归调用
  127. }
  128. Data = List;
  129. }
  130. else if (item.Groups[2].ToString().ToLower() == "null") Data = null;//如果数据为null(字符串类型),直接转换成null
  131. else Data = item.Groups[2].ToString(); //数据为数字、字符串中的一类,直接写入
  132. Dic.Add(item.Groups[1].ToString(), Data);
  133. }
  134. catch { }
  135. }
  136. }
  137. return Dic;
  138. }
  139. }
  140. }