/**发布环境 使用发布环境 发布的正式签名KEY**/
#define Distribution
///**测试环境 使用测试环境 使用测试的签名KEY**/
//#undef Distribution
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
//using HDL_ON.Entity;
using RestSharp;
using Shared.SimpleControl;
namespace Shared
{
public class HttpUtil
{
/////
///// API_HTTPS
/////
//public const string API_HTTPS = "https://";
///
/// RegionMark
///
public const string RegionMark = "HDL";
///
/// 请求超时时间
///
public const int TIME_OUT = 10;
///
/// 特殊接口请求超时时间
///
public const int TIME_OUT_LONG = 20;
///////
/////// Bearer 暂时设为空,从登陆成功的返回的headerPrefix参数动态获取
///////
//public const string TOKEN_BEARER = "Bearer ";
#if Distribution
///**********正式环境**********
///
/// 固定域名,正式环境
/// 公共域名就近解析
///
public const string GlobalRequestHttpsHost = "https://bahrain-gateway.hdlcontrol.com";
///
///
///
const string APP_KEY = "HDL-HOME-IND-APP";
///
///
///
const string SECRET_KEY = "yPL345bn68gHnvilG4tYbq3cTYkiHu";
#else
///**********测试环境**********
///
/// 固定域名,测试境
/// 公共域名就近解析
///
public const string GlobalRequestHttpsHost = "https://china-gateway.hdlcontrol.com";
///
///
///
const string APP_KEY = "HDL-APP-TENANT";
///
///
///
const string SECRET_KEY = "CeL345bn28gHnvi9G4tYcq3cTYkiiC";
#endif
#region **********签名校验**********
/////
/////
/////
//const string APP_KEY = "HDL-HOME-APP-TEST";
/////
/////
/////
//const string SECRET_KEY = "WeJ8TY88vbakCcnvH8G1tDUqzLWY8yss";
///
/// 获取当前时间戳值
///
///
static string GetTimestamp ()
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime (new System.DateTime (1970, 1, 1)); // 当地时区
return ((long)(DateTime.Now - startTime).TotalMilliseconds).ToString (); // 相差秒数
//return ((long)(DateTime.Now - startTime).TotalSeconds).ToString(); // 相差秒数
}
///
///
///
///
///
static string SignMD5Encrypt (string s)
{
byte [] sign = MD5.Create ().ComputeHash (UTF8Encoding.UTF8.GetBytes (s));
string signstr = string.Empty;
foreach (byte item in sign) {
signstr += item.ToString ("X2");
}
return signstr.ToLower ();
}
///
/// 判断当前值是否需要参与签名,保持跟云端一致
/// 空字符串不参与
/// 数组,集合,对象不参与
///
///
///
static bool IfValueNeedSign (string valueStr)
{
if (string.IsNullOrEmpty (valueStr) || valueStr.StartsWith ("{") || valueStr.StartsWith ("[")) {
return false;
}
return true;
}
///
/// 2020-11-02
/// 基础服务的接口都要校验sign
/// 计算sign签名
///
///
public static string GetSignRequestJson (object requestObj)
{
try {
//1. 将model实体转为Dictionary
var paramDictionary = Newtonsoft.Json.JsonConvert.DeserializeObject> (Newtonsoft.Json.JsonConvert.SerializeObject (requestObj));
//2. 计算sign
if (paramDictionary != null) {
paramDictionary.Add ("appKey", APP_KEY);
paramDictionary.Add ("timestamp", GetTimestamp ());
//2.1 字典升序
paramDictionary = paramDictionary.OrderBy (o => o.Key).ToDictionary (o => o.Key, p => p.Value);
//2.2 拼接按URL键值对
string str = string.Empty;
foreach (KeyValuePair item in paramDictionary) {
//Value为null不参加校验
if (item.Value != null) {
//检测当前参数是否需要参与校验
if (IfValueNeedSign (item.Value.ToString ())) {
//如果是bool类型,要转小写
if (item.Value is bool) {
str += item.Key + "=" + item.Value.ToString ().ToLower () + "&";
} else {
str += item.Key + "=" + item.Value.ToString () + "&";
}
}
}
}
//2.3 拼接SECRET_KEY
str = str.Substring (0, str.Length - 1) + SECRET_KEY;
//2.4 MD5转换+转小写
var signstr = SignMD5Encrypt (str);
paramDictionary.Add ("sign", signstr);
var signResult = Newtonsoft.Json.JsonConvert.SerializeObject (paramDictionary);
return signResult;
} else {
return "";
}
} catch {
return "";
}
}
#endregion
}
///
/// 响应参数
///
[Serializable]
public class ResponsePackNew
{
///
/// 响应状态码
///
public string Code;
///
/// 响应内容
///
public object Data;
///
/// 响应错误信息
///
public string message;
///
/// 这个是请求错误时的扩展数据,以后所有的附带扩展数据都会放在这里面动态维护
///
public object extra;
}
}