|
@@ -927,7 +927,7 @@ namespace OASystem.Infrastructure.Repositories.Groups
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public List<TranslateResult> ReTransBatch(List<string> list, string aimlanguage)
|
|
|
+ public async Task<List<TranslateResult>> ReTransBatch(List<string> list, string aimlanguage)
|
|
|
{
|
|
|
|
|
|
List<TranslateResult> reultStr = new List<TranslateResult>();
|
|
@@ -970,7 +970,7 @@ namespace OASystem.Infrastructure.Repositories.Groups
|
|
|
string curtime = ((GetBeijingTime().ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString();
|
|
|
dic.Add("curtime", curtime);
|
|
|
string signStr = appKey + Truncate(q) + salt + curtime + appSecret; ;
|
|
|
- string sign = ComputeHash(signStr, new SHA256CryptoServiceProvider());
|
|
|
+ string sign = ComputeHash(signStr, SHA256.Create());
|
|
|
|
|
|
|
|
|
dic.Add("q", Qtext);
|
|
@@ -982,7 +982,7 @@ namespace OASystem.Infrastructure.Repositories.Groups
|
|
|
try
|
|
|
{
|
|
|
|
|
|
- jsonStr = TransSync(url, dic);
|
|
|
+ jsonStr = await TransSync(url, dic);
|
|
|
|
|
|
JObject trans = (JObject)JsonConvert.DeserializeObject(jsonStr);
|
|
|
string errorCode = trans["errorCode"].ToString();
|
|
@@ -1021,54 +1021,26 @@ namespace OASystem.Infrastructure.Repositories.Groups
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
- protected string TransSync(string url, Dictionary<string, string> dic)
|
|
|
+ protected async Task<string> TransSync(string url, Dictionary<string, string> dic)
|
|
|
{
|
|
|
string result = "";
|
|
|
- HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
|
|
|
- req.Method = "POST";
|
|
|
- req.ContentType = "application/x-www-form-urlencoded";
|
|
|
-
|
|
|
- StringBuilder builder = new StringBuilder();
|
|
|
- int i = 0;
|
|
|
- foreach (var item in dic)
|
|
|
+ using (HttpClient client = new HttpClient())
|
|
|
{
|
|
|
- if (i > 0)
|
|
|
- builder.Append("&");
|
|
|
- if (item.Key == "q")
|
|
|
+ var content = new FormUrlEncodedContent(dic);
|
|
|
+ HttpResponseMessage response = await client.PostAsync(url, content);
|
|
|
+ response.EnsureSuccessStatusCode();
|
|
|
+ if (response.Content.Headers.ContentType.MediaType.ToLower().Equals("audio/mp3"))
|
|
|
{
|
|
|
- builder.AppendFormat(item.Value);
|
|
|
+ await SaveBinaryFile(response, "合成的音频存储路径");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- builder.AppendFormat("{0}={1}", item.Key, item.Value);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- i++;
|
|
|
- }
|
|
|
- byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
|
|
|
- req.ContentLength = data.Length;
|
|
|
- using (Stream reqStream = req.GetRequestStream())
|
|
|
- {
|
|
|
- reqStream.Write(data, 0, data.Length);
|
|
|
- reqStream.Close();
|
|
|
- }
|
|
|
- HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
|
|
|
- if (resp.ContentType.ToLower().Equals("audio/mp3"))
|
|
|
- {
|
|
|
- SaveBinaryFile(resp, "合成的音频存储路径");
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- Stream stream = resp.GetResponseStream();
|
|
|
- using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
|
|
|
- {
|
|
|
- result = reader.ReadToEnd();
|
|
|
+ result = await response.Content.ReadAsStringAsync();
|
|
|
}
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
- private static bool SaveBinaryFile(WebResponse response, string FileName)
|
|
|
+ private static async Task<bool> SaveBinaryFile(HttpResponseMessage response, string FileName)
|
|
|
{
|
|
|
string FilePath = FileName + DateTime.Now.Millisecond.ToString() + ".mp3";
|
|
|
bool Value = true;
|
|
@@ -1078,20 +1050,18 @@ namespace OASystem.Infrastructure.Repositories.Groups
|
|
|
{
|
|
|
if (File.Exists(FilePath))
|
|
|
File.Delete(FilePath);
|
|
|
- Stream outStream = File.Create(FilePath);
|
|
|
- Stream inStream = response.GetResponseStream();
|
|
|
-
|
|
|
- int l;
|
|
|
- do
|
|
|
+ using (Stream outStream = File.Create(FilePath))
|
|
|
+ using (Stream inStream = await response.Content.ReadAsStreamAsync())
|
|
|
{
|
|
|
- l = inStream.Read(buffer, 0, buffer.Length);
|
|
|
- if (l > 0)
|
|
|
- outStream.Write(buffer, 0, l);
|
|
|
+ int l;
|
|
|
+ do
|
|
|
+ {
|
|
|
+ l = await inStream.ReadAsync(buffer, 0, buffer.Length);
|
|
|
+ if (l > 0)
|
|
|
+ await outStream.WriteAsync(buffer, 0, l);
|
|
|
+ }
|
|
|
+ while (l > 0);
|
|
|
}
|
|
|
- while (l > 0);
|
|
|
-
|
|
|
- outStream.Close();
|
|
|
- inStream.Close();
|
|
|
}
|
|
|
catch
|
|
|
{
|
|
@@ -1116,23 +1086,15 @@ namespace OASystem.Infrastructure.Repositories.Groups
|
|
|
}
|
|
|
public static DateTime GetBeijingTime()
|
|
|
{
|
|
|
- WebRequest request = null;
|
|
|
- WebResponse response = null;
|
|
|
- WebHeaderCollection headerCollection = null;
|
|
|
string datetime = string.Empty;
|
|
|
try
|
|
|
{
|
|
|
- request = WebRequest.Create("https://www.baidu.com");
|
|
|
- request.Timeout = 3000;
|
|
|
- request.Credentials = CredentialCache.DefaultCredentials;
|
|
|
- response = request.GetResponse();
|
|
|
- headerCollection = response.Headers;
|
|
|
- foreach (var h in headerCollection.AllKeys)
|
|
|
+ using (HttpClient client = new HttpClient())
|
|
|
{
|
|
|
- if (h == "Date")
|
|
|
- {
|
|
|
- datetime = headerCollection[h];
|
|
|
- }
|
|
|
+ client.Timeout = TimeSpan.FromSeconds(3);
|
|
|
+ HttpResponseMessage response = client.GetAsync("https://www.baidu.com").Result;
|
|
|
+ response.EnsureSuccessStatusCode();
|
|
|
+ datetime = response.Headers.Date?.ToString() ?? DateTime.Now.ToString();
|
|
|
}
|
|
|
return Convert.ToDateTime(datetime);
|
|
|
}
|
|
@@ -1140,22 +1102,6 @@ namespace OASystem.Infrastructure.Repositories.Groups
|
|
|
{
|
|
|
return DateTime.Now;
|
|
|
}
|
|
|
- finally
|
|
|
- {
|
|
|
- if (request != null)
|
|
|
- {
|
|
|
- request.Abort();
|
|
|
- }
|
|
|
- if (response != null)
|
|
|
- {
|
|
|
- response.Close();
|
|
|
- }
|
|
|
- if (headerCollection != null)
|
|
|
- {
|
|
|
- headerCollection.Clear();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
}
|
|
|
|
|
|
|