简体   繁体   English

解压缩从c#发送到PHP的字符串

[英]Decompression of a string sent from c# to PHP

This is the function which zip the string to be sent to php code 这是将要发送到php代码的字符串压缩的函数

public string Zip(string value)
    {
        //Transform string into byte[]  
        byte[] byteArray = Encoding.UTF8.GetBytes(value);
        int indexBA = 0;
        foreach (char item in value.ToCharArray())
        {
            byteArray[indexBA++] = (byte)item;
        }
        //Prepare for compress
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        System.IO.Compression.GZipStream sw = new System.IO.Compression.GZipStream(ms,
        System.IO.Compression.CompressionMode.Compress);
        //Compress
        sw.Write(byteArray, 0, byteArray.Length);
        //Close, DO NOT FLUSH cause bytes will go missing...
        sw.Close();
        //Transform byte[] zip data to string
        byteArray = ms.ToArray();
        System.Text.StringBuilder sB = new System.Text.StringBuilder(byteArray.Length);
        foreach (byte item in byteArray)
        {
            sB.Append((char)item);
        }
        ms.Close();
        sw.Dispose();
        ms.Dispose();
        return sB.ToString();
    }

and am sending the request using 我正在使用发送请求

 string data = req.Zip(xml);
        string resp = req.post(url,"&Data="+data);

i tried to use gzuncompress , gzdecode but all produce a data error any body knows why ? 我试图使用gzuncompress,gzdecode,但所有产生数据错误,任何人都知道为什么?

This code is bizarre to start with: 这段代码很奇怪:

byte[] byteArray = Encoding.UTF8.GetBytes(value);
int indexBA = 0;
foreach (char item in value.ToCharArray())
{
    byteArray[indexBA++] = (byte)item;
}

You're converting it to a byte array using UTF-8 encoding... and then you're overwriting the contents of that array (or at least some of the contents of that array) by casting each character to a byte - which is effectively applying ISO-Latin-1 encoding. 您正在使用UTF-8编码将其转换为字节数组... 然后您通过将每个字符转换为一个字节来覆盖该数组的内容(或该数组的至少一些内容) - 这是有效地应用ISO-Latin-1编码。

Then later on, you're converting arbitrary binary data to a string like this: 然后,您将任意二进制数据转换为如下字符串:

byteArray = ms.ToArray();
System.Text.StringBuilder sB = new System.Text.StringBuilder(byteArray.Length);
foreach (byte item in byteArray)
{
    sB.Append((char)item);
}

Don't do that. 不要那样做。 It's opaque binary data - the chances of the "string" you're creating (again, effectively created via ISO-8859-1) being properly transferable are very slim. 这是不透明的二进制数据 - 您正在创建的“字符串”(再次,通过ISO-8859-1有效创建)可以正确转移的可能性非常小。

When encoding arbitrary binary data as a string, you should almost always use Base64: 将任意二进制数据编码为字符串时,您几乎应该始终使用Base64:

string base64 = Convert.ToBase64String(byteArray);

You're then also using the data as URL-encoded form data - despite the fact that the string could quite easily include characters such as & and % which have special meaning within URL encoded text. 然后,您还将数据用作URL编码的表单数据 - 尽管字符串可以很容易地包含诸如&%字符,这些字符在URL编码文本中具有特殊含义。 Don't do that either. 不要这样做。

Basically, you should: 基本上,你应该:

  • Choose which encoding you want to use for the initial text to binary conversion. 选择要用于初始文本到二进制转换的编码。 UTF-8 is a good choice here, as it can represent all of Unicode. UTF-8在这里是一个不错的选择,因为它可以代表所有的Unicode。
  • Perform the compression (and no, flushing should not cause problems here, although you should also close anyway - ideally via a using statement) 执行压缩(不,冲洗应该在这里会出现问题,但你也应该密切反正-通过一个理想的using语句)
  • Transform the binary data back into text (assuming you really have to) using base64. 使用base64将二进制数据转换回文本(假设您确实必须)。 If you're going to use this as a URL parameter, you should use a web-safe variant of base64, as described on the Wikipedia base64 page . 如果您要将此作为URL参数使用,则应使用base64的Web安全变体,如Wikipedia base64页面所述

To decompress, you'll obviously need to perform the base64 to binary conversion before decompression. 要解压缩,您显然需要在解压缩之前执行base64到二进制转换。

If possible, it would be significantly more efficient (in terms of the transferred data) to post the compressed data as binary data rather than as a URL-encoded form parameter. 如果可能的话,将压缩数据作为二进制数据而不是URL编码的表单参数发布将更加有效(就传输的数据而言)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM