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????