|
|
@ -0,0 +1,642 @@
|
|
|
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
|
|
using System.Net;
|
|
|
|
|
|
|
|
using System.Net.Security;
|
|
|
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
|
|
|
using System.Security.Cryptography.X509Certificates;
|
|
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
using System.Web;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace WpfApp1.transToNet
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
internal class jsonNet : Messiah
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
static string https = "http://api.fanyi.baidu.com/api/trans/vip/translate?";
|
|
|
|
|
|
|
|
static string appid = "20230925001829663";//20230925001829663,20230923001827495
|
|
|
|
|
|
|
|
static Random rd = new Random();
|
|
|
|
|
|
|
|
static string salt = rd.Next(100000).ToString();
|
|
|
|
|
|
|
|
static string key = "f7ZHpw28izQ1VU_ixoY5";//f7ZHpw28izQ1VU_ixoY5,yjvMNY33Qc1TYSBjTQYQ
|
|
|
|
|
|
|
|
//baidu
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static string xunweb = "https://itrans.xf-yun.com/v1/its";
|
|
|
|
|
|
|
|
static string xunid = "1299c840";
|
|
|
|
|
|
|
|
static string xunsec = "M2Q4YzkzZTIwZjNkMGE2ZjkwZWMxNzZk";
|
|
|
|
|
|
|
|
static string xunkey = "828cb2dc915d28464a904bda5d7b01b3";
|
|
|
|
|
|
|
|
static string xunfl = "en";//ja
|
|
|
|
|
|
|
|
static string xundl = "cn";
|
|
|
|
|
|
|
|
static string xuntxt = "Sakura miko";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//static HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://api.fanyi.baidu.com/api/trans/vip/translate?q=apple&from=en&to=zh&appid=20230925001829663&salt=1435660288&sign=c963bc2275c0fd1ed30da014c1ef1933");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
private static HttpClient HttpRe = new HttpClient()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
BaseAddress = new Uri("http://api.fanyi.baidu.com/api/trans/vip/translate?"),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static async Task GetAsync(HttpClient httpc)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
using HttpResponseMessage response = await httpc.GetAsync(temp);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//var value = response.EnsureSuccessStatusCode().WriteRequestToConsole();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var jsonResponse = await response.Content.ReadAsStringAsync();//收到json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Console.WriteLine($"{jsonResponse}\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private HttpClient HttpRequestC(string url)//创建HTTP
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
HttpClient httpClient = new HttpClient()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
BaseAddress = new Uri(url)
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
return httpClient;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static string EncryptString(string str)//百度MD5加密计算方式
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
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 static string HmacSha256(string message, string secret)//hmachas256加密
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
byte[] keyBytes = Encoding.UTF8.GetBytes(secret);
|
|
|
|
|
|
|
|
byte[] messageBytes = Encoding.UTF8.GetBytes(message);
|
|
|
|
|
|
|
|
using (HMACSHA256 hmac = new HMACSHA256(keyBytes))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
byte[] signatureBytes = hmac.ComputeHash(messageBytes);
|
|
|
|
|
|
|
|
return Convert.ToBase64String(signatureBytes);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static string Sha256(string message)//SHA-256加密
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
byte[] messageBytes = Encoding.UTF8.GetBytes(message);
|
|
|
|
|
|
|
|
using (SHA256 sha256 = SHA256.Create())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
byte[] signatureBytes = sha256.ComputeHash(messageBytes);
|
|
|
|
|
|
|
|
return Convert.ToBase64String(signatureBytes);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static string signBody(string body)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
byte[] messageBytes = Encoding.UTF8.GetBytes(body);
|
|
|
|
|
|
|
|
using (SHA256 sha256 = SHA256.Create())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
byte[] signatureBytes = sha256.ComputeHash(messageBytes);
|
|
|
|
|
|
|
|
string signature = Convert.ToBase64String(signatureBytes);
|
|
|
|
|
|
|
|
return hmacsign(signature, xunsec);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static string hmacsign(string signature, string apiSecret)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
byte[] keyBytes = Encoding.UTF8.GetBytes(apiSecret);
|
|
|
|
|
|
|
|
byte[] messageBytes = Encoding.UTF8.GetBytes(signature);
|
|
|
|
|
|
|
|
using (HMACSHA256 hmac = new HMACSHA256(keyBytes))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
byte[] signatureBytes = hmac.ComputeHash(messageBytes);
|
|
|
|
|
|
|
|
return Convert.ToBase64String(signatureBytes);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static string ComputeSha256(string message)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
byte[] messageBytes = Encoding.UTF8.GetBytes(message);
|
|
|
|
|
|
|
|
using (SHA256 sha256 = SHA256.Create())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
byte[] signatureBytes = sha256.ComputeHash(messageBytes);
|
|
|
|
|
|
|
|
return Convert.ToBase64String(signatureBytes);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static string BuildHttpBody()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Dictionary<string, object> paramMap = new()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
{ "from", "cn" },
|
|
|
|
|
|
|
|
{ "to", "en" },
|
|
|
|
|
|
|
|
{ "appid", xunid },
|
|
|
|
|
|
|
|
{ "salt", DateTime.Now.Millisecond.ToString() },
|
|
|
|
|
|
|
|
{ "signType", "v3" },
|
|
|
|
|
|
|
|
{ "signature", "" },
|
|
|
|
|
|
|
|
{ "voice", "0" },
|
|
|
|
|
|
|
|
{ "text", "你好,世界!" }
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
string body = JsonConvert.SerializeObject(paramMap);
|
|
|
|
|
|
|
|
string signature = signBody(body);
|
|
|
|
|
|
|
|
paramMap["signature"] = signature;
|
|
|
|
|
|
|
|
return JsonConvert.SerializeObject(paramMap);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static Dictionary<string, string> BuildHttpHeader(string body)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
string curTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
|
|
|
string param = xunkey + curTime + body;
|
|
|
|
|
|
|
|
string checkSum = ComputeSha256(param);
|
|
|
|
|
|
|
|
Dictionary<string, string> header = new()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
{ "Content-Type", "application/json" },
|
|
|
|
|
|
|
|
{ "X-Appid", xunid },
|
|
|
|
|
|
|
|
{ "X-CurTime", curTime },
|
|
|
|
|
|
|
|
{ "X-Param", body },
|
|
|
|
|
|
|
|
{ "X-CheckSum", checkSum }
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
return header;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public string RequestJsonPrivateBaiDu(string input, string srcl, string dstl, string aid, string kkey)//自定义id,key以及源语言和目标语言
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
string sign = EncryptString(aid + input + salt + kkey);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string temp = "q=" + HttpUtility.UrlEncode(input) + "&from=" + srcl + "&to=" + dstl + "&appid=" + aid + "&salt=" + salt + "&sign=" + sign;
|
|
|
|
|
|
|
|
Console.WriteLine(https + temp);
|
|
|
|
|
|
|
|
//Task t = GetAsync(HttpRe); t.Wait();
|
|
|
|
|
|
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(https + temp);
|
|
|
|
|
|
|
|
request.Method = "GET";
|
|
|
|
|
|
|
|
request.ContentType = "text/html;charset=UTF-8";
|
|
|
|
|
|
|
|
request.UserAgent = null;
|
|
|
|
|
|
|
|
request.Timeout = 6000;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var response = request.GetResponse();
|
|
|
|
|
|
|
|
var myResponseStream = response.GetResponseStream();
|
|
|
|
|
|
|
|
var myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
|
|
|
|
|
|
|
|
string retString = myStreamReader.ReadToEnd();
|
|
|
|
|
|
|
|
tempC2 temp1 = JsonConvert.DeserializeObject<tempC2>(retString);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Trans_result result = temp1.trans_result[0];
|
|
|
|
|
|
|
|
string s = result.dst;
|
|
|
|
|
|
|
|
return s;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public string RequestJsonLanguageChangeBaiDu(string input, string srcl, string dstl)//可以自定义源语言与目标语言
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
string sign = EncryptString(appid + input + salt + key);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string temp = "q=" + HttpUtility.UrlEncode(input) + "&from=" + srcl + "&to=" + dstl + "&appid=" + appid + "&salt=" + salt + "&sign=" + sign;
|
|
|
|
|
|
|
|
Console.WriteLine(https + temp);
|
|
|
|
|
|
|
|
//Task t = GetAsync(HttpRe); t.Wait();
|
|
|
|
|
|
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(https + temp);
|
|
|
|
|
|
|
|
request.Method = "GET";
|
|
|
|
|
|
|
|
request.ContentType = "text/html;charset=UTF-8";
|
|
|
|
|
|
|
|
request.UserAgent = null;
|
|
|
|
|
|
|
|
request.Timeout = 6000;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var response = request.GetResponse();
|
|
|
|
|
|
|
|
var myResponseStream = response.GetResponseStream();
|
|
|
|
|
|
|
|
var myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
|
|
|
|
|
|
|
|
string retString = myStreamReader.ReadToEnd();
|
|
|
|
|
|
|
|
tempC2 temp1 = JsonConvert.DeserializeObject<tempC2>(retString);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Trans_result result = temp1.trans_result[0];
|
|
|
|
|
|
|
|
string s = result.dst;
|
|
|
|
|
|
|
|
return s;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public string RequestJsonBaiDu(string input)//默认
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
string sign = EncryptString(appid + input + salt + key);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string temp = "q=" + HttpUtility.UrlEncode(input) + "&from=jp&to=zh&appid=" + appid + "&salt=" + salt + "&sign=" + sign;
|
|
|
|
|
|
|
|
Console.WriteLine(https + temp);
|
|
|
|
|
|
|
|
//Task t = GetAsync(HttpRe); t.Wait();
|
|
|
|
|
|
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(https + temp);
|
|
|
|
|
|
|
|
request.Method = "GET";
|
|
|
|
|
|
|
|
request.ContentType = "text/html;charset=UTF-8";
|
|
|
|
|
|
|
|
request.UserAgent = null;
|
|
|
|
|
|
|
|
request.Timeout = 6000;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var response = request.GetResponse();
|
|
|
|
|
|
|
|
var myResponseStream = response.GetResponseStream();
|
|
|
|
|
|
|
|
var myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
|
|
|
|
|
|
|
|
string retString = myStreamReader.ReadToEnd();
|
|
|
|
|
|
|
|
tempC2 temp1 = JsonConvert.DeserializeObject<tempC2>(retString);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Trans_result result = temp1.trans_result[0];
|
|
|
|
|
|
|
|
string s = result.dst;
|
|
|
|
|
|
|
|
return s;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public string RequestJsonXunFei(string input)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
// 接口地址
|
|
|
|
|
|
|
|
string host = "itrans.xfyun.cn";
|
|
|
|
|
|
|
|
string url = string.Format("https://{0}/v2/its", host);
|
|
|
|
|
|
|
|
// 原文
|
|
|
|
|
|
|
|
string q = "amyotrophic lateral sclerosis";
|
|
|
|
|
|
|
|
q = input;
|
|
|
|
|
|
|
|
// 源语言
|
|
|
|
|
|
|
|
string from = "ja";
|
|
|
|
|
|
|
|
// 目标语言
|
|
|
|
|
|
|
|
string to = "cn";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string apikey = "a31395d71149fc5f1e73353f56d3d726";
|
|
|
|
|
|
|
|
string apisecret = "YWVhYWUyM2YyMTAyMDdhYTk3YTRhMjli";
|
|
|
|
|
|
|
|
string appid = "56e76335";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string requestJson = HttpUtil.BuildXunFeiRequestJson(appid, from, to, q);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string Digest = "SHA-256=" + HttpUtil.ComputeHash256_base64(requestJson, new SHA256CryptoServiceProvider());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DateTime dateTime = DateTime.UtcNow;
|
|
|
|
|
|
|
|
string dateStr = dateTime.ToString("r");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string signature = string.Format("host: {0}\ndate: {1}\nPOST /v2/its HTTP/1.1\ndigest: {2}", host, dateStr, Digest);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string signature_sha = HttpUtil.hmacsha256(signature, apisecret);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string Authorization = string.Format("api_key=\"{0}\", algorithm=\"hmac-sha256\", headers=\"host date request-line digest\", signature=\"{1}\"", apikey, signature_sha);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//post
|
|
|
|
|
|
|
|
byte[] byteArray = Encoding.UTF8.GetBytes(requestJson);
|
|
|
|
|
|
|
|
HttpWebRequest httpWebRequest;
|
|
|
|
|
|
|
|
//HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
|
|
|
|
|
|
|
|
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(HttpUtil.CheckValidationResult);
|
|
|
|
|
|
|
|
httpWebRequest.ProtocolVersion = HttpVersion.Version11;
|
|
|
|
|
|
|
|
// 这里设置了协议类型。
|
|
|
|
|
|
|
|
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;// SecurityProtocolType.Tls1.2;
|
|
|
|
|
|
|
|
httpWebRequest.KeepAlive = false;
|
|
|
|
|
|
|
|
ServicePointManager.CheckCertificateRevocationList = true;
|
|
|
|
|
|
|
|
ServicePointManager.DefaultConnectionLimit = 100;
|
|
|
|
|
|
|
|
ServicePointManager.Expect100Continue = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
httpWebRequest.Method = "POST";
|
|
|
|
|
|
|
|
httpWebRequest.ContentType = "application/json";
|
|
|
|
|
|
|
|
httpWebRequest.ContentLength = byteArray.Length;
|
|
|
|
|
|
|
|
httpWebRequest.Accept = "application/json";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
httpWebRequest.Date = dateTime;
|
|
|
|
|
|
|
|
httpWebRequest.Host = host;
|
|
|
|
|
|
|
|
httpWebRequest.Headers.Add("Digest", Digest);
|
|
|
|
|
|
|
|
httpWebRequest.Headers.Add("Authorization", Authorization);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
httpWebRequest.KeepAlive = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int respondCode = 0;
|
|
|
|
|
|
|
|
string translation = string.Empty;
|
|
|
|
|
|
|
|
string respondStr = string.Empty;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
using (Stream reqStream = httpWebRequest.GetRequestStream())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
reqStream.Write(byteArray, 0, byteArray.Length);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using (HttpWebResponse webResponse = (HttpWebResponse)httpWebRequest.GetResponse())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
respondCode = (int)webResponse.StatusCode;
|
|
|
|
|
|
|
|
if (respondCode == 200)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
using (StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
respondStr = sr.ReadToEnd();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
dynamic TempResult = JsonConvert.DeserializeObject(respondStr);
|
|
|
|
|
|
|
|
respondStr = Convert.ToString(TempResult["data"]["result"]["trans_result"]["dst"]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return respondStr;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public string RequestJsonPrivateXunFei(string input, string srcl, string dstl, string xid, string xse , string xkey)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
// 接口地址
|
|
|
|
|
|
|
|
string host = "itrans.xfyun.cn";
|
|
|
|
|
|
|
|
string url = string.Format("https://{0}/v2/its", host);
|
|
|
|
|
|
|
|
// 原文
|
|
|
|
|
|
|
|
string q = "";
|
|
|
|
|
|
|
|
q = input;
|
|
|
|
|
|
|
|
// 源语言
|
|
|
|
|
|
|
|
string from = srcl;
|
|
|
|
|
|
|
|
// 目标语言
|
|
|
|
|
|
|
|
string to = dstl; //"cn";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string apikey = xkey;
|
|
|
|
|
|
|
|
string apisecret = xse;
|
|
|
|
|
|
|
|
string appid = xid;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string requestJson = HttpUtil.BuildXunFeiRequestJson(appid, from, to, q);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string Digest = "SHA-256=" + HttpUtil.ComputeHash256_base64(requestJson, new SHA256CryptoServiceProvider());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DateTime dateTime = DateTime.UtcNow;
|
|
|
|
|
|
|
|
string dateStr = dateTime.ToString("r");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string signature = string.Format("host: {0}\ndate: {1}\nPOST /v2/its HTTP/1.1\ndigest: {2}", host, dateStr, Digest);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string signature_sha = HttpUtil.hmacsha256(signature, apisecret);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string Authorization = string.Format("api_key=\"{0}\", algorithm=\"hmac-sha256\", headers=\"host date request-line digest\", signature=\"{1}\"", apikey, signature_sha);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//post
|
|
|
|
|
|
|
|
byte[] byteArray = Encoding.UTF8.GetBytes(requestJson);
|
|
|
|
|
|
|
|
HttpWebRequest httpWebRequest;
|
|
|
|
|
|
|
|
//HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
|
|
|
|
|
|
|
|
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(HttpUtil.CheckValidationResult);
|
|
|
|
|
|
|
|
httpWebRequest.ProtocolVersion = HttpVersion.Version11;
|
|
|
|
|
|
|
|
// 这里设置了协议类型。
|
|
|
|
|
|
|
|
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;// SecurityProtocolType.Tls1.2;
|
|
|
|
|
|
|
|
httpWebRequest.KeepAlive = false;
|
|
|
|
|
|
|
|
ServicePointManager.CheckCertificateRevocationList = true;
|
|
|
|
|
|
|
|
ServicePointManager.DefaultConnectionLimit = 100;
|
|
|
|
|
|
|
|
ServicePointManager.Expect100Continue = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
httpWebRequest.Method = "POST";
|
|
|
|
|
|
|
|
httpWebRequest.ContentType = "application/json";
|
|
|
|
|
|
|
|
httpWebRequest.ContentLength = byteArray.Length;
|
|
|
|
|
|
|
|
httpWebRequest.Accept = "application/json";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
httpWebRequest.Date = dateTime;
|
|
|
|
|
|
|
|
httpWebRequest.Host = host;
|
|
|
|
|
|
|
|
httpWebRequest.Headers.Add("Digest", Digest);
|
|
|
|
|
|
|
|
httpWebRequest.Headers.Add("Authorization", Authorization);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
httpWebRequest.KeepAlive = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int respondCode = 0;
|
|
|
|
|
|
|
|
string translation = string.Empty;
|
|
|
|
|
|
|
|
string respondStr = string.Empty;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
using (Stream reqStream = httpWebRequest.GetRequestStream())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
reqStream.Write(byteArray, 0, byteArray.Length);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using (HttpWebResponse webResponse = (HttpWebResponse)httpWebRequest.GetResponse())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
respondCode = (int)webResponse.StatusCode;
|
|
|
|
|
|
|
|
if (respondCode == 200)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
using (StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
respondStr = sr.ReadToEnd();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
dynamic TempResult = JsonConvert.DeserializeObject(respondStr);
|
|
|
|
|
|
|
|
// 这是返回的 译文
|
|
|
|
|
|
|
|
respondStr = Convert.ToString(TempResult["data"]["result"]["trans_result"]["dst"]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return respondStr;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public string RequestJsonLanguageChangeXunFei(string input, string srcl, string dstl)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
// 接口地址
|
|
|
|
|
|
|
|
string host = "itrans.xfyun.cn";
|
|
|
|
|
|
|
|
string url = string.Format("https://{0}/v2/its", host);
|
|
|
|
|
|
|
|
// 原文
|
|
|
|
|
|
|
|
string q = "";
|
|
|
|
|
|
|
|
q = input;
|
|
|
|
|
|
|
|
// 源语言
|
|
|
|
|
|
|
|
string from = srcl;
|
|
|
|
|
|
|
|
// 目标语言
|
|
|
|
|
|
|
|
string to = dstl;
|
|
|
|
|
|
|
|
string apikey = "a31395d71149fc5f1e73353f56d3d726";
|
|
|
|
|
|
|
|
string apisecret = "YWVhYWUyM2YyMTAyMDdhYTk3YTRhMjli";
|
|
|
|
|
|
|
|
string appid = "56e76335";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string requestJson = HttpUtil.BuildXunFeiRequestJson(appid, from, to, q);
|
|
|
|
|
|
|
|
string Digest = "SHA-256=" + HttpUtil.ComputeHash256_base64(requestJson, new SHA256CryptoServiceProvider());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DateTime dateTime = DateTime.UtcNow;
|
|
|
|
|
|
|
|
string dateStr = dateTime.ToString("r");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string signature = string.Format("host: {0}\ndate: {1}\nPOST /v2/its HTTP/1.1\ndigest: {2}", host, dateStr, Digest);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string signature_sha = HttpUtil.hmacsha256(signature, apisecret);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string Authorization = string.Format("api_key=\"{0}\", algorithm=\"hmac-sha256\", headers=\"host date request-line digest\", signature=\"{1}\"", apikey, signature_sha);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//post
|
|
|
|
|
|
|
|
byte[] byteArray = Encoding.UTF8.GetBytes(requestJson);
|
|
|
|
|
|
|
|
HttpWebRequest httpWebRequest;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
|
|
|
|
|
|
|
|
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(HttpUtil.CheckValidationResult);
|
|
|
|
|
|
|
|
httpWebRequest.ProtocolVersion = HttpVersion.Version11;
|
|
|
|
|
|
|
|
// 这里设置了协议类型。
|
|
|
|
|
|
|
|
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;// SecurityProtocolType.Tls1.2;
|
|
|
|
|
|
|
|
httpWebRequest.KeepAlive = false;
|
|
|
|
|
|
|
|
ServicePointManager.CheckCertificateRevocationList = true;
|
|
|
|
|
|
|
|
ServicePointManager.DefaultConnectionLimit = 100;
|
|
|
|
|
|
|
|
ServicePointManager.Expect100Continue = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
httpWebRequest.Method = "POST";
|
|
|
|
|
|
|
|
httpWebRequest.ContentType = "application/json";
|
|
|
|
|
|
|
|
httpWebRequest.ContentLength = byteArray.Length;
|
|
|
|
|
|
|
|
httpWebRequest.Accept = "application/json";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
httpWebRequest.Date = dateTime;
|
|
|
|
|
|
|
|
httpWebRequest.Host = host;
|
|
|
|
|
|
|
|
httpWebRequest.Headers.Add("Digest", Digest);
|
|
|
|
|
|
|
|
httpWebRequest.Headers.Add("Authorization", Authorization);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
httpWebRequest.KeepAlive = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int respondCode = 0;
|
|
|
|
|
|
|
|
string translation = string.Empty;
|
|
|
|
|
|
|
|
string respondStr = string.Empty;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
using (Stream reqStream = httpWebRequest.GetRequestStream())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
reqStream.Write(byteArray, 0, byteArray.Length);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using (HttpWebResponse webResponse = (HttpWebResponse)httpWebRequest.GetResponse())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
respondCode = (int)webResponse.StatusCode;
|
|
|
|
|
|
|
|
if (respondCode == 200)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
using (StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
respondStr = sr.ReadToEnd();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
dynamic TempResult = JsonConvert.DeserializeObject(respondStr);
|
|
|
|
|
|
|
|
// 接收到的 译文
|
|
|
|
|
|
|
|
respondStr = Convert.ToString(TempResult["data"]["result"]["trans_result"]["dst"]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return respondStr;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Trans_result
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
public string src { get; set; }
|
|
|
|
|
|
|
|
public string dst { get; set; }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class tempC2
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
public List<Trans_result> trans_result { get; set; }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class ResponseData
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
public int code { get; set; }
|
|
|
|
|
|
|
|
public string message { get; set; }
|
|
|
|
|
|
|
|
public string sid { get; set; }
|
|
|
|
|
|
|
|
public List<Result> data { get; set; }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class Result
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
public string src { get; set; }
|
|
|
|
|
|
|
|
public string tgt { get; set; }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 讯飞
|
|
|
|
|
|
|
|
class XunFeiRequestClass
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
[JsonProperty("common")]
|
|
|
|
|
|
|
|
public common c { get; set; }
|
|
|
|
|
|
|
|
[JsonProperty("business")]
|
|
|
|
|
|
|
|
public business b { get; set; }
|
|
|
|
|
|
|
|
[JsonProperty("data")]
|
|
|
|
|
|
|
|
public data d { get; set; }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class common
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
[JsonProperty("app_id")]
|
|
|
|
|
|
|
|
public string app_id { get; set; }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class business
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
[JsonProperty("from")]
|
|
|
|
|
|
|
|
public string from { get; set; }
|
|
|
|
|
|
|
|
[JsonProperty("to")]
|
|
|
|
|
|
|
|
public string to { get; set; }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class data
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
[JsonProperty("text")]
|
|
|
|
|
|
|
|
public string text { get; set; }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class HttpUtil//讯飞用
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//计算字符哈希值
|
|
|
|
|
|
|
|
public static string ComputeHash256_base64(string input, HashAlgorithm algorithm)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Byte[] inputBytes = Encoding.UTF8.GetBytes(input);
|
|
|
|
|
|
|
|
Byte[] hashedBytes = algorithm.ComputeHash(inputBytes);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Convert.ToBase64String(hashedBytes);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//构建讯飞翻译API的请求参数的JSON字符串
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static string BuildXunFeiRequestJson(string appid, string from, string to, string text)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
common c = new common();
|
|
|
|
|
|
|
|
c.app_id = appid;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
business b = new business();
|
|
|
|
|
|
|
|
b.from = from;
|
|
|
|
|
|
|
|
b.to = to;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data d = new data();
|
|
|
|
|
|
|
|
d.text = EncryptBase64(text);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
XunFeiRequestClass xunFeiRequestClass = new XunFeiRequestClass();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
xunFeiRequestClass.c = c;
|
|
|
|
|
|
|
|
xunFeiRequestClass.b = b;
|
|
|
|
|
|
|
|
xunFeiRequestClass.d = d;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return JsonConvert.SerializeObject(xunFeiRequestClass);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//进行Base64编码
|
|
|
|
|
|
|
|
public static string EncryptBase64(string text)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
byte[] bytes = Encoding.UTF8.GetBytes(text);
|
|
|
|
|
|
|
|
return Convert.ToBase64String(bytes);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//计算字符串的HMAC-SHA256签名
|
|
|
|
|
|
|
|
public static string hmacsha256(string text, string secret)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
string signRet = string.Empty;
|
|
|
|
|
|
|
|
using (HMACSHA256 mac = new HMACSHA256(Encoding.UTF8.GetBytes(secret)))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
byte[] hash = mac.ComputeHash(Encoding.UTF8.GetBytes(text));
|
|
|
|
|
|
|
|
signRet = Convert.ToBase64String(hash);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return signRet;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//验证服务器证书是否有效
|
|
|
|
|
|
|
|
public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|