|
|
using System;
|
|
|
using System.Collections;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Security.Cryptography;
|
|
|
using System.Text;
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
namespace POSV
|
|
|
{
|
|
|
public abstract class AbstractRequestData
|
|
|
{
|
|
|
public readonly SortedList<string , string> NewParameters;
|
|
|
|
|
|
protected AbstractRequestData()
|
|
|
{
|
|
|
this.NewParameters = new SortedList<string , string>();
|
|
|
}
|
|
|
|
|
|
|
|
|
public string CreateMd5Sign(string key , string value)
|
|
|
{
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
//ArrayList akeys = new ArrayList(this.NewParameters.to);
|
|
|
//akeys.Sort();
|
|
|
|
|
|
foreach (string k in this.NewParameters.Keys)
|
|
|
{
|
|
|
string v = (string)this.NewParameters[k];
|
|
|
if (null != v && "".CompareTo(v) != 0
|
|
|
&& "sign".CompareTo(k) != 0
|
|
|
//&& "sign_type".CompareTo(k) != 0
|
|
|
&& "key".CompareTo(k) != 0)
|
|
|
{
|
|
|
sb.Append(k + "=" + v + "&");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
sb.Append(key + "=" + value);
|
|
|
string sign = GetMD5(sb.ToString() , Encoding.UTF8.BodyName).ToUpper();
|
|
|
|
|
|
return sign;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取大写的MD5签名结果
|
|
|
/// </summary>
|
|
|
/// <param name="encypStr">需要加密的字符串</param>
|
|
|
/// <param name="charset">编码</param>
|
|
|
/// <returns></returns>
|
|
|
public string GetMD5(string encypStr , string charset)
|
|
|
{
|
|
|
string retStr;
|
|
|
MD5CryptoServiceProvider m5 = new MD5CryptoServiceProvider();
|
|
|
|
|
|
//创建md5对象
|
|
|
byte[] inputBye;
|
|
|
byte[] outputBye;
|
|
|
|
|
|
//使用GB2312编码方式把字符串转化为字节数组.
|
|
|
try
|
|
|
{
|
|
|
inputBye = Encoding.GetEncoding(charset).GetBytes(encypStr);
|
|
|
}
|
|
|
catch
|
|
|
{
|
|
|
inputBye = Encoding.GetEncoding("GB2312").GetBytes(encypStr);
|
|
|
}
|
|
|
outputBye = m5.ComputeHash(inputBye);
|
|
|
|
|
|
retStr = BitConverter.ToString(outputBye);
|
|
|
retStr = retStr.Replace("-" , "").ToUpper();
|
|
|
return retStr;
|
|
|
}
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// 输出XML
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public string ParseXML()
|
|
|
{
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
sb.Append("<xml>");
|
|
|
foreach (string k in this.NewParameters.Keys)
|
|
|
{
|
|
|
string v = (string)this.NewParameters[k];
|
|
|
if (v != null && Regex.IsMatch(v , @"^[0-9.]$"))
|
|
|
{
|
|
|
|
|
|
sb.Append("<" + k + ">" + v + "</" + k + ">");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
sb.Append("<" + k + "><![CDATA[" + v + "]]></" + k + ">");
|
|
|
}
|
|
|
|
|
|
}
|
|
|
sb.Append("</xml>");
|
|
|
return sb.ToString();
|
|
|
}
|
|
|
}
|
|
|
}
|