调用百度接口编写简单工具(c#版本)

先上效果,可以翻译句子和单词,可以中译英和英译中。文末有下载链接。

 

 

主要代码如下,其中需要在百度翻译api申请的id和秘钥,标准版完全免费

百度翻译开放平台 (baidu.com)icon-default.png?t=M1L8https://api.fanyi.baidu.com/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Text;
using System.Net;
using System.IO;
using System.Security.Cryptography;
using System.Web;
using Newtonsoft.Json;


namespace btranslate
{
    public partial class bTranslate : Form
    {
        public bTranslate()
        {
            InitializeComponent();
        }

        private void buttonTranslate_Click(object sender, EventArgs e)
        {
            // 原文
            string q = richTextBoxInput.Text;
            // 源语言
            string from = "zh";
            // 目标语言
            string to = "en";
            // 改成您的APP ID
            string appId = "你的id";
            Random rd = new Random();
            string salt = rd.Next(100000).ToString();
            // 改成您的密钥
            string secretKey = "你的秘钥";
            string sign = EncryptString(appId + q + salt + secretKey);
            string url = "http://api.fanyi.baidu.com/api/trans/vip/translate?";
            url += "q=" + HttpUtility.UrlEncode(q);
            url += "&from=" + from;
            url += "&to=" + to;
            url += "&appid=" + appId;
            url += "&salt=" + salt;
            url += "&sign=" + sign;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";
            request.UserAgent = null;
            request.Timeout = 6000;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            Console.WriteLine(retString);
            Console.ReadLine();
            richTextBoxResult.Clear();

            PostResult res = JsonConvert.DeserializeObject<PostResult>(retString);
            if (res.Error_code == null)
            {
                richTextBoxResult.AppendText(res.Trans_result[0].Dst);
            }else
                richTextBoxResult.AppendText(res.Error_msg);


        }

        public static string EncryptString(string str)
        {
            MD5 md5 = MD5.Create();
            // 将字符串转换成字节数组
            byte[] byteOld = Encoding.UTF8.GetBytes(str);
            // 调用加密方法
            byte[] byteNew = md5.ComputeHash(byteOld);
            // 将加密结果转换为字符串
            StringBuilder sb = new StringBuilder();
            foreach (byte b in byteNew)
            {
                // 将字节转换成16进制表示的字符串,
                sb.Append(b.ToString("x2"));
            }
            // 返回加密的字符串
            return sb.ToString();
        }

        private void buttonTranslateEn2Zh_Click(object sender, EventArgs e)
        {
            // 原文
            string q = richTextBoxInput.Text;
            // 源语言
            string from = "en";
            // 目标语言
            string to = "zh";
            // 改成您的APP ID
            string appId = "你的id";
            Random rd = new Random();
            string salt = rd.Next(100000).ToString();
            // 改成您的密钥
            string secretKey = "你的秘钥";
            string sign = EncryptString(appId + q + salt + secretKey);
            string url = "http://api.fanyi.baidu.com/api/trans/vip/translate?";
            url += "q=" + HttpUtility.UrlEncode(q);
            url += "&from=" + from;
            url += "&to=" + to;
            url += "&appid=" + appId;
            url += "&salt=" + salt;
            url += "&sign=" + sign;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";
            request.UserAgent = null;
            request.Timeout = 6000;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            Console.WriteLine(retString);
            Console.ReadLine();
            richTextBoxResult.Clear();

            PostResult res = JsonConvert.DeserializeObject<PostResult>(retString);
            if (res.Error_code == null)
            {
                richTextBoxResult.AppendText(res.Trans_result[0].Dst);
            }
            else
                richTextBoxResult.AppendText(res.Error_msg);
        }
    }

    public class PostResult
    {
        public string Error_code { set; get; }
        public string Error_msg { set; get; }
        public string From { set; get; }
        public string To { set; get; }
        public TranslateContent[] Trans_result { set; get; }
    }
    public class TranslateContent
    {
        public string Src { set; get; }
        public string Dst { set; get; }
    }

}

传输链接:https://cowtransfer.com/s/7ca02ee3683d4c 或 打开【奶牛快传】cowtransfer.com 使用传输口令:3yhe92 提取; 

posted @ 2022-08-21 10:12  Oliver2022  阅读(124)  评论(0编辑  收藏  举报