using System;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
class Program
{
static async Task Main(string[] args)
{
string word = "你说你,想要逃,剩下空心要不要";
string result = await Translate(word);
Console.WriteLine(result);
}
static async Task<string> Translate(string word)
{
bool isChinese = Regex.IsMatch(word, @"[\u4e00-\u9f5a]");
string fromLang = isChinese ? "zh-Hans" : "en-GB";
string toLang = isChinese ? "en-GB" : "zh-Hans";
string uri = "https://cn.bing.com/translator";
using (HttpClient client = new HttpClient())
{
string gi = await client.GetStringAsync(uri);
string ig = Regex.Match(gi, @"IG:""(.*?)""").Groups[1].Value;
string token = Regex.Match(gi, @"params_AbusePreventionHelper = (.*?);").Groups[1].Value;
string[] tokens = token.Replace("[", "").Split(',');
string t = tokens[1].Substring(1, 32);
string url = $"https://cn.bing.com/ttranslatev3?isVertical=1&&IG={ig}&IID=translator.5027";
var data = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("fromLang", fromLang),
new KeyValuePair<string, string>("text", word),
new KeyValuePair<string, string>("to", toLang),
new KeyValuePair<string, string>("token", t),
new KeyValuePair<string, string>("key", tokens[0]),
new KeyValuePair<string, string>("tryFetchingGenderDebiasedTranslations", "true")
});
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36");
try
{
HttpResponseMessage response = await client.PostAsync(url, data);
string responseString = await response.Content.ReadAsStringAsync();
JArray jsonResponse = JArray.Parse(responseString);
string translatedText = jsonResponse[0]["translations"][0]["text"].ToString();
return translatedText;
}
catch
{
return "翻译失败";
}
}
}
}
如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!
txttool.com 说一段 esp56物联 查询128 IP查询