using System.Drawing;
using System.Text;
using System;
using static System.Net.Mime.MediaTypeNames;
using System.Reflection;
using System.Xml;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Linq;
Console.Write("开始:");
UTF8LengthPadding.Main();
Console.ReadLine();
public class UTF8LengthPadding
{
public static void Main()
{
//秘钥
string appsecret = "12345";
//参数类
Parm parm = new Parm();
//存放参数数据的集合
Dictionary<string, string> dic = new Dictionary<string, string>();
//循环类 添加到集合中
Type myEntityType = parm.GetType();
PropertyInfo[] properties = myEntityType.GetProperties();
foreach (PropertyInfo property in properties)
{
string propertyName = property.Name;
object propertyValue = property.GetValue(parm);
dic.Add(propertyName, propertyValue.ToString());
}
//对key进行排序
var sortedKeys = dic.OrderBy(kvp => kvp.Key).ToDictionary(kvp => kvp.Key);
//拼接结果
string prestr = "";
//最终结果
string finalStr = "";
foreach (var item in sortedKeys.Keys)
{
//key的长度
int keyLength = item.Length;
//不足两位补0
string key = keyLength.ToString("00");
//拼接
prestr = prestr + key;
//组装key值
prestr = prestr + "-" + item + ":";
//组装value长度
string valueStr = dic[item];
//value长度
int valueLength = valueStr.Length;
//不足四位补0
string value = valueLength.ToString("0000");
//拼接
prestr = prestr + value;
//组装value
prestr = prestr + "-" + valueStr + ";";
}
//去掉最后一个;
if (prestr.Length > 0)
{
prestr = prestr.Substring(0, prestr.Length - 1);
}
//第三步 计算sign md5加密后,返回的是32位小写的MD5值
finalStr = prestr + appsecret;
Console.WriteLine(finalStr); // 输出
Console.WriteLine(GetMD5_32(finalStr)); // 输出
}
/// <summary>
/// 获得32位的MD5加密
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string GetMD5_32(string input)
{
System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] data = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(input));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sb.Append(data[i].ToString("x2"));
}
return sb.ToString();
}
}
public class Parm
{
public string appkey { get; set; } = "test2-xx";
public string page_no { get; set; } = "0";
public string end_time { get; set; } = "2016-08-01 13:00:00";
public string start_time { get; set; } = "2016-08-01 12:00:00";
public string page_size { get; set; } = "40";
public string sid { get; set; } = "test2";
public string timestamp { get; set; } = "1470042310";
}
标签:string,get,c#,对接,System,接口,using,prestr,public
From: https://blog.csdn.net/qq_61596453/article/details/140828243