阿里云机器翻译NET使用Demo

Step By Step


机器翻译封装的SDK调用

1、SDK安装:aliyun-net-sdk-alimt
阿里云机器翻译NET使用Demo

2、Code Sample

using System;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.alimt.Model.V20181012;

namespace AlimtDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", "LTAIOZZgYX******", "v7CjUJCMk7j9aKduMAQLjy********");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            TranslateGeneralRequest translateGeneralRequest = new TranslateGeneralRequest();
            translateGeneralRequest.Method = Aliyun.Acs.Core.Http.MethodType.POST;

            translateGeneralRequest.FormatType = "text";
            translateGeneralRequest.TargetLanguage = "en";
            translateGeneralRequest.SourceLanguage = "zh";
            translateGeneralRequest.SourceText = "北京欢迎你";
            translateGeneralRequest.Scene = "general";

            try
            {
                var response = client.GetAcsResponse(translateGeneralRequest);
                Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));

                Console.ReadKey();
            }
            catch (ServerException e)
            {
                Console.WriteLine(e);
            }
            catch (ClientException e)
            {
                Console.WriteLine(e);
            }

            Console.ReadKey();
        }
    }
}

3、The Result

{"RequestId":"C4B626D3-AA4B-419B-9499-7799015AA88A","Data":{"Translated":"Welcome to Beijing"},"Code":"200"}

NET Core SDK 调用

1、SDK安装:aliyun-net-sdk-core

阿里云机器翻译NET使用Demo

2、Code Sample

using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using System;

namespace CoreSDKDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", "LTAIOZZgYX******", "v7CjUJCMk7j9aKduMAQLjy********");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            CommonRequest commonRequest = new CommonRequest();

            commonRequest.Action = "TranslateGeneral";
            commonRequest.Version = "2018-10-12";
            commonRequest.Method = Aliyun.Acs.Core.Http.MethodType.POST;
            commonRequest.Domain = "mt.cn-hangzhou.aliyuncs.com";

            commonRequest.AddBodyParameters("FormatType", "text");

            commonRequest.AddBodyParameters("Scene", "general");
            commonRequest.AddBodyParameters("SourceLanguage", "zh");
            commonRequest.AddBodyParameters("SourceText", "中国人民*");
            commonRequest.AddBodyParameters("TargetLanguage", "en");
            CommonResponse response = null;

            // Initiate the request and get the response
            response = client.GetCommonResponse(commonRequest);
            Console.WriteLine("Result:" + response.Data);

            Console.ReadKey();
        }
    }
}

3、The Result

Result:{"RequestId":"7EF04C0F-4FD6-423A-BE77-F2CA2E3CD18A","Data":{"Translated":"People's *"},"Code":"200"}

更多参考

机器翻译通用版调用指南
阿里云常见参数获取位置

上一篇:阿里云机器翻译NET使用Demo


下一篇:VB实现SHELL扩展之接口参数获取失败探析