/// <summary> /// 发送json并返回json消息 /// </summary> /// <param name="json"></param> /// <returns></returns> private static async Task <string> SendAndReceive(JsonClass json) { string str = ToJson(json); try { //发送json数据 await webSocket.SendAsync(new ArraySegment <byte>(Encoding.UTF8.GetBytes(str)), WebSocketMessageType.Text, true, cancellationToken); var result = new byte[1024]; //接收数据 await webSocket.ReceiveAsync(new ArraySegment <byte>(result), new CancellationToken()); str += "\r\n" + Encoding.UTF8.GetString(result, 0, result.Length) + "\r\n"; } catch { str = "连接错误"; } return(str); }
/// <summary> /// 上传“.torrent”文件添加BitTorrent下载 /// </summary> /// <param name="file"></param> /// <returns></returns> public static async Task <string> AddTorrent(string path) { string str = ""; string fsbase64 = ""; byte[] fs = File.ReadAllBytes(path); fsbase64 = Convert.ToBase64String(fs); //转为Base64编码 var json = new JsonClass(); json.Jsonrpc = "2.0"; json.Id = "qwer"; json.Method = "aria2.addTorrent"; List <string> paramslist = new List <string>(); //添加“.torrent”文件本地地址 paramslist.Add(fsbase64); json.Params = paramslist; str = await SendAndReceive(json); return(str); }