public ProtocolReq DecryptAndDeserialize(XXTea xxTea, byte[] buffer, int offset, int count) { try { byte[] decrpted = xxTea.Decrypt(buffer, offset, count); using (MemoryStream ms = new MemoryStream(decrpted, 0, decrpted.Length, true, true)) { return(Serializer.Deserialize <ProtocolReq>(ms)); } } catch (Exception ex) { return(null); } }
//private static readonly NLog.Logger log = NLog.LogManager.GetCurrentClassLogger(); public static async Task <Result> RequestAsync( string method, string url, NameValueCollection queryParameters = null, WebHeaderCollection headers = null, byte[] body = null, string contentType = "application/x-www-form-urlencoded", XXTea xxtea = null ) { string parameterString = GetParameterString(queryParameters, true); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + parameterString); request.Method = method; if (headers != null && headers.Count > 0) { foreach (var key in headers.AllKeys) { request.Headers[key] = headers[key]; } } if (body != null && body.Length > 0) { request.ContentType = contentType; using (Stream reqStream = await request.GetRequestStreamAsync()) { byte[] reqBytes; if (xxtea != null) { reqBytes = xxtea.Encrypt(body); } else { reqBytes = body; } await reqStream.WriteAsync(reqBytes, 0, reqBytes.Length); } } using (HttpWebResponse response = (HttpWebResponse)await GetResponseWithoutExceptionAsync(request)) using (Stream resStream = response.GetResponseStream()) using (MemoryStream stream = new MemoryStream()) { await resStream.CopyToAsync(stream); byte[] data = stream.ToArray(); byte[] result; if (xxtea != null) { result = xxtea.Decrypt(data, 0, data.Length); } else { result = data; } return(new Result() { StatusCode = response.StatusCode, Response = result }); } }