具體調用:
XunFeiAssistant.getVoiceContent("16k.wav")api
using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Net.Security; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Web; namespace THHN.Assistant { public class XunFeiAssistant { //===================================================================================================================== const string appid = "填寫你本身的appid"; //訊飛開放平臺註冊申請應用的應用ID(appid) const string apiKey = "填寫你本身的apikey"; //訊飛開放平臺註冊申請應用的APIKey /// <summary> /// 獲取簽名 MD5(apiKey + curTime + param) /// </summary> /// <param name="curTime"></param> /// <param name="param"></param> /// <returns></returns> public static string getSign(string curTime, string param) { string str = apiKey + curTime + param; return GetMd5Str32(str); } /// <summary> /// 進行md5簽名 /// </summary> /// <param name="str"></param> /// <returns></returns> static string GetMd5Str32(string str) { MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider(); char[] temp = str.ToCharArray(); byte[] buf = new byte[temp.Length]; for (int i = 0; i < temp.Length; i++) { buf[i] = (byte)temp[i]; } byte[] data = md5Hasher.ComputeHash(buf); StringBuilder sBuilder = new StringBuilder(); for (int i = 0; i < data.Length; i++) { sBuilder.Append(data[i].ToString("x2")); } return sBuilder.ToString(); } /// <summary> /// 獲取時間標籤 /// </summary> /// <returns></returns> static string getTimestamp() { var ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); return Convert.ToInt64(ts.TotalSeconds).ToString(); } /// <summary> /// 對文件內容進行base64編碼 /// </summary> /// <param name="filepath"></param> /// <returns></returns> static string getFileBase64String(string filepath) { FileStream filestream = new FileStream(filepath, FileMode.Open); byte[] bt = new byte[filestream.Length]; filestream.Read(bt, 0, bt.Length); var base64Str = Convert.ToBase64String(bt); filestream.Close(); return base64Str; } /// <summary> /// 對字符串進行base64編碼 /// </summary> /// <param name="encode"></param> /// <param name="source"></param> /// <returns></returns> static string EncodeBase64(Encoding encode, string source) { byte[] bytes = encode.GetBytes(source); string base64Str = Convert.ToBase64String(bytes); return base64Str; } //===================================================================================================================== /// <summary> /// 獲取wav文件語言內容 /// </summary> /// <param name="filepath"></param> /// <returns></returns> public static string getVoiceContent(string filepath) { string url = "http://api.xfyun.cn/v1/service/v1/iat"; string param = "{\"engine_type\": \"sms16k\",\"aue\": \"raw\"}"; var BodyData = "audio=" + getFileBase64String(filepath); return post(url, param, BodyData); } /// <summary> /// post請求 /// </summary> /// <param name="url"></param> /// <param name="param"></param> /// <param name="body"></param> /// <returns></returns> public static string post(string url, string param, string body) { HttpWebRequest httpRequest = null; HttpWebResponse httpResponse = null; if (url.Contains("https://")) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url)); } else { httpRequest = (HttpWebRequest)WebRequest.Create(url); } httpRequest.Method = "post"; param = EncodeBase64(Encoding.UTF8, param); string curTime = getTimestamp(); string checkSum = getSign(curTime, param); httpRequest.Headers.Add("X-Appid", XunFeiAssistant.appid); httpRequest.Headers.Add("X-CurTime", curTime); httpRequest.Headers.Add("X-Param", param); httpRequest.Headers.Add("X-CheckSum", checkSum); //根據API的要求,定義相對應的Content-Type httpRequest.ContentType = "application/x-www-form-urlencoded"; if (0 < body.Length) { byte[] data = Encoding.UTF8.GetBytes(body); httpRequest.ContentLength = data.Length; using (Stream stream = httpRequest.GetRequestStream()) { stream.Write(data, 0, data.Length); stream.Flush(); } } try { httpResponse = (HttpWebResponse)httpRequest.GetResponse(); } catch (WebException ex) { return ""; } Stream st = httpResponse.GetResponseStream(); StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8")); string result = reader.ReadToEnd(); st.Close(); st.Dispose(); reader.Close(); reader.Dispose(); return result; } public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; } } }