C# 实现的几种在线翻译

Google翻译

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace Google
{
    class Program
    {
        static void Main(string[] args)
        {
            Google();
            Console.ReadKey();
        }

        public static void Google()
        {
            string url = "https://translate.google.cn/_/TranslateWebserverUi/data/batchexecute?rpcids=MkEWBc&bl=boq_translate-webserver_20210927.13_p0&soc-app=1&soc-platform=1&soc-device=1&rt=c";
            string q = "The invention relates to a \"three-dimensional structure\" obtained from a tubular preform, which is obtained from a net of the Chebyshev type (with such a structure also being called \"elastic gridshell\") having a flared shape maintained by means selected from the group comprising hoops (5, 7, 9), spacers (11) and cables (11). The invention also relates to a method for installing such a structure, in which method the tubular preform is brought to the installation point, then deformations are applied to this preform that allow the final desired flared shape to be achieved. Particular application to the production of street furniture allowing shaded areas to be created in towns by cultivating everything that is fixed on a heavy container containing cultivated soil and the necessary devices.";
            string from = "auto";
            string to = "zh";
            var from_data = "f.req=" + System.Web.HttpUtility.UrlEncode(
                string.Format("[[[\"MkEWBc\",\"[[\\\"{0}\\\",\\\"{1}\\\",\\\"{2}\\\",true],[null]]\", null, \"generic\"]]]",
                ReplaceString(q), from, to), Encoding.UTF8).Replace("+", "%20");
            byte[] postData = Encoding.UTF8.GetBytes(from_data);
            WebClient client = new WebClient();
            client.Encoding = Encoding.UTF8;
            client.Headers.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            client.Headers.Add("ContentLength", postData.Length.ToString());
            client.Headers.Add("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
            client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36");
            byte[] responseData = client.UploadData(url, "POST", postData);
            string content = Encoding.UTF8.GetString(responseData);
            Console.WriteLine(MatchResult(content));
        }

        /// <summary>
        /// 匹配翻译结果
        /// </summary>
        /// <returns></returns>
        public static string MatchResult(string content)
        {
            string result = "";
            string patttern = @",\[\[\\\""(.*?)\\\"",";
            Regex regex = new Regex(patttern);
            MatchCollection matchcollection = regex.Matches(content);
            if (matchcollection.Count > 0)
            {
                List<string> list = new List<string>();
                foreach (Match match in matchcollection)
                {
                    list.Add(match.Groups[1].Value);
                }
              
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值