Network request Json

Signed-off-by: lilium-saber <WR364999428@hotmail.com>
GDN
lilium-saber 1 year ago
parent fe5dbe1666
commit 0ccf2337a1

@ -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);
}
}
}

@ -10,13 +10,13 @@ namespace WpfApp1.translationKey
interface TranslationApiStr
{
public abstract List<String> OutputTranslationS(String id);
public abstract List<String> OutputTranslationNameList();
public abstract List<string> OutputTranslationS(string id);
public abstract List<string> OutputTranslationNameList();
}
interface TranslationApiInt
{
public abstract List<String> OutputTranslationI(int id);
public abstract List<string> OutputTranslationI(int id);
}
}

@ -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);
}

@ -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<String> OutputTranslationS(String id)
public List<string> OutputTranslationS(string id)
{
this.name = id;
this.path = this.path + this.name + ".txt";
StreamReader sr = new StreamReader(this.path);
var temp = new List<String> { };
var temp = new List<string> { };
temp.Add(this.getTranslationPrivateApi(sr));
temp.Add(this.getTranslationPrivateKey(sr));
@ -58,12 +58,12 @@ namespace WpfApp1.translationKey
return temp;
}
public List<String> OutputTranslationNameList()
public List<string> OutputTranslationNameList()
{
List<String> temp = new List<String> { };
List<string> temp = new List<string> { };
StreamReader sr = new StreamReader(this.txtPath);
String temp2 = sr.ReadLine();
string temp2 = sr.ReadLine();
if (temp2 == null)
{
temp.Add("ERROR");

@ -9,9 +9,9 @@ namespace WpfApp1.translationKey
class translationPublic : TranslationApiInt
{
public List<String> OutputTranslationI(int id)
public List<string> OutputTranslationI(int id)
{
var temp = new List<String> { };
var temp = new List<string> { };
switch (id)
{
case 0://讯飞

Loading…
Cancel
Save