From 44887c1dd4763842e914be19b9259181e3d4898f Mon Sep 17 00:00:00 2001 From: lilium-saber Date: Sat, 7 Oct 2023 17:33:28 +0800 Subject: [PATCH] network to get json Signed-off-by: lilium-saber --- src/Network/jsonNet.cs | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/Network/jsonNet.cs b/src/Network/jsonNet.cs index 8b814f9..035642b 100644 --- a/src/Network/jsonNet.cs +++ b/src/Network/jsonNet.cs @@ -20,14 +20,10 @@ namespace WpfApp1.translationKey { static string https = "http://api.fanyi.baidu.com/api/trans/vip/translate?"; - static string trans = "tradition"; static string appid = "20230925001829663";//20230925001829663,20230923001827495 - static string fl = "en"; - static string dl = "zh"; static Random rd = new Random(); static string salt = rd.Next(100000).ToString(); static string key = "f7ZHpw28izQ1VU_ixoY5";//f7ZHpw28izQ1VU_ixoY5,yjvMNY33Qc1TYSBjTQYQ - static string sign = EncryptString(appid + trans + salt + key); //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"); @@ -68,13 +64,36 @@ namespace WpfApp1.translationKey return sb.ToString(); } - - public void requestJson() + public string RequestJsonLanguageChange(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(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(trans) + "&from=" + fl + "&to=" + dl + "&appid=" + appid + "&salt=" + salt + "&sign=" + sign; + 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); @@ -89,9 +108,10 @@ namespace WpfApp1.translationKey var myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); tempC2 temp1 = JsonConvert.DeserializeObject(retString); + Trans_result result = temp1.trans_result[0]; string s = result.dst; - Console.WriteLine(s); + return s; }