From 0ccf2337a155fbc6a285852d794747a7d4bebe5f Mon Sep 17 00:00:00 2001 From: lilium-saber Date: Tue, 26 Sep 2023 19:12:26 +0800 Subject: [PATCH] Network request Json Signed-off-by: lilium-saber --- src/Network/jsonNet.cs | 86 +++++++++++++++++++++ src/translationKey/translationApi.cs | 6 +- src/translationKey/translationPrivateIn.cs | 10 +-- src/translationKey/translationPrivateOut.cs | 16 ++-- src/translationKey/translationPublic.cs | 4 +- 5 files changed, 104 insertions(+), 18 deletions(-) create mode 100644 src/Network/jsonNet.cs diff --git a/src/Network/jsonNet.cs b/src/Network/jsonNet.cs new file mode 100644 index 0000000..5fe7772 --- /dev/null +++ b/src/Network/jsonNet.cs @@ -0,0 +1,86 @@ +using Stripe.Terminal; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Reflection.Metadata; +using System.Security.Cryptography; +using System.Security.Policy; +using System.Text; +using System.Threading.Tasks; +using System.Web; + +namespace WpfApp1.translationKey +{ + internal class jsonNet + { + static string https = "http://api.fanyi.baidu.com/api/trans/vip/translate?"; + + static string trans = "apple"; + static string appid = "20230925001829663"; + 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"; + 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"); + /* + 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"); + } + */ + + public 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(); + } + + public void requestJson() + { + string temp = "q=" + HttpUtility.UrlEncode(trans) + "&from" + fl + "&to" + dl + "&appid" + appid + "&salt" + salt + "&sign" + sign; + //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(); + Console.WriteLine(retString); + } + + } +} diff --git a/src/translationKey/translationApi.cs b/src/translationKey/translationApi.cs index d3f2764..4caa6cf 100644 --- a/src/translationKey/translationApi.cs +++ b/src/translationKey/translationApi.cs @@ -10,13 +10,13 @@ namespace WpfApp1.translationKey interface TranslationApiStr { - public abstract List OutputTranslationS(String id); - public abstract List OutputTranslationNameList(); + public abstract List OutputTranslationS(string id); + public abstract List OutputTranslationNameList(); } interface TranslationApiInt { - public abstract List OutputTranslationI(int id); + public abstract List OutputTranslationI(int id); } } diff --git a/src/translationKey/translationPrivateIn.cs b/src/translationKey/translationPrivateIn.cs index e85b5d5..dea2343 100644 --- a/src/translationKey/translationPrivateIn.cs +++ b/src/translationKey/translationPrivateIn.cs @@ -9,10 +9,10 @@ namespace WpfApp1.translationKey { class TranslationPrivateIn { - private String pathTemp = "E:\\programingg\\vsprogam\\testRjgc\\translationResource"; - private String txtPath = "E:\\programingg\\vsprogam\\testRjgc\\translationResource\\ListPrivate.txt"; + private string pathTemp = "E:\\programingg\\vsprogam\\testRjgc\\translationResource"; + private string txtPath = "E:\\programingg\\vsprogam\\testRjgc\\translationResource\\ListPrivate.txt"; - public void InputTranslationPrivate(String name, String id, String key) + public void InputTranslationPrivate(string name, string id, string key) { pathTemp = pathTemp + "\\" + name + ".txt"; StreamWriter sw = new StreamWriter(pathTemp); @@ -25,13 +25,13 @@ namespace WpfApp1.translationKey sw2.Close(); } - public void InputTranslationId(String id, StreamWriter sw) + public void InputTranslationId(string id, StreamWriter sw) { sw.WriteLine(id); } - public void InputTranslationKey(String key, StreamWriter sw) + public void InputTranslationKey(string key, StreamWriter sw) { sw.WriteLine(key); } diff --git a/src/translationKey/translationPrivateOut.cs b/src/translationKey/translationPrivateOut.cs index 56e529c..752c59e 100644 --- a/src/translationKey/translationPrivateOut.cs +++ b/src/translationKey/translationPrivateOut.cs @@ -9,9 +9,9 @@ namespace WpfApp1.translationKey { class translationPrivateOut : TranslationApiStr { - private String name; - private String path = "E:\\programingg\\vsprogam\\testRjgc\\translationResource\\"; - private String txtPath = "E:\\programingg\\vsprogam\\testRjgc\\translationResource\\ListPrivate.txt"; + private string name; + private string path = "E:\\programingg\\vsprogam\\testRjgc\\translationResource\\"; + private string txtPath = "E:\\programingg\\vsprogam\\testRjgc\\translationResource\\ListPrivate.txt"; private String getTranslationPrivateApi(StreamReader sr) { @@ -43,14 +43,14 @@ namespace WpfApp1.translationKey } - public List OutputTranslationS(String id) + public List OutputTranslationS(string id) { this.name = id; this.path = this.path + this.name + ".txt"; StreamReader sr = new StreamReader(this.path); - var temp = new List { }; + var temp = new List { }; temp.Add(this.getTranslationPrivateApi(sr)); temp.Add(this.getTranslationPrivateKey(sr)); @@ -58,12 +58,12 @@ namespace WpfApp1.translationKey return temp; } - public List OutputTranslationNameList() + public List OutputTranslationNameList() { - List temp = new List { }; + List temp = new List { }; StreamReader sr = new StreamReader(this.txtPath); - String temp2 = sr.ReadLine(); + string temp2 = sr.ReadLine(); if (temp2 == null) { temp.Add("ERROR"); diff --git a/src/translationKey/translationPublic.cs b/src/translationKey/translationPublic.cs index 86d1dc3..a965aea 100644 --- a/src/translationKey/translationPublic.cs +++ b/src/translationKey/translationPublic.cs @@ -9,9 +9,9 @@ namespace WpfApp1.translationKey class translationPublic : TranslationApiInt { - public List OutputTranslationI(int id) + public List OutputTranslationI(int id) { - var temp = new List { }; + var temp = new List { }; switch (id) { case 0://讯飞