|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Net;
|
|
|
using System.Net.Security;
|
|
|
using System.Security.Cryptography.X509Certificates;
|
|
|
using System.Text;
|
|
|
|
|
|
namespace Common
|
|
|
{
|
|
|
public static class CommonTool
|
|
|
{
|
|
|
|
|
|
|
|
|
#region 客户端请求参数验证
|
|
|
|
|
|
/// <summary>
|
|
|
/// 参数按照参数名ASCII码从小到大排序(字典序)
|
|
|
/// </summary>
|
|
|
/// <param name="paramsMap"></param>
|
|
|
/// <returns></returns>
|
|
|
public static string GetParamSrc(Dictionary<string, string> paramsMap)
|
|
|
{
|
|
|
|
|
|
var vDic = paramsMap.OrderBy(x => x.Key, new ComparerString()).ToDictionary(x => x.Key, y => y.Value);
|
|
|
|
|
|
StringBuilder str = new StringBuilder();
|
|
|
|
|
|
foreach (KeyValuePair<string, string> kv in vDic)
|
|
|
{
|
|
|
string pkey = kv.Key;
|
|
|
string pvalue = kv.Value;
|
|
|
str.Append(pkey + "=" + pvalue + "&");
|
|
|
}
|
|
|
|
|
|
string result = str.ToString().Substring(0, str.ToString().Length - 1);
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
public class ComparerString : IComparer<String>
|
|
|
{
|
|
|
public int Compare(String x, String y)
|
|
|
{
|
|
|
return string.CompareOrdinal(x, y);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// 对客户端上传的参数进行MD5效验
|
|
|
/// </summary>
|
|
|
/// <param name="MD5Str"></param>
|
|
|
/// <param name=""></param>
|
|
|
/// <returns></returns>
|
|
|
public static bool ValidationMD5(string MD5Str, Dictionary<string, string> dics, string fileName)
|
|
|
{
|
|
|
string stringA = GetParamSrc(dics);
|
|
|
|
|
|
string stringSignTmp = stringA + "&filename=" + fileName;
|
|
|
string signValue = Encryption.MD5Encrypt(stringSignTmp).ToUpper();
|
|
|
return signValue == MD5Str;
|
|
|
}
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
public static string GetNotityReqFileName(NotityReqType type)
|
|
|
{
|
|
|
|
|
|
if (type == NotityReqType.VIU)
|
|
|
{
|
|
|
return "NOTIFY_VIUPLOAD_REQ_15_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".json";
|
|
|
}
|
|
|
else if (type == NotityReqType.VIPU)
|
|
|
{
|
|
|
return "NOTIFY_VIPUPLOAD_REQ_15_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".json";
|
|
|
}
|
|
|
else if (type == NotityReqType.ETCTU)
|
|
|
{
|
|
|
return "NOTIFY_ETCTUPLOAD_REQ_15_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".json";
|
|
|
}
|
|
|
else if (type == NotityReqType.HOURSUM)
|
|
|
{
|
|
|
return "NOTIFY_HOURSUMUPLOAD_REQ_15_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".json";
|
|
|
}
|
|
|
else if (type == NotityReqType.LOG)
|
|
|
{
|
|
|
return "NOTIFY_LOGBUPLOAD_REQ_15_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".json";
|
|
|
}
|
|
|
else if (type == NotityReqType.PIC)
|
|
|
{
|
|
|
return "NOTIFY_PICREALTRANSFER_REQ_15_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".json";
|
|
|
}
|
|
|
else if (type == NotityReqType.GetError)
|
|
|
{
|
|
|
return "NOTIFY_GETERRORDATA_REQ_15_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".json";
|
|
|
}
|
|
|
else if (type == NotityReqType.FixError)
|
|
|
{
|
|
|
return "NOTIFY_FIXERRORDATA_REQ_15_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".json";
|
|
|
}
|
|
|
return "";
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static string GetNotityTableName(NotityReqType type)
|
|
|
{
|
|
|
if (type == NotityReqType.VIU)
|
|
|
{
|
|
|
return "GANTRY_NOTIFY_VIU";
|
|
|
}
|
|
|
else if (type == NotityReqType.VIPU)
|
|
|
{
|
|
|
return "GANTRY_NOTIFY_VIPU";
|
|
|
}
|
|
|
else if (type == NotityReqType.ETCTU)
|
|
|
{
|
|
|
return "GANTRY_NOTIFY_ETCT";
|
|
|
}
|
|
|
else if (type == NotityReqType.HOURSUM)
|
|
|
{
|
|
|
return "GANTRY_NOTIFY_HOURSUM";
|
|
|
}
|
|
|
else if (type == NotityReqType.LOG)
|
|
|
{
|
|
|
return "GANTRY_NOTIFY_LOG";
|
|
|
}
|
|
|
else if (type == NotityReqType.PIC)
|
|
|
{
|
|
|
return "GANTRY_NOTIFY_PIC";
|
|
|
}
|
|
|
return "";
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 生成单个随机数字
|
|
|
/// </summary>
|
|
|
private static int createNum()
|
|
|
{
|
|
|
Random random = new Random(Guid.NewGuid().GetHashCode());
|
|
|
int num = random.Next(10);
|
|
|
return num;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 生成单个大写随机字母
|
|
|
/// </summary>
|
|
|
private static string createBigAbc()
|
|
|
{
|
|
|
//A-Z的 ASCII值为65-90
|
|
|
Random random = new Random(Guid.NewGuid().GetHashCode());
|
|
|
int num = random.Next(65, 91);
|
|
|
string abc = Convert.ToChar(num).ToString();
|
|
|
return abc;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 生成单个小写随机字母
|
|
|
/// </summary>
|
|
|
private static string createSmallAbc()
|
|
|
{
|
|
|
//a-z的 ASCII值为97-122
|
|
|
Random random = new Random(Guid.NewGuid().GetHashCode());
|
|
|
int num = random.Next(97, 123);
|
|
|
string abc = Convert.ToChar(num).ToString();
|
|
|
return abc;
|
|
|
}
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// 生成随机字符串
|
|
|
/// </summary>
|
|
|
/// <param name="length">字符串的长度</param>
|
|
|
/// <returns></returns>
|
|
|
public static string CreateRandomString(int length)
|
|
|
{
|
|
|
// 创建一个StringBuilder对象存储密码
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
//使用for循环把单个字符填充进StringBuilder对象里面变成14位密码字符串
|
|
|
for (int i = 0; i < length; i++)
|
|
|
{
|
|
|
Random random = new Random(Guid.NewGuid().GetHashCode());
|
|
|
//随机选择里面其中的一种字符生成
|
|
|
switch (random.Next(3))
|
|
|
{
|
|
|
case 0:
|
|
|
//调用生成生成随机数字的方法
|
|
|
sb.Append(createNum());
|
|
|
break;
|
|
|
case 1:
|
|
|
//调用生成生成随机小写字母的方法
|
|
|
sb.Append(createSmallAbc());
|
|
|
break;
|
|
|
case 2:
|
|
|
//调用生成生成随机大写字母的方法
|
|
|
sb.Append(createBigAbc());
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
return sb.ToString();
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void HttpsCert()
|
|
|
{
|
|
|
ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate;
|
|
|
}
|
|
|
|
|
|
private static bool RemoteCertificateValidate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
|
|
|
{
|
|
|
// Console.WriteLine("Warning, trust any certificate");
|
|
|
//为了通过证书验证,总是返回true
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///// <summary>
|
|
|
///// 发送数据到服务端 HttpResponseMessage
|
|
|
///// </summary>
|
|
|
///// <param name="json"></param>
|
|
|
///// <param name="fileName"></param>
|
|
|
///// <returns></returns>
|
|
|
//public static ResponseResul PostInterface(string json, string fileName, int gzip, string urlS, string ipAddress)
|
|
|
//{
|
|
|
|
|
|
|
|
|
// try
|
|
|
// {
|
|
|
|
|
|
// ///读取服务器地址等信息
|
|
|
// // ServiceConfig sc = ReadXML.GetServiceConfig();
|
|
|
|
|
|
// Uri InterfaceAddress = new Uri(ipAddress + urlS + fileName);
|
|
|
// // Uri InterfaceAddress = new Uri("http://192.168.10.5:1111/test3.aspx");
|
|
|
|
|
|
// byte[] buffer = null;
|
|
|
|
|
|
// if (gzip == 0)
|
|
|
// {
|
|
|
// buffer = Encoding.UTF8.GetBytes(json);
|
|
|
|
|
|
// }
|
|
|
// else if (gzip == 1)
|
|
|
// {
|
|
|
// // buffer = Common.JsonToByte(json, sc.TempPath, fileName);
|
|
|
// buffer = CommonTool.JsonToByte(json, fileName);
|
|
|
// }
|
|
|
|
|
|
// string binfile_md5 = Encryption.MD5Encrypt(buffer);
|
|
|
|
|
|
// CommonTool.HttpsCert();//跳过https证书
|
|
|
// HttpClient client = new HttpClient();
|
|
|
// client.Timeout = new TimeSpan(0, 0, 30);
|
|
|
|
|
|
// // 添加头
|
|
|
// client.DefaultRequestHeaders.Add("binfile-md5", binfile_md5);
|
|
|
// client.DefaultRequestHeaders.Add("binfile-gzip", gzip == 1 ? "true" : "false");
|
|
|
|
|
|
// client.DefaultRequestHeaders.Add("Accept", "application/json,application/zip");
|
|
|
|
|
|
// client.DefaultRequestHeaders.Add("Host", InterfaceAddress.Host + (InterfaceAddress.IsDefaultPort ? "" : ":" + InterfaceAddress.Port));
|
|
|
|
|
|
// // 处理POST体
|
|
|
// MultipartFormDataContent postContent = new MultipartFormDataContent();
|
|
|
|
|
|
// // 添加Form
|
|
|
// ByteArrayContent dataContent = new ByteArrayContent(Encoding.UTF8.GetBytes(fileName));
|
|
|
// dataContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { Name = "filename" };
|
|
|
// postContent.Add(dataContent);
|
|
|
|
|
|
// // 添加文件
|
|
|
// postContent.Add(new ByteArrayContent(buffer), "binFile", fileName);
|
|
|
|
|
|
// Stopwatch st = new Stopwatch();
|
|
|
// st.Start();//开始计时
|
|
|
|
|
|
// HttpResponseMessage postResult = client.PostAsync(InterfaceAddress.ToString(), postContent).Result;//发送消息给服务端
|
|
|
|
|
|
// st.Stop();//终止计时
|
|
|
|
|
|
// LogCommon.WriteLogNormalTS("执行用时:" + st.ElapsedMilliseconds.ToString() + "毫秒");
|
|
|
|
|
|
// ResponseResul rr = new ResponseResul();
|
|
|
// //返回json
|
|
|
|
|
|
// ///读取返回的Json文件的内容
|
|
|
// byte[] b = postResult.Content.ReadAsByteArrayAsync().Result;
|
|
|
|
|
|
|
|
|
// string resultFileContent = System.Text.Encoding.UTF8.GetString(b);
|
|
|
|
|
|
// //// 发送POST
|
|
|
// //if (Convert.ToInt32(postResult.StatusCode) >= 700)
|
|
|
// //{
|
|
|
// // if (Convert.ToInt32(postResult.StatusCode) != 707)
|
|
|
// // {
|
|
|
// // LogCommon.WriteLogNormalTS("错误编码:" + postResult.StatusCode + "错误信息:" + resultFileContent);
|
|
|
|
|
|
// // return null;
|
|
|
// // }
|
|
|
// // else
|
|
|
// // {
|
|
|
// // rr.Info = "上传成功:" + resultFileContent;
|
|
|
// // return rr;
|
|
|
// // }
|
|
|
// //}
|
|
|
|
|
|
// ////返回的MD5
|
|
|
// //string resultMd5 = ((string[])((postResult.Headers.GetValues("binfile-md5"))))[0];
|
|
|
// ////返回的数据长度
|
|
|
// //string resultLength = ((string[])((postResult.Content.Headers.GetValues("Content-Length"))))[0];
|
|
|
|
|
|
// //string newMd5 = Common.StrToMD5(resultFileContent, Convert.ToInt32(resultLength));
|
|
|
// rr = new JavaScriptSerializer().Deserialize<ResponseResul>(resultFileContent);
|
|
|
|
|
|
|
|
|
|
|
|
// //解析返回的JSon串
|
|
|
// return rr;
|
|
|
// }
|
|
|
// catch (Exception ex)
|
|
|
// {
|
|
|
// LogCommon.WriteLogNormalTS(ex.Message);
|
|
|
|
|
|
// return null;
|
|
|
// }
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|