资源简介

使用c# webapi 实现科大讯飞的语音听写功能,gb2312 报错的话需要安装System.Text.Encoding.CodePages这个包。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using Newtonsoft.Json;



namespace Webiat
{
    class Program
    {
        public static String Md5(string s)
        {
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(s);
            bytes = md5.ComputeHash(bytes);
            md5.Clear();
            string ret = ““;
            for (int i = 0; i < bytes.Length; i++)
            {
                ret += Convert.ToString(bytes[i] 16).PadLeft(2 ‘0‘);
            }
            return ret.PadLeft(32 ‘0‘);
        }

        public static void Headers()
        {
            string x_appid = “****“;
            string api_key = “*******“;
            string path = “./1.wav“;

            string aue = “raw“ engine_type = “sms16k“;
            string param = “{\“aue\“:\““ + aue + “\““ + “\“engine_type\“:\““ + engine_type + “\“}“;
       
            System.Text.Encoding encode = System.Text.Encoding.ASCII;
            byte[] bytedata = encode.GetBytes(param);
            string x_param = Convert.Tobase64String(bytedata);
          

            TimeSpan ts = DateTime.UtcNow - new DateTime(1970 1 1 0 0 0 0);
            string curTime = Convert.ToInt64(ts.TotalSeconds).ToString();

            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string result = string.Format(“{0}{1}{2}“ api_key curTime x_param);
            //Console.WriteLine(Program.Md5(x_param));
            string X_checksum = Program.Md5(result);
            //Console.WriteLine(X_checksum);

            byte[] arr = File.ReadAllBytes(path);
            string cc = Convert.Tobase64String(arr);
            string data = “audio=“ + cc;
            
            string Url = “http://api.xfyun.cn/v1/service/v1/iat“;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = “POST“;
            request.ContentType = “application/x-www-form-urlencoded“;
            request.Headers[“X-Appid“] = x_appid;
            request.Headers[“X-CurTime“] = curTime;
            request.Headers[“X-Param“] = x_param;
            request.Headers[“X-CheckSum“] = X_checksum;

            request.ContentLength =Encoding.UTF8.GetByteCount(data);
            Stream requestStream = request.GetRequestStream();
            StreamWriter streamWriter = new StreamWriter(requestStream Encoding.GetEncoding(“gb2312“));
            streamWriter.Write(data);
            streamWriter.Close();
            
            string htmlStr = string.Empty;
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            Stream responseStream = response.GetRespon

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-05-17 18:01  Demo\
     目录           0  2018-05-07 19:09  Demo\.vs\
     目录           0  2018-05-07 19:09  Demo\.vs\Demo\
     目录           0  2018-05-07 19:09  Demo\.vs\Demo\v15\
     文件       47616  2018-06-06 16:38  Demo\.vs\Demo\v15\.suo
     目录           0  2018-05-07 19:09  Demo\.vs\Demo\v15\Server\
     目录           0  2018-06-06 16:38  Demo\.vs\Demo\v15\Server\sqlite3\
     文件           0  2018-05-07 19:09  Demo\.vs\Demo\v15\Server\sqlite3\db.lock
     文件      626688  2018-05-31 09:11  Demo\.vs\Demo\v15\Server\sqlite3\storage.ide
     文件       45622  2018-04-20 09:57  Demo\1.wav
     目录           0  2018-06-06 16:37  Demo\Demo\
     文件         189  2018-05-07 19:09  Demo\Demo\App.config
     目录           0  2018-05-07 19:09  Demo\Demo\bin\
     目录           0  2018-05-17 18:02  Demo\Demo\bin\Debug\
     文件       45622  2018-04-20 09:57  Demo\Demo\bin\Debug\1.wav
     文件        7168  2018-05-31 09:11  Demo\Demo\bin\Debug\demo.exe
     文件         189  2018-05-07 19:09  Demo\Demo\bin\Debug\Demo.exe.config
     文件       13824  2018-05-31 09:11  Demo\Demo\bin\Debug\Demo.pdb
     文件      662528  2018-03-24 17:44  Demo\Demo\bin\Debug\Newtonsoft.Json.dll
     文件      684778  2018-03-24 17:44  Demo\Demo\bin\Debug\Newtonsoft.Json.xml
     文件        2647  2018-05-21 15:06  Demo\Demo\Demo.csproj
     目录           0  2018-05-07 19:09  Demo\Demo\obj\
     目录           0  2018-05-21 14:49  Demo\Demo\obj\Debug\
     文件           0  2018-05-31 09:11  Demo\Demo\obj\Debug\Demo.csproj.CopyComplete
     文件          42  2018-05-21 14:49  Demo\Demo\obj\Debug\Demo.csproj.CoreCompileInputs.cache
     文件         685  2018-05-30 19:51  Demo\Demo\obj\Debug\Demo.csproj.FileListAbsolute.txt
     文件       59231  2018-05-21 14:49  Demo\Demo\obj\Debug\Demo.csprojResolveAssemblyReference.cache
     文件        7168  2018-05-31 09:11  Demo\Demo\obj\Debug\demo.exe
     文件       13824  2018-05-31 09:11  Demo\Demo\obj\Debug\Demo.pdb
     文件         713  2018-05-10 08:51  Demo\Demo\obj\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        7004  2018-05-21 14:49  Demo\Demo\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
............此处省略41个文件信息

评论

共有 条评论