Skip to content
This repository has been archived by the owner on Aug 17, 2024. It is now read-only.

Commit

Permalink
优化阿里API请求方式
Browse files Browse the repository at this point in the history
  • Loading branch information
Isayama-Kagura committed Aug 11, 2022
1 parent e81014e commit d9cf188
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
28 changes: 19 additions & 9 deletions TsubakiTranslator/TranslateAPILibrary/AliyunTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public string Translate(string sourceText)

string method = "POST";
string accept = "application/json";
//string contentType = "application/json";
string contentType = "application/json; charset=utf-8";
string date = DateTime.UtcNow.ToString("r");
string host = "mt.cn-hangzhou.aliyuncs.com";
string path = "/api/translate/web/general";
Expand All @@ -56,7 +56,7 @@ public string Translate(string sourceText)

string uuid = Guid.NewGuid().ToString();

string stringToSign = method + "\n" + accept + "\n" + bodyMd5 + "\n" + host + "\n" + date + "\n"
string stringToSign = method + "\n" + accept + "\n" + bodyMd5 + "\n" + contentType + "\n" + date + "\n"
+ "x-acs-signature-method:HMAC-SHA1\n"
+ "x-acs-signature-nonce:" + uuid + "\n"
+ path;
Expand All @@ -70,12 +70,12 @@ public string Translate(string sourceText)

string authHeader = "acs " + SecretId + ":" + signature;

var client = new RestClient($"https://{host}");
var request = new RestRequest(path,Method.Post);
var client = new RestClient(CommonFunction.Client);
var request = new RestRequest($"https://{host}{path}",Method.Post);
request.AddHeader("Authorization", authHeader);
request.AddHeader("Accept", accept);
request.AddHeader("Content-MD5", bodyMd5);
request.AddHeader("Content-Type", host);
//request.AddHeader("Content-Type", host);
request.AddHeader("Date", date);
request.AddHeader("x-acs-signature-method", "HMAC-SHA1");
request.AddHeader("x-acs-signature-nonce", uuid);
Expand All @@ -85,10 +85,20 @@ public string Translate(string sourceText)

var response = client.Execute(request);

Regex reg = new Regex(@"""Translated"":""(.*?)""");
Match match = reg.Match(response.Content);
string result = match.Groups[1].Value;
return result;
string result = response.Content;
Regex codeReg = new Regex("},\"Code\":\"200\"");
if (codeReg.IsMatch(result))
{
Regex reg = new Regex(@",""Translated"":""(.*?)""},");
Match match = reg.Match(response.Content);
result = match.Groups[1].Value;
return result;
}
else
{
return result;
}


}

Expand Down
2 changes: 1 addition & 1 deletion TsubakiTranslator/TsubakiTranslator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<Company>OTAKU Technology Co., Ltd.</Company>
<PackageProjectUrl>https://github.com/Isayama-Kagura/TsubakiTranslator</PackageProjectUrl>
<Copyright>© 2021 Tsubaki Translator</Copyright>
<Version>1.0.4.1</Version>
<Version>1.0.4.2</Version>
<Product>Tsubaki Translator</Product>
</PropertyGroup>

Expand Down

0 comments on commit d9cf188

Please sign in to comment.