public void FileAsync_Compress_Decompress_Async() { var filename = FileTool.GetTempFileName(); using (var fs = FileAsync.OpenWrite(filename)) using (var gzip = new GZipStream(fs, CompressionMode.Compress)) { gzip.WriteAsync(PlainBytes, 0, PlainBytes.Length).Wait(); } var fi = new FileInfo(filename); Assert.IsTrue(fi.Exists); Assert.IsTrue(PlainBytes.Length > fi.Length); using (var fs = FileAsync.OpenRead(filename)) using (var gzip = new GZipStream(fs, CompressionMode.Decompress, true)) { var output = With.TryFunctionAsync(() => gzip.ReadAllBytesAsync().Result); Assert.AreEqual(PlainBytes.Length, output.Length); Assert.AreEqual(PlainBytes, output); } fi = new FileInfo(filename); fi.Delete(); }
public void Memory_Compress_Decompress_Async() { var input = (byte[])PlainBytes.Clone(); byte[] compressed; using (var outStream = new MemoryStream(input.Length)) { using (var gzip = new GZipStream(outStream, CompressionMode.Compress)) { gzip.WriteAsync(input, 0, input.Length).Wait(); } compressed = outStream.ToArray(); Assert.IsNotNull(compressed); Assert.IsTrue(compressed.Length > 0); } using (var gzip = new GZipStream(new MemoryStream(compressed), CompressionMode.Decompress)) { var decompressed = With.TryFunctionAsync(() => gzip.ReadAllBytesAsync().Result); Assert.IsTrue(decompressed.Length > 0); Assert.AreEqual(PlainBytes, decompressed); } }
private async Task <byte[]> getResponseData(CancellationToken?cancellationToken) { if (cancellationToken == null) { cancellationToken = new CancellationToken(); } //var client = new HttpClient(_handler, true); var client = _httpClientFactory.CreateClient("defaulthttpgetter"); client.BaseAddress = _requestMsg.RequestUri; var contentType = _contentType; if (_requestMsg.Method == HttpMethod.Post) { if (_jsonContent != null) { if (!_hasChangeContentType) { contentType = ContentTypeEnum.Json; } var content = new StringContent(_jsonContent.ToStringEx(Formatting.None), _encoding, getContentType(contentType)); _requestMsg.Content = content; } else if (_values.HasData()) { var hasFile = _values.Any(x => x.Value is FormFile); if (!_hasChangeContentType) { if (hasFile) { contentType = ContentTypeEnum.FormData; } else { contentType = ContentTypeEnum.FormUrlencoded; } } if (!hasFile && contentType != ContentTypeEnum.FormData) { var content = new FormUrlEncodedContent(_values.Select(x => new KeyValuePair <string, string>(x.Key, x.Value.ToStringEx()))); _requestMsg.Content = content; } else { string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); var content = new MultipartFormDataContent(boundary); foreach (var value in _values) { if (value.Value is FormFile) { var file = (FormFile)value.Value; if (!file.FileData.HasData()) { continue; } content.Add(new ByteArrayContent(file.FileData), value.Key, file.FilePath); } else { content.Add(new StringContent((string)value.Value), value.Key); } } } } else { var content = new StringContent(_stringContent, _encoding, getContentType(contentType)); _requestMsg.Content = content; } } if (_cookies.IsValueCreated && _cookies.Value.HasData()) { var cookie = _cookies.Value.Select(x => $"{x.Key}={x.Value};").JoinToString(" "); _requestMsg.Headers.Add("Cookie", cookie); } HttpResponseMessage resp = null; retry: try { //if (cancellationToken.HasValue) //{ resp = (await client.SendAsync(_requestMsg, cancellationToken.Value)).EnsureSuccessStatusCode(); //} var sourceStream = await resp.Content.ReadAsStreamAsync(); byte[] data = null; if (resp.Content.Headers.ContentEncoding.Any(x => x.Contains("gzip"))) { using (GZipStream stream = new GZipStream(sourceStream, CompressionMode.Decompress)) { data = await stream.ReadAllBytesAsync(); } } else if (resp.Content.Headers.ContentEncoding.Any(x => x.Contains("deflate"))) { using (DeflateStream stream = new DeflateStream(sourceStream, CompressionMode.Decompress)) { data = await stream.ReadAllBytesAsync(); } } else { data = await sourceStream.ReadAllBytesAsync(); } _retryCount--; return(data); } catch (Exception e) { var content = ""; if (resp == null) { content = await resp.Content.ReadAsStringAsync(); } LoggerManager.Default.Debug("读取错误:" + (string.IsNullOrWhiteSpace(content)?e.Message:content)); _retryCount--; if (_retryCount > 0) { goto retry; } throw new HttpWebGetterException(_requestMsg.RequestUri.ToStringEx(), e, content); } }