/**发布环境 使用发布环境 发布的正式签名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
|
{
|
|
///// <summary>
|
///// API_HTTPS
|
///// </summary>
|
//public const string API_HTTPS = "https://";
|
/// <summary>
|
/// RegionMark
|
/// </summary>
|
public const string RegionMark = "HDL";
|
/// <summary>
|
/// 请求超时时间
|
/// </summary>
|
public const int TIME_OUT = 10;
|
/// <summary>
|
/// 特殊接口请求超时时间
|
/// </summary>
|
public const int TIME_OUT_LONG = 20;
|
/////// <summary>
|
/////// Bearer 暂时设为空,从登陆成功的返回的headerPrefix参数动态获取
|
/////// </summary>
|
//public const string TOKEN_BEARER = "Bearer ";
|
|
|
|
|
|
#if Distribution
|
///**********正式环境**********
|
/// <summary>
|
/// 固定域名,正式环境
|
/// 公共域名就近解析
|
/// </summary>
|
public const string GlobalRequestHttpsHost = "https://bahrain-gateway.hdlcontrol.com";
|
/// <summary>
|
///
|
/// </summary>
|
const string APP_KEY = "HDL-HOME-IND-APP";
|
/// <summary>
|
///
|
/// </summary>
|
const string SECRET_KEY = "yPL345bn68gHnvilG4tYbq3cTYkiHu";
|
|
#else
|
///**********测试环境**********
|
/// <summary>
|
/// 固定域名,测试境
|
/// 公共域名就近解析
|
/// </summary>
|
public const string GlobalRequestHttpsHost = "https://china-gateway.hdlcontrol.com";
|
/// <summary>
|
///
|
/// </summary>
|
const string APP_KEY = "HDL-APP-TENANT";
|
/// <summary>
|
///
|
/// </summary>
|
const string SECRET_KEY = "CeL345bn28gHnvi9G4tYcq3cTYkiiC";
|
#endif
|
|
|
|
#region **********签名校验**********
|
///// <summary>
|
/////
|
///// </summary>
|
//const string APP_KEY = "HDL-HOME-APP-TEST";
|
///// <summary>
|
/////
|
///// </summary>
|
//const string SECRET_KEY = "WeJ8TY88vbakCcnvH8G1tDUqzLWY8yss";
|
|
|
|
/// <summary>
|
/// 获取当前时间戳值
|
/// </summary>
|
/// <returns></returns>
|
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(); // 相差秒数
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="signstr"></param>
|
/// <returns></returns>
|
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 ();
|
}
|
|
/// <summary>
|
/// 判断当前值是否需要参与签名,保持跟云端一致
|
/// 空字符串不参与
|
/// 数组,集合,对象不参与
|
/// </summary>
|
/// <param name="valueStr"></param>
|
/// <returns></returns>
|
static bool IfValueNeedSign (string valueStr)
|
{
|
if (string.IsNullOrEmpty (valueStr) || valueStr.StartsWith ("{") || valueStr.StartsWith ("[")) {
|
return false;
|
}
|
return true;
|
}
|
|
/// <summary>
|
/// 2020-11-02
|
/// 基础服务的接口都要校验sign
|
/// 计算sign签名
|
/// </summary>
|
/// <returns></returns>
|
public static string GetSignRequestJson (object requestObj)
|
{
|
try {
|
//1. 将model实体转为Dictionary<string, object>
|
var paramDictionary = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>> (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<string, object> 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
|
}
|
|
/// <summary>
|
/// 响应参数
|
/// </summary>
|
[Serializable]
|
public class ResponsePackNew
|
{
|
/// <summary>
|
/// 响应状态码
|
/// </summary>
|
public string Code;
|
|
/// <summary>
|
/// 响应内容
|
/// </summary>
|
public object Data;
|
|
/// <summary>
|
/// 响应错误信息
|
/// </summary>
|
public string message;
|
|
/// <summary>
|
/// 这个是请求错误时的扩展数据,以后所有的附带扩展数据都会放在这里面动态维护
|
/// </summary>
|
public object extra;
|
|
|
}
|
|
}
|