-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaiduTranslate.cs
180 lines (156 loc) · 5.82 KB
/
BaiduTranslate.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System;
using System.Text;
using System.Net;
using System.IO;
using System.Security.Cryptography;
using System.Web;
using HDF.DeskBand;
using System.Windows.Forms;
namespace Baidu
{
public class BaiduTranslate
{
/// <summary>
/// 通用翻译api实体
/// </summary>
public class BaiduTranslateApiResult
{
public string from { get; set; }
public string to { get; set; }
public TranslateResult[] trans_result { get; set; }
public string error_code { get; set; }
public class TranslateResult
{
public string src { get; set; }
public string dst { get; set; }
}
}
/// <summary>
/// 语种识别api实体
/// </summary>
public class BaiduLanguageApiResult
{
public string error_code { get; set; }
public string error_msg { get; set; }
public LanguageResult data { get; set; }
public class LanguageResult
{
public string src { get; set; }
}
}
public static string AppID;
public static string SecretKey;
public static BaiduTranslateApiResult Translate(string str, string from = "auto", string to = "auto")
{
if (string.IsNullOrEmpty(AppID) || string.IsNullOrEmpty(SecretKey))
return null;
// 原文
string q = str;
// 源语言
//string from = "en";
// 目标语言
//string to = "zh";
// 改成您的APP ID
string appId = AppID;
Random rd = new Random();
string salt = rd.Next(100000).ToString();
// 改成您的密钥
string secretKey = 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;
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (Stream myResponseStream = response.GetResponseStream())
{
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<BaiduTranslateApiResult>(retString);
return obj;
}
}
catch (Exception ex)
{
Log.Write(ex.Message);
return null;
}
}
public static BaiduLanguageApiResult Language(string str)
{
if (string.IsNullOrEmpty(AppID) || string.IsNullOrEmpty(SecretKey))
return null;
// 原文
string q = str;
// 源语言
//string from = "en";
// 目标语言
//string to = "zh";
// 改成您的APP ID
string appId = AppID;
Random rd = new Random();
string salt = rd.Next(100000).ToString();
// 改成您的密钥
string secretKey = SecretKey;
string sign = EncryptString(appId + q + salt + secretKey);
string url = "http://api.fanyi.baidu.com/api/trans/vip/language?";
url += "q=" + HttpUtility.UrlEncode(q);
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;
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (Stream myResponseStream = response.GetResponseStream())
{
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<BaiduLanguageApiResult>(retString);
return obj;
}
}
catch (Exception ex)
{
Log.Write(ex.Message);
return null;
}
}
// 计算MD5值
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();
}
}
}