How to download and decompress file from Ebay API - c#

I am attempting to get a file from Ebays API
https://developer.ebay.com/api-docs/sell/feed/resources/task/methods/getResultFile#h2-samples
I am getting data back but it is not decompressing properly or encoding properly. It should be an gziped xml file. The documentation is not very clear on this actually. I am using RestSharp for my http calls (106.15.0).
Exception:
Found invalid data while decoding
My Code:
const string url = "sell/feed/v1/task/task-16-SOMENUMBER/download_result_file";
var restClient = new RestClient(_restApiUrl)
{
Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(authentication.AuthorizationToken, "Bearer")
};
var httpRequest = new RestRequest(url, Method.GET);
httpRequest.AddHeader("Accept-Encoding", "application/gzip");
httpRequest.AddHeader("Accept", "*/*");
byte[] myfile = restClient.DownloadData(httpRequest);
var decodedString = Encoding.UTF8.GetString(myfile);
using (var stream = new MemoryStream(myfile))
{
string res;
using (GZipStream zipStream = new GZipStream(stream, CompressionMode.Decompress))
{
using (var sr = new StreamReader(zipStream))
{
res = sr.ReadToEnd(); //ERROR HERE: Found invalid data while decoding
}
}
var result = res;
}
First 30 of returned string (Encoding.Default.GetString(myfile))
PK\u0003\u0004\u0014\0\b\b\b\0\f£†T\0\0\0\0\0\0\0\0\0\0\0\0>\0\0\0ActiveInventoryReport
Hex
50-4B-03-04-14-00-08-08-08-00-0C-A3-86-54-00-00-00-00-00-00-00-00-00-00-00-00-3E-00-00-00-41-63-74-69-76-65-49-6E-76-65-6E-74-6F-72-79-52-65-70-6F-72-74-2D-41-70-72-2D-30-36-2D-32-30-32-32-2D-32-30-3A-32-34-3A-32-30-2D-30-37-30-30-2D-31-33-33-34-39-39-38-35-32-34-2E-78-6D-6C-BD-9D-5B-53-9B-47-B6-86-AF-77-7E-45-CA-F7-32-7D-3E-4C-79-3C-25-09-03-1E-C0-D6-48-38-60-DF-B1-8D-F6-98-0A-01-17-86-4C-3C-BF-7E-F7-E1-

Related

Accepting application/gzip file in POST body

All,
I have a instance where I need to accept a gzip file from an HTTP POST in an aspnet core webapi. I have tried getting it from HTTPContext.Form.Files which obviously didn't work since it is content-type application/gzip. I have also tried:
var stream = new MemoryStream();
await HttpContext.Request.Body.CopyToAsync(stream);
await Decompress(stream, filename);
private async Task Decompress(MemoryStream compressedFileStream, string filename)
{
var stream = new MemoryStream();
using (var decompressor = new GZipStream(compressedFileStream, CompressionMode.Decompress))
{
await decompressor.CopyToAsync(stream);
}
using (var streamReader = new StreamReader(stream))
{
var content = await streamReader.ReadToEndAsync();
var doc = new XmlDocument();
doc.LoadXml(content);
}
}
content is "" so it's not getting the content of what is in the gzip file.
All help would be greatly appreciated.
Here is my latest attempt:
var stream = new MemoryStream();
await HttpContext.Request.Body.CopyToAsync(stream);
await using (var decompressor = new GZipStream(stream, CompressionMode.Decompress))
{
var stream1 = new MemoryStream();
await decompressor.CopyToAsync(stream1);
using (var streamReader = new StreamReader(stream1))
{
var content = await streamReader.ReadToEndAsync();
var doc = new XmlDocument();
doc.LoadXml(content);
}
}
I think the issue is I am not getting the request body correctly. Any thoughts on that?
Thank you!

Aspose.zip error Unexpected end of data corrupted error

I am trying to compress several excel files, but when generate the excel it generates corrupt. I don't know what could be any idea?
var Clients = getClients();
var carpetaUrl = string.Format(#"{0}/{1}/{2}.zip", IdCuenta, IdListado, "Folder");
try
{
using (MemoryStream zipFile = new MemoryStream())
{
using (var archive = new Archive())
{
foreach (var item in Clients)
{
var (fileUrl, stream) = GenerateFileExcel(item.archivoNombre,item.IdClient);
archive.CreateEntry(fileUrl, stream);
}
archive.Save(zipFile, new ArchiveSaveOptions() { Encoding = Encoding.ASCII });
}
var uri = B.Azure.Storages.UploadFile("tttt", zipFile, carpetaUrl, false).ToString();
return uri;
}

How to convert selected image in Xamarin to byte array then to string ,while the string will be attached to an api

the current code is working but the converted base64string output is attached to the api , but am having 404 error because the request url is too long. so i just need modification , to shorten the converted base64string. or any different approach.
var file = await CrossMedia.Current.PickPhotoAsync(new PickMediaOptions { PhotoSize = PhotoSize.Small });
byte[] imageArray = null;
if (file != null)
{
using (HttpClient client = new HttpClient())
using (MemoryStream ms = new MemoryStream())
{
var stream = file.GetStream();
stream.CopyTo(ms);
imageArray = ms.ToArray();
var base64String = Convert.ToBase64String(imageArray);

Bittrex websocket encoding

i use this topic
Bittrex websockets encoding method?
bittrex websocket api return json string.
{"C":"d-88903F9C-uIVg,2|Cv,81594","G":"f8N0wWnjI7HYfemJI8e0xCbSANWfxnpNCIT4iAHhWDhZjpcgeesgN6r5uUtMrGXJhuLnOyMI52yEsxDhlxcpAJc6+oPWpOx4b7k2EWza50QZM5sNKXQHzanncDgQPzJQc7yWAg==","M":[]}
I want decode param G(OpenBuyOrders). Firstly i think its gzip string, but decoding method not works
I use python method
gzip.decompress(binascii.a2b_base64(b"f8N0wWnjI7HYfemJI8e0xCbSANWfxnpNCIT4iAHhWDhZjpcgeesgN6r5uUtMrGXJhuLnOyMI52yEsxDhlxcpAJc6+oPWpOx4b7k2EWza50QZM5sNKXQHzanncDgQPzJQc7yWAg=="))
its raise error.
I try make it with c#. Same error. In documentation i not find about this format
public static string Decode(string wireData)
{
try
{
// Step 1: Base64 decode the wire data into a gzip blob
byte[] gzipData = Convert.FromBase64String(wireData);
// Step 2: Decompress gzip blob into minified JSON
using (var decompressedStream = new MemoryStream())
using (var compressedStream = new MemoryStream(gzipData))
using (var deflateStream = new DeflateStream(compressedStream, CompressionMode.Decompress))
{
deflateStream.CopyTo(decompressedStream);
decompressedStream.Position = 0;
using (var streamReader = new StreamReader(decompressedStream))
{
return streamReader.ReadToEnd();
}
}
}
catch (Exception ex)
{
return "";
}
}

WP8 Gzip compression

WP8 does not support Gzip compression, but there is 3rd party libraries that will allow you do so, i have tried many but i am not able to make it work. this is my latest try:
var handler = new HttpClientHandler();
if (handler.SupportsAutomaticDecompression)
{
handler.AutomaticDecompression = DecompressionMethods.GZip |
DecompressionMethods.Deflate;
}
Uri myUri = new Uri("http://www.web.com");
HttpClient client = new HttpClient(handler);
client.BaseAddress = myUri;
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("ubq-compression", "gzip");
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, myUri);
req.Content = new StringContent(finalURL, Encoding.UTF8);
HttpResponseMessage rm = client.SendAsync(req).Result;
string rst = await rm.Content.ReadAsStringAsync();
the API return to me an array of bytes but the the first 300 are not Gziped but everything else is
i need to unzip everything that comes after the 300 bytes.
i am using the http://www.nuget.org/packages/Microsoft.Net.Http
// i am splitting the array
byte[] hJ = res.Take(300).ToArray();
byte[] bJ = res.Skip(300).ToArray();
bj is what need to be decompressed.
i am trying this
MemoryStream stream = new MemoryStream();
stream.Write(bj, 0, bj.Length);
using (var inStream = new MemoryStream(bj))
{
var bigStreamsss = new GZipStream(inStream, CompressionMode.Decompress, true);
using (var bigStreamOut = new MemoryStream())
{
bigStreamsss.CopyTo(bigStreamOut);
output = Encoding.UTF8.GetString(bigStreamOut.ToArray(), 0, bigStreamOut.ToArray().Length);
}
}
but it is always failing on this line "
var bigStreamsss = new GZipStream(inStream, CompressionMode.Decompress, true);
"
Any help would be much appreciated
If you are using the compression header there's nothing you need to do. The server compresses and the client decompresses automatically and you don't have to worry about anything. However it sounds like you're using some proprietary content compression standard where you only compress some of it. If that's the case don't mess with any compression settings on the http client, and instead use a 3rd party uncompress library. Just seek 300 bytes on the response stream, then pass that stream to the library. You should be able to use my inflater from my gzip library, that you can find here: https://github.com/dotMorten/SharpGIS.GZipWebClient/blob/master/src/SharpGIS.GZipWebClient/GZipDeflateStream.cs
It's extremely light-weight (it's just this one file). First call
myResultStream.Seek(300, SeekOrigin.Begin);
If the stream isn't seekable, you will need to read the first 300 bytes first though.
Then use my class to decompress the rest:
Stream gzipStream = new SharpGIS.GZipInflateStream(myResultStream);
You can now read the gzipStream as if it was an uncompressed stream.
However I really don't understand why you don't use standard http compression and let the server compress everything including the first 300 bytes. It's much easier and better for the all the kittens out there.
You can use http://www.componentace.com/zlib_.NET.htm library (available through nuget):
part of request handler:
try {
var request = (HttpWebRequest)callbackResult.AsyncState;
using (var response = request.EndGetResponse(callbackResult))
using (var stream = response.GetResponseStream()) {
bool zip = false;
if (response.Headers.AllKeys.Contains("Content-Encoding") &&
response.Headers[HttpRequestHeader.ContentEncoding].ToLower() == "gzip") zip = true;
using (var reader = zip ?
#if NETFX_CORE
new StreamReader(new GZipStream(stream, CompressionMode.Decompress))
#else
new StreamReader(new zlib.ZOutputStream(stream), false)
#endif
: new StreamReader(stream)) {
var str = reader.ReadToEnd();
result = new ObjectResult(str);
}
}
}
catch (WebException e) {...}

Categories

Resources