科大讯飞 语音评测 流式版鉴权

 public static string getAuthUrl(string hostUrl, string apiKey, string apiSecret)
        {
            Uri uri = new Uri(hostUrl);
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            string date = DateTime.UtcNow.ToUniversalTime().ToString(CultureInfo.CurrentCulture.DateTimeFormat.RFC1123Pattern, new System.Globalization.CultureInfo("en-us"));

            StringBuilder builder = new StringBuilder("host: ").Append(uri.Host).Append("\n").//
                    Append("date: ").Append(date).Append("\n").//
                    Append("GET ").Append(uri.LocalPath).Append(" HTTP/1.1");

            byte[] hexDigits;
            string sha;
            using (var hmacsha256 = new HMACSHA256(Encoding.UTF8.GetBytes(apiSecret)))
            {
                hexDigits = hmacsha256.ComputeHash(Encoding.UTF8.GetBytes(builder.ToString()));
                sha = Convert.ToBase64String(hexDigits);
            }

            string authorization = string.Format("hmac username=\"{0}\", algorithm=\"{1}\", headers=\"{2}\", signature=\"{3}\"", apiKey, "hmac-sha256", "host date request-line", sha);
            Debug.Log(authorization);

            hostUrl += "?authorization=" + Convert.ToBase64String(Encoding.UTF8.GetBytes(authorization)) + "&date=" + date + "&host=" + uri.Host;

            return hostUrl;
        }
public async void WebSocket()
        {
            try
            {
                //ClientWebSocket ws = new ClientWebSocket();
                //CancellationToken ct = new CancellationToken();
                string authUrl = getAuthUrl(hostUrl, apiKey, apiSecret);

                Debug.Log(authUrl);
                Uri url = new Uri(authUrl);
                await ws.ConnectAsync(url, ct);
                //await ws.SendAsync(new ArraySegment<byte>(Encoding.UTF8.GetBytes("hello")), WebSocketMessageType.Binary, true, ct); //发送数据
                isSocket = true;

                while (true)
                {
                    var result = new byte[1024];
                    await ws.ReceiveAsync(new ArraySegment<byte>(result), new CancellationToken());//接受数据
                    var str = Encoding.UTF8.GetString(result, 0, result.Length);
                    onMessage(str);
                }
            }
            catch (Exception ex)
            {
                Debug.Log(ex.Message);
            }
        }

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值