using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Windows.Forms;
namespace UAS_MES_NEW.PublicMethod
{
public class HttpHandler : IHttpHandler
{
///
/// 您将需要在网站的 Web.config 文件中配置此处理程序
/// 并向 IIS 注册它,然后才能使用它。有关详细信息,
/// 请参见下面的链接: http://go.microsoft.com/?linkid=8101007
///
public bool IsReusable
{
// 如果无法为其他请求重用托管处理程序,则返回 false。
// 如果按请求保留某些状态信息,则通常这将为 false。
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
}
public static bool CheckUserLogin(string Sn, out string oMsg)
{
oMsg = "";
try
{
string url = "http://yncharge.cn/charge-web/a/cp/chargePile/orderByLT";//html调用的地址
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
if (webrequest == null)
{
return false;
}
webrequest.Method = "POST";
webrequest.Timeout = 1000;
webrequest.ContentType = "application/x-www-form-urlencoded";
System.Collections.Hashtable pars = new System.Collections.Hashtable();
pars.Add("chargePointId", "2e4b20e728f74db3a8e1455d088909d7");
pars.Add("chargePileCode", Sn.Substring(Sn.Length - 9));
string buffer = "";
//发送POST数据
if (!(pars == null || pars.Count == 0))
{
foreach (string key in pars.Keys)
{
buffer = buffer + "&" + key + "=" + pars[key].ToString();
}
byte[] data = Encoding.UTF8.GetBytes(buffer);
using (Stream stream = webrequest.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}
string[] values = webrequest.Headers.GetValues("Content-Type");
WebResponse myResponse = webrequest.GetResponse();
using (Stream resStream = myResponse.GetResponseStream())//得到回写的流
{
StreamReader newReader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
string Content = newReader.ReadToEnd();
Dictionary dic = new Dictionary();
dic = ToDictionary(Content);
if (dic.ContainsKey("result"))
{
if (dic["result"].ToString().Contains("0"))
{
oMsg = "SN未测试";
return false;
}
else if (dic["result"].ToString().Contains("2"))
{
oMsg = "SN测试失败";
return false;
}
}
else
{
oMsg = "SN不符合规范";
return false;
}
newReader.Close();
}
}
catch (Exception ex)
{
oMsg = ex.Message.ToString();
return false;
}
return true;
}
public static Dictionary ToDictionary(string JsonData)
{
object Data = null;
Dictionary Dic = new Dictionary();
if (JsonData.StartsWith("["))
{
//如果目标直接就为数组类型,则将会直接输出一个Key为List的List>集合
//使用示例List> ListDic = (List>)Dic["List"];
List> List = new List>();
MatchCollection ListMatch = Regex.Matches(JsonData, @"{[\s\S]+?}");//使用正则表达式匹配出JSON数组
foreach (Match ListItem in ListMatch)
{
List.Add(ToDictionary(ListItem.ToString()));//递归调用
}
Data = List;
Dic.Add("List", Data);
}
else
{
MatchCollection Match = Regex.Matches(JsonData, @"""(.+?)"": {0,1}(\[[\s\S]+?\]|null|"".+?""|-{0,1}\d*)");//使用正则表达式匹配出JSON数据中的键与值
foreach (Match item in Match)
{
try
{
if (item.Groups[2].ToString().StartsWith("["))
{
//如果目标是数组,将会输出一个Key为当前Json的List>集合
//使用示例List> ListDic = (List>)Dic["Json中的Key"];
List> List = new List>();
MatchCollection ListMatch = Regex.Matches(item.Groups[2].ToString(), @"{[\s\S]+?}");//使用正则表达式匹配出JSON数组
foreach (Match ListItem in ListMatch)
{
List.Add(ToDictionary(ListItem.ToString()));//递归调用
}
Data = List;
}
else if (item.Groups[2].ToString().ToLower() == "null") Data = null;//如果数据为null(字符串类型),直接转换成null
else Data = item.Groups[2].ToString(); //数据为数字、字符串中的一类,直接写入
Dic.Add(item.Groups[1].ToString(), Data);
}
catch { }
}
}
return Dic;
}
}
}