//发送请求 public static String sendRequest(String url, String body, String ak_id, String ak_secret) { String result = ""; HttpResponse response = new HttpResponse(); //try { Uri realUrl = new Uri(url); //http header 参数 String method = "POST"; String accept = "application/json"; String content_type = "application/json"; DateTime time = DateTime.UtcNow; string date = ToGMTString(time); // 1.对body做MD5+BASE64加密 String bodyMd5 = MD5Base64(body); String stringToSign = method + "\n" + accept + "\n" + bodyMd5 + "\n" + content_type + "\n" + date; // 2.计算 HMAC-SHA1 String signature = HMACSha1(stringToSign, ak_secret); // 3.得到 authorization header String authHeader = "Dataplus " + ak_id + ":" + signature; HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(realUrl); httpWebRequest.Method = method; httpWebRequest.Accept = accept; httpWebRequest.ContentType = content_type; //httpWebRequest.Data MethodInfo priDateMethod = httpWebRequest.Headers.GetType().GetMethod("AddWithoutValidate", BindingFlags.Instance | BindingFlags.NonPublic); priDateMethod.Invoke(httpWebRequest.Headers, new[] { "Date", date }); //httpWebRequest.Authorization //MethodInfo priAuthorizationMethod = httpWebRequest.Headers.GetType().GetMethod("AddWithoutValidate", BindingFlags.Instance | BindingFlags.NonPublic); //priAuthorizationMethod.Invoke(httpWebRequest.Headers, new[] { "Authorization", authHeader }); httpWebRequest.Headers.Add("Authorization", authHeader); byte[] bt = Encoding.UTF8.GetBytes(body); //httpWebRequest.ContentLength //MethodInfo priLengthMethod = httpWebRequest.Headers.GetType().GetMethod("AddWithoutValidate", BindingFlags.Instance | BindingFlags.NonPublic); //priLengthMethod.Invoke(httpWebRequest.Headers, new[] { "ContentLength", bt.Length.ToString() }); httpWebRequest.ContentLength = bt.Length; ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); httpWebRequest.ServicePoint.UseNagleAlgorithm = true; httpWebRequest.GetRequestStream().Write(bt, 0, bt.Length); HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); if (HttpStatusCode.OK == httpWebResponse.StatusCode) { response.setStatus(200); StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.UTF8); string responseContent = streamReader.ReadToEnd(); streamReader.Close(); httpWebResponse.Close(); response.setResult(responseContent); response.setMassage("OK"); result = responseContent; } } //catch (Exception e) { //Debug.Log("请求异常!" + e.HelpLink); } return(result); }
/* * 发送POST请求 */ //public static HttpResponse sendTtsPost(String textData, String audioType, String audioName, String url, String ak_id, String ak_secret) //{ // String result = ""; // HttpResponse response = new HttpResponse(); // //URL realUrl = new URL(url); // Uri realUrl = new Uri(url); // /* // * http header 参数 // */ // String method = "POST"; // String content_type = "text/plain"; // String accept = "audio/" + audioType + ",application/json"; // int length = textData.Length; // DateTime time = DateTime.UtcNow; // string date = ToGMTString(time); // // 1.对body做MD5+BASE64加密 // String bodyMd5 = MD5Base64(Encoding.UTF8.GetBytes(textData)); // String stringToSign = method + "\n" + accept + "\n" + bodyMd5 + "\n" + content_type + "\n" + date; // // 2.计算 HMAC-SHA1 // String signature = HMACSha1(stringToSign, ak_secret); // // 3.得到 authorization header // String authHeader = "Dataplus " + ak_id + ":" + signature; // var httpWebRequest = (HttpWebRequest)WebRequest.Create(realUrl); // httpWebRequest.Method = method; // httpWebRequest.Accept = accept; // httpWebRequest.ContentType = content_type; // //httpWebRequest.Date // MethodInfo priDateMethod = httpWebRequest.Headers.GetType().GetMethod("AddWithoutValidate", BindingFlags.Instance | BindingFlags.NonPublic); // priDateMethod.Invoke(httpWebRequest.Headers, new[] { "Date", date }); // //httpWebRequest.Authorization // //MethodInfo priAuthorizationMethod = httpWebRequest.Headers.GetType().GetMethod("AddWithoutValidate", BindingFlags.Instance | BindingFlags.NonPublic); // //priAuthorizationMethod.Invoke(httpWebRequest.Headers, new[] { "Authorization", authHeader }); // httpWebRequest.Headers.Add("Authorization", authHeader); // //httpWebRequest.ContentLength // //MethodInfo priLengthMethod = httpWebRequest.Headers.GetType().GetMethod("AddWithoutValidate", BindingFlags.Instance | BindingFlags.NonPublic); // //priLengthMethod.Invoke(httpWebRequest.Headers, new[] { "ContentLength", Encoding.UTF8.GetBytes(textData).Length.ToString() }); // httpWebRequest.ContentLength = Encoding.UTF8.GetBytes(textData).Length; // httpWebRequest.GetRequestStream().Write(Encoding.UTF8.GetBytes(textData), 0, Encoding.UTF8.GetBytes(textData).Length); // Debug.Log("Accept: " + httpWebRequest.Accept + " " + " bodyMd5: " + bodyMd5 + " " + " ContentType: " + httpWebRequest.ContentType + " " + " Date: " + httpWebRequest.Headers.Get("Date") + " " + " Authorization: " + authHeader + " ContentLength: " + httpWebRequest.ContentLength); // HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); // if (HttpStatusCode.OK == httpWebResponse.StatusCode) // { // try // { // response.setStatus(200); // StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.Unicode); // string responseContent = streamReader.ReadToEnd(); // streamReader.Close(); // httpWebResponse.Close(); // response.setMassage("OK"); // Debug.Log("post response status code: [" + response.getStatus() + "], generate tts audio file :" + audioName + "." + audioType); // string savePath = Application.streamingAssetsPath + "/" + audioName + "." + audioType; //本地保存路径 // FileStream fs = new FileStream(savePath, FileMode.Append); // var buff = Encoding.Unicode.GetBytes(responseContent); // fs.Write(buff, 0, buff.Length); // fs.Close(); // Debug.Log(savePath); // response.setResult(savePath); // } // catch (Exception ex) // { // Debug.Log("报错 ==》 " + ex.Message); // } // } // return response; //} public static HttpResponse sendTtsPost(String textData, String audioType, String audioName, String url, String ak_id, String ak_secret) { String result = ""; HttpResponse response = new HttpResponse(); //URL realUrl = new URL(url); Uri realUrl = new Uri(url); /* * http header 参数 */ String method = "POST"; String content_type = "text/plain"; String accept = "audio/" + audioType + ",application/json"; int length = textData.Length; DateTime time = DateTime.UtcNow; string date = ToGMTString(time); // 1.对body做MD5+BASE64加密 String bodyMd5 = MD5Base64(Encoding.UTF8.GetBytes(textData)); String stringToSign = method + "\n" + accept + "\n" + bodyMd5 + "\n" + content_type + "\n" + date; // 2.计算 HMAC-SHA1 String signature = HMACSha1(stringToSign, ak_secret); // 3.得到 authorization header String authHeader = "Dataplus " + ak_id + ":" + signature; var httpWebRequest = (HttpWebRequest)WebRequest.Create(realUrl); httpWebRequest.Method = method; httpWebRequest.Accept = accept; httpWebRequest.ContentType = content_type; MethodInfo priDateMethod = httpWebRequest.Headers.GetType().GetMethod("AddWithoutValidate", BindingFlags.Instance | BindingFlags.NonPublic); priDateMethod.Invoke(httpWebRequest.Headers, new[] { "Date", date }); httpWebRequest.Headers.Add("Authorization", authHeader); httpWebRequest.ContentLength = Encoding.UTF8.GetBytes(textData).Length; httpWebRequest.GetRequestStream().Write(Encoding.UTF8.GetBytes(textData), 0, Encoding.UTF8.GetBytes(textData).Length); HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); if (HttpStatusCode.OK == httpWebResponse.StatusCode) { try { response.setStatus(200); StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.Unicode); string responseContent = streamReader.ReadToEnd(); streamReader.Close(); httpWebResponse.Close(); response.setMassage("OK"); string savePath = Application.streamingAssetsPath + "/" + audioName + "." + audioType; //本地保存路径 FileStream fs = new FileStream(savePath, FileMode.Append); var buff = Encoding.Unicode.GetBytes(responseContent); fs.Write(buff, 0, buff.Length); fs.Close(); response.setResult(savePath); } catch (Exception ex) { Debug.Log("报错 ==》 " + ex.Message); } } return(response); }
/* * 发送POST请求 */ public static HttpResponse sendAsrPost(byte[] audioData, String audioFormat, int sampleRate, String url, String ak_id, String ak_secret) { String result = ""; HttpResponse response = new HttpResponse(); try { Uri realUrl = new Uri(url); /* * http header 参数 */ String method = "POST"; String accept = "application/json"; String content_type = "audio/" + audioFormat + ";samplerate=" + sampleRate; int length = audioData.Length; DateTime time = DateTime.UtcNow; String date = ToGMTString(time); // 1.对body做MD5+BASE64加密 String bodyMd5 = MD5Base64(audioData); String md52 = MD5Base64(Encoding.GetEncoding("GBK").GetBytes(bodyMd5)); String stringToSign = method + "\n" + accept + "\n" + md52 + "\n" + content_type + "\n" + date; // 2.计算 HMAC-SHA1 String signature = HMACSha1(stringToSign, ak_secret); // 3.得到 authorization header String authHeader = "Dataplus " + ak_id + ":" + signature; HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(realUrl); httpWebRequest.Method = method; httpWebRequest.Accept = accept; httpWebRequest.ContentType = content_type; MethodInfo priDateMethod = httpWebRequest.Headers.GetType().GetMethod("AddWithoutValidate", BindingFlags.Instance | BindingFlags.NonPublic); priDateMethod.Invoke(httpWebRequest.Headers, new[] { "Date", date }); MethodInfo priAuthorizationMethod = httpWebRequest.Headers.GetType().GetMethod("AddWithoutValidate", BindingFlags.Instance | BindingFlags.NonPublic); priAuthorizationMethod.Invoke(httpWebRequest.Headers, new[] { "Authorization", authHeader }); //httpWebRequest.Headers.Add("Authorization", authHeader); //httpWebRequest.ContentLength //MethodInfo priLengthMethod = httpWebRequest.Headers.GetType().GetMethod("AddWithoutValidate", BindingFlags.Instance | BindingFlags.NonPublic); //priLengthMethod.Invoke(httpWebRequest.Headers, new[] { "ContentLength", audioData.Length.ToString() }); httpWebRequest.ContentLength = audioData.Length; httpWebRequest.GetRequestStream().Write(audioData, 0, audioData.Length); HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); Debug.Log("StatusCode: " + httpWebResponse.StatusCode); if (HttpStatusCode.OK == httpWebResponse.StatusCode) { response.setStatus(200); StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.UTF8); string responseContent = streamReader.ReadToEnd(); streamReader.Close(); httpWebResponse.Close(); response.setResult(responseContent); Debug.Log("response: " + responseContent); response.setMassage("OK"); //string savePath = @"D:\1.Txt"; //本地保存路径 //FileStream fs = new FileStream(savePath, FileMode.Append); //var buff = Encoding.Unicode.GetBytes(responseContent); //fs.Write(buff, 0, buff.Length); //fs.Close(); } } catch (Exception ex) { } return(response); }
/* * 发送POST请求 */ public static HttpResponse sendTtsPost(String textData, String audioType, String audioName, String url, String ak_id, String ak_secret) { String result = ""; HttpResponse response = new HttpResponse(); Uri realUrl = new Uri(url); /* * http header 参数 */ String method = "POST"; String content_type = "text/plain"; String accept = "audio/" + audioType + ",application/json"; int length = textData.Length; DateTime time = DateTime.UtcNow; string date = ToGMTString(time); Console.WriteLine("date: " + date); // 1.对body做MD5+BASE64加密 String bodyMd5 = MD5Base64(Encoding.UTF8.GetBytes(textData)); String stringToSign = method + "\n" + accept + "\n" + bodyMd5 + "\n" + content_type + "\n" + date; // 2.计算 HMAC-SHA1 String signature = HMACSha1(stringToSign, ak_secret); // 3.得到 authorization header String authHeader = "Dataplus " + ak_id + ":" + signature; var httpWebRequest = (HttpWebRequest)WebRequest.Create(realUrl); httpWebRequest.Method = method; httpWebRequest.Accept = accept; httpWebRequest.ContentType = content_type; //bool bo = GMTStrParse(DateTime.Now.ToString("r"), out time); //MethodInfo priMethod = httpWebRequest.Date.GetType().GetMethod("AddWithoutValidate", BindingFlags.Instance | BindingFlags.NonPublic); //priMethod.Invoke(httpWebRequest.Date, new[] { "Kind", "DateTimeKind.Utc" }); //httpWebRequest.Date = DateTime.ParseExact(DateTime.Now.ToString("r").Replace("GMT", ""), "ddd, dd MMM yyyy HH':'mm':'ss zzz", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal); httpWebRequest.Date = time; //httpWebRequest.Date.Kind = DateTimeKind.Utc; // httpWebRequest.Date = DateTime.SpecifyKind(DateTime.UtcNow, DateTimeKind.Utc); httpWebRequest.Headers.Add("Authorization", authHeader); httpWebRequest.ContentLength = Encoding.UTF8.GetBytes(textData).Length; httpWebRequest.GetRequestStream().Write(Encoding.UTF8.GetBytes(textData), 0, Encoding.UTF8.GetBytes(textData).Length); Console.WriteLine("Accept: " + httpWebRequest.Accept + " " + " bodyMd5: " + bodyMd5 + " " + " ContentType: " + httpWebRequest.ContentType + " " + " Date: " + httpWebRequest.Date + " " + " Authorization: " + authHeader + " ContentLength: " + httpWebRequest.ContentLength); HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); if (HttpStatusCode.OK == httpWebResponse.StatusCode) { try { response.setStatus(200); StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.Unicode); string responseContent = streamReader.ReadToEnd(); streamReader.Close(); httpWebResponse.Close(); response.setResult(responseContent); response.setMassage("OK"); Console.WriteLine("post response status code: [" + response.getStatus() + "], generate tts audio file :" + audioName + "." + audioType); string savePath = @"D:\" + audioName + "." + audioType; //本地保存路径 FileStream fs = new FileStream(savePath, FileMode.Append); var buff = Encoding.Unicode.GetBytes(responseContent); fs.Write(buff, 0, buff.Length); fs.Close(); } catch (Exception ex) { Console.WriteLine("报错 ==》 " + ex.Message); } } return(response); }