具体调用:
XunFeiAssistant.getVoiceContent("16k.wav")
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;
}
}
}