2

First of the all: I checked internet during 2 days and check ALL available options.

But nothing not help me(((

Now code:

PHP Side:

$data = gzencode(<<HERE IS DATA>>, 9);
$post_data = array('data' => $data);
$headers = array('Content-type: multipart/form-data');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_exec($ch);

PAY ATTENTION: Its not covert to base64 !

Output looks like:

úYNk™%¨e4²~ry˜¯ß5Ö¾<ИçúV£w˜«yYXÞ   j–šµ(skZBF‚’³=-7ïòu‚Jøˆ:è47š5Üç–F<ê¤Vp>]Ïbl
ÒYS¼Â”ôž²ß“­¨œTøì<ù—›×ÁËÜB´ÑAöä°%["pþkÝW–þbÜW=g§çÌF{¹é ’טà ýò°í4Á–µxMƒ<Å6Ø1¥q¡ei)}¦GY4Ëâøh[8Ü.)ÒÆaŽ
‹ã£M ‡‘|ÂrüöÜ›ïYð°Õ¨Z‰Dvçµ½Äʘ{«%ñÑÈnŠ‘BtRoñy³l³Áƒ+¼‰M¡WAΓê˺Žá(Rá.‚²#ˆšá†‚ñN dâíŒÏÃ

Now

AspCore side

1 option:

If I save this "úYNk™%¨e..........." in file (on PHP side) by

$myfile = fopen("data.txt", "w") or die("Unable to open file!");
fwrite($myfile, $data);
fclose($myfile);

And then i read this file AS BYTE ARRAY => I can decode this by this code:

byte[] data = File.ReadAllBytes(@"C:\wamp64\www\data.txt");             
using (var msi = new MemoryStream(data))
using (var mso = new MemoryStream())
{
    using (var gs = new GZipStream(msi, CompressionMode.Decompress))
    {
        //gs.CopyTo(mso);
        gs.CopyTo(mso);
    }

    return Encoding.UTF8.GetString(mso.ToArray());
}

But if I read the same file as string - This is doesn't work!

string data_string = File.ReadAllText(@"C:\wamp64\www\newfile5.txt");
byte[] data = Encoding.UTF8.GetBytes(data_string);
using (var msi = new MemoryStream(data))
using (var mso = new MemoryStream())
{
    using (var gs = new GZipStream(msi, CompressionMode.Decompress))
    {
        //gs.CopyTo(mso);
        gs.CopyTo(mso);
    }

    return Encoding.UTF8.GetString(mso.ToArray());
}

Now HTTP side (On asp core)

I read this data as:

string temp_file_name = "report_" + DateTime.UtcNow.Ticks;

                StreamWriter sw = new StreamWriter(temp_file_name);

                string _data = "";
                if (Request.Form != null && Request.Form.Keys != null)
                {
                    _data = Request.Form["data"];
                    sw.Write(_data);
                }

                sw.Close();

                string decompressed_data = "";

I try runtime decode or read this file....same problem - i cannot decode this data........

Any Idea why it????

Thank you


Update (For @ADyson)

PHP Code:

<?php
$xml = 'Hello Stackoverflow!';
$compr_data = gzencode($xml, 9);
$myfile = fopen("data.txt", "w") or die("Unable to open file!");

fwrite($myfile, $compr_data);
fclose($myfile);

$post_data = array('data' => $compr_data);
$headers = array('Content-type: multipart/form-data');



$ch = curl_init();

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);



curl_setopt($ch, CURLOPT_URL, "MY ASP CORE WEB API ENDPOINT URL");
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);


$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

echo($httpCode);

?>

Result of file (Written by PHP)

‹     
óHÍÉÉW.ILÎÎ/K-JËÉ/W Ê×/Ü   

Web API Code:

string temp_file_name = "stackoverflow_report_" + DateTime.UtcNow.Ticks;

                StreamWriter sw = new StreamWriter(temp_file_name);

                string _data = "";
                if (Request.Form != null && Request.Form.Keys != null)
                {
                    _data = Request.Form["data"];
                    sw.Write(_data);
                }

                sw.Close();

                string decompressed_data = "";


                try
                {
                    _logger.Warn("#1");

                    byte[] te = System.IO.File.ReadAllBytes(temp_file_name);
                    using (var msi = new MemoryStream(te))
                    using (var mso = new MemoryStream())
                    {
                        using (var gs = new GZipStream(msi, CompressionMode.Decompress))
                        {
                            //gs.CopyTo(mso);
                            gs.CopyTo(mso); <<<<<----- HERE IS ERROR
                        }

                        string res = Encoding.UTF8.GetString(mso.ToArray());
                        _logger.Warn(res);
                    }


                }
                catch(Exception ex)
                {
                    _logger.Warn(ex.Message);
                }

Error message:

The archive entry was compressed using an unsupported compression method.

File written by WebAPI

�     
�H���W.IL��/K-J��/W ��/�   

The files are different((( WHY????

Alex
  • 31
  • 4
  • if you can decode it as byte array, then what's the problem? It **is** binary data, so that makes sense. It's unclear why you expect it to work using the UTF-8-encoded string method, or how that would be useful. As per the [documentation](https://learn.microsoft.com/en-us/dotnet/api/system.text.encoding.getbytes?view=netframework-4.8), Encoding.UTF8.GetBytes _encodes a set of characters into a sequence of bytes_ ...the whole point of this method is to take a regular string and **encode** it as binary data. This is **not** useful for **de**coding anything! – ADyson Feb 26 '20 at 13:44
  • @ADyson as I said - when I get this data in webcontroller - and get this data as string ---even I save this data in file and then read as byte array - I cannot decompress it.... And if I understand correct - from request i can get FORM Data only as string...so how I can decompress it?? I really not understand( – Alex Feb 26 '20 at 14:24
  • So you mean that `File.ReadAllBytes` doesn't work after you get it from the controller and save it to a file? Because in the question you said it **does** work like that. – ADyson Feb 26 '20 at 14:33
  • P.S. How/why are you receiving this data from a HTML form, if it actually comes from a PHP script? Can't you just download it from the PHP URL straight into your C# application? Or upload it as a file (rather than string) from your form? The scenario / use case is a bit confusing. – ADyson Feb 26 '20 at 14:41
  • @ADyson - yes, if I got this string from Request.Form["data"]; and save in file and then read as byte array it's not work. This is work only if I save file in pho and then read as byte array in C#. – Alex Feb 26 '20 at 14:56
  • And now I cannot read from php. Php side - it's a client who send me request each X time and I need to handle them And he cannot change nothin on his side – Alex Feb 26 '20 at 14:58
  • @ADyson In generally the file saved by php and file saved by asp core....looks different.....but it was the same content....weird – Alex Feb 26 '20 at 15:02
  • why not get the user to upload it as a file instead of a string? That might help, potentially. And what exactly does "doesn't work" mean really? You get an error? Or some unexpected result? – ADyson Feb 26 '20 at 15:16
  • @AXyson in my situation the client already send this data in this way for many channels. So here I need to write application for his data format. Doesn't work - it's failed with error like this archive was archive by not supported method, something like that – Alex Feb 26 '20 at 15:24
  • Please provide the exact error message, not your re-written version of it, and tell us which line of code causes it. – ADyson Feb 26 '20 at 15:30
  • P.S. have you tried opening the file via FileStream, like this example: https://stackoverflow.com/a/1348305/5947043 ? – ADyson Feb 26 '20 at 15:33
  • @ADyson Updated Post - added code + file's content + error message – Alex Feb 26 '20 at 16:01
  • You seem to be suggesting to the system that the data is UTF-8 encoded when it obviously is not. – Mike Robinson Feb 26 '20 at 17:00
  • @MikeRobinson So the question is....how to prevent this? And how to handle and process this requests? Guys Im 2 days spend to fix it out and Nothing! PLEASE HELP – Alex Feb 26 '20 at 17:09
  • Is this still an issue for you? I may have some time to look at it more this week if you still need help. – ADyson Feb 29 '20 at 20:24
  • Hi @ADyson. I "fix" it by write request handler on PHP, decode data and then then send to my Asp core service. This is the fastest way to make it work. – Alex Mar 04 '20 at 17:47

0 Answers0