У меня есть один API, который возвращает сжатые данные. Упрощенная версия моего кода выглядит так:
Код: Выделить всё
[HttpPost("[action]", Name = "GetData")]
public IActionResult GetData(string request)
{
string data = GetStaticXMLFileData();
data = CompressString(data);
return new ContentResult
{
Content = data,
ContentType = "text/xml",
StatusCode = (int)HttpStatusCode.OK
};
}
private string CompressString(string text)
{
string compressedString;
using (var memoryStream = new MemoryStream())
{
using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
{
using (var writer = new StreamWriter(gZipStream, Encoding.UTF8))
{
writer.Write(text);
}
byte[] compressedData = memoryStream.ToArray();
compressedString = System.Text.Encoding.UTF8.GetString(compressedData);
}
}
return compressedString;
}
Теперь я пытаюсь использовать их в своем клиентском коде, как показано ниже. :
Код: Выделить всё
private string GetDataAPIResponse()
{
string httpResponse = string.Empty;
var urlValue = string.Format("https://localhost:44314/api/CompressedData/GetData?compress=true&result_format=xml");
var requestUri = new Uri(urlValue);
var buffer = System.Text.Encoding.UTF8.GetBytes("Test"); //Sample Data Request
var byteContent = new ByteArrayContent(buffer);
using (var httpClient = new HttpClient())
{
using (var response = httpClient.PostAsync(requestUri, byteContent).GetAwaiter().GetResult())
{
response.EnsureSuccessStatusCode();
if (response.IsSuccessStatusCode && response.StatusCode == HttpStatusCode.OK)
{
Task task = response.Content.ReadAsStreamAsync();
task.Wait();
Stream responseStream = Constants.IsCompressed ?
new GZipStream(task.Result, CompressionMode.Decompress) :
task.Result;
if (responseStream != null)
{
using (var readStream = new StreamReader(responseStream, Encoding.UTF8))
httpResponse = readStream.ReadToEnd(); //Here, I got the Error.
}
}
}
return httpResponse;
}
}
В этой строке я получаю сообщение об ошибке.
Код: Выделить всё
httpResponse = readStream.ReadToEnd();
Код: Выделить всё
System.IO.InvalidDataException: 'The archive entry was compressed using an unsupported compression method.'
Код: Выделить всё
at System.IO.Compression.Inflater.Inflate(FlushCode flushCode)
at System.IO.Compression.Inflater.ReadInflateOutput(Byte* bufPtr, Int32 length, FlushCode flushCode, Int32& bytesRead)
at System.IO.Compression.Inflater.ReadOutput(Byte* bufPtr, Int32 length, Int32& bytesRead)
at System.IO.Compression.Inflater.InflateVerified(Byte* bufPtr, Int32 length)
at System.IO.Compression.DeflateStream.ReadCore(Span`1 buffer)
at System.IO.Compression.DeflateStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at System.IO.Compression.GZipStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at System.IO.StreamReader.ReadBuffer()
at System.IO.StreamReader.ReadToEnd()
Подробнее здесь: https://stackoverflow.com/questions/792 ... g-an-unsup