private DataInfo HandleRequest(HttpListenerRequest request, HttpListenerResponse response) { string data = null; try { var byteList = new List <byte>(); var byteArr = new byte[2048]; int readLen = 0; int len = 0; //接收客户端传过来的数据并转成字符串类型 do { readLen = request.InputStream.Read(byteArr, 0, byteArr.Length); len += readLen; byteList.AddRange(byteArr); } while (readLen != 0); data = Encoding.UTF8.GetString(byteList.ToArray(), 0, len); //获取得到数据data可以进行其他操作 } catch (Exception ex) { response.StatusDescription = "404"; response.StatusCode = 404; Console.ForegroundColor = ConsoleColor.Red; //Console.WriteLine($"在接收数据时发生错误:{ex.ToString()}"); return(null); //把服务端错误信息直接返回可能会导致信息不安全,此处仅供参考 } response.StatusDescription = "200"; //获取或设置返回给客户端的 HTTP 状态代码的文本说明。 response.StatusCode = 200; // 获取或设置返回给客户端的 HTTP 状态代码。 Console.ForegroundColor = ConsoleColor.Green; //Console.WriteLine($"接收数据完成:{data.Trim()},时间:{DateTime.Now.ToString()}"); if (request.RawUrl == "/getDogInfo") { return(getDogInfo()); } else if (request.RawUrl == "/encryp") { ParamsList param = JsonConvert.DeserializeObject <ParamsList>(data); return(encryp(param)); } else if (request.RawUrl == "/decrypt") { DecryptParamsList param = JsonConvert.DeserializeObject <DecryptParamsList>(data); return(decrypt(param)); } else { DataInfo datainfo = new DataInfo(); datainfo.Code = 0; datainfo.Data = ""; return(datainfo); } }
//加密 private DataInfo encryp(ParamsList param) { EncrypList encrypList = new EncrypList(); encrypList.Encryp = new List <string>(); foreach (string str in param.paramsString) { if (str != "" && str != null) { string s = RUtils.Encrypt(bKey, str); encrypList.Encryp.Add(s); } else { encrypList.Encryp.Add(""); } } DataInfo datainfo = new DataInfo(); datainfo.Code = 0; datainfo.Data = encrypList; return(datainfo); }