Json请求错误,而不是常规错误



尝试通过WebSocket (WebSocketSharp)连接https://www.xbtce.com在这里输入链接描述

这里是登录json模型

 {
      "Id": <some unique Id>,
      "Request": "Login",
      "Params": {
        "AuthType": "HMAC",
        "WebApiId":  <Web API Id>,
        "WebApiKey": <Web API Key>,
        "Timestamp": <timestamp (e.g. Date.now())>,
        "Signature": <signature>,
        "DeviceId":  <Device Id>
      }
    }

这是我的代码,总是给出

 {
  "Response": "Error",
  "Error": "Cannot parse JSON request!"
}

代替正常响应

{
  "Id": <your unique Id>,
  "Response": "Error",
  "Error": <error description from TickTrader Server>
}

我的代码
    private static WebSocket socket = new WebSocket("wss://cryptottlivewebapi.xbtce.net:3020");
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            socket.OnMessage += Socket_OnMessage;
            socket.OnOpen += socket_Opened;
            socket.OnError += socket_Error;
            socket.Connect();

            socket.OnClose += socket_Closed;
        }
        private void Socket_OnMessage(object sender, MessageEventArgs e)
        {
            if (e.IsText)
            {
                MessageBox.Show(e.Data.ToString());
            }
            else
            {
                MessageBox.Show(e.RawData.ToString());
            }
        }

        private void socket_Closed(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }


        private void socket_Error(object sender, ErrorEventArgs e)
        {
            MessageBox.Show(e.Exception.ToString());
        }
        private void socket_Opened(object sender, EventArgs e)
        {
            string jsonSerialized = JsonConvert.SerializeObject(GetLogin()).ToString() ;
            MessageBox.Show(jsonSerialized);
            socket.Send(jsonSerialized);
        }
        LoginParams GetLogin()
        {
            return new LoginParams
            {
                Id = Guid.NewGuid().ToString(),
                Request = "Login",
                Params = new Parameters
                {
                    AuthType = "HMAC",
                    WebApiId = "my id",
                    WebApiKey = "my api key",
                    Timestamp = ((Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds).ToString(),
                    Signature = Hmac.CreateToken(((Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds).ToString(), "2058e846-0a32-49b9-bc8f-4e28afd0aa9c", "my id", "my key").ToString(),
                    DeviceId = "WebBrowser"
                }
            };
        }
        public static double DateTimeToUnixTimestamp(DateTime dateTime)
        {
            return (TimeZoneInfo.ConvertTimeToUtc(dateTime) -
                   new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc)).TotalSeconds;
        }
    }
public static class Hmac
    {
        public static string CreateToken(string timestamp, string id , string key, string secret)
        {
            string message = timestamp + id + key;
            secret = secret ?? "";
            var encoding = new System.Text.ASCIIEncoding();
            byte[] keyByte = encoding.GetBytes(secret);
            byte[] messageBytes = encoding.GetBytes(message);
            using (var hmacsha256 = new HMACSHA256(keyByte))
            {
                byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
                return Convert.ToBase64String(hashmessage);
            }
        }
    }
    public class LoginParams
    {
        public string Id { get; set; }
        public string Request { get; set; }
        public Parameters Params { get; set; }
    }
    public class Parameters
    {
        public string AuthType {
            get; set; }
        public string WebApiId { get; set; }
        public string WebApiKey { get; set; }
        public string Timestamp {
            get; set; }
        public string Signature { get; set; }
        public string DeviceId { get; set; }
    }

Json序列化输出

{
"Id": "be078b3d-2eb8-475a-97fb-89aedf775b78",
"Request": "Login",
"Params": {
    "AuthType": "HMAC",
    "WebApiId": "myid",
    "WebApiKey": "mykey",
    "Timestamp": "1467486605",
    "Signature": "NbREH0HkPadfuDESuGEHho/FwX+DFdIoCV/D5aW/gv8=",
    "DeviceId": "WebBrowser"
}

}

Thanks in advance

最后发现了问题,似乎时间戳不应该是字符串,而是一个长

    LoginParams GetLogin()
    {
        return new LoginParams
        {
            Id = Guid.NewGuid().ToString(),
            Request = "Login",
            Params = new Parameters
            {
                AuthType = "HMAC",
                WebApiId = "my id",
                WebApiKey = "my api key",
                Timestamp = ((long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds).ToString(),
                Signature = Hmac.CreateToken(((long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds), "2058e846-0a32-49b9-bc8f-4e28afd0aa9c", "my id", "my key").ToString(),
                DeviceId = "WebBrowser"
            }
        };
    }

相关内容

  • 没有找到相关文章

最新更新