using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net; using System.IO; using System.Threading; using Newtonsoft.Json.Linq; namespace voice { public partial class Form1 : Form { string testtoken = ""; string cuid = "";//隨便寫(xiě) string serverURL = "http://vop.baidu.com/server_api"; public Form1() { InitializeComponent(); cuid = Guid.NewGuid().ToString(); } private string Post(string audioFilePath) { serverURL += "?lan=zh&cuid=kwwwvagaa&token=" + testtoken; FileStream fs = new FileStream(audioFilePath, FileMode.Open); byte[] voice = new byte[fs.Length]; fs.Read(voice, 0, voice.Length); fs.Close(); fs.Dispose(); HttpWebRequest request = null; Uri uri = new Uri(serverURL); request = (HttpWebRequest)WebRequest.Create(uri); request.Timeout = 5000; request.Method = "POST"; request.ContentType = "audio/wav; rate=16000"; request.ContentLength = voice.Length; try { using (Stream writeStream = request.GetRequestStream()) { writeStream.Write(voice, 0, voice.Length); writeStream.Close(); writeStream.Dispose(); } } catch { return null; } string result = string.Empty; string result_final = string.Empty; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (Stream responseStream = response.GetResponseStream()) { using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8)) { string line = string.Empty; StringBuilder sb = new StringBuilder(); while (!readStream.EndOfStream) { line = readStream.ReadLine(); sb.Append(line); sb.Append("\r"); } readStream.Close(); readStream.Dispose(); result = sb.ToString(); string[] indexs = result.Split(','); foreach (string index in indexs) { string[] _indexs = index.Split('"'); if (_indexs[2] == ":[") result_final = _indexs[3]; } } responseStream.Close(); responseStream.Dispose(); } response.Close(); } return result_final; } private void button1_Click(object sender, EventArgs e) { textBox1.Text = Post(Application.StartupPath + "\\2.wav"); } } }
附件中有源代碼,具體看里面的README
創(chuàng)新互聯(lián)從2013年開(kāi)始,先為阿魯科爾沁等服務(wù)建站,阿魯科爾沁等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為阿魯科爾沁企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。 附件:http://down.51cto.com/data/2368300另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。