dua55014 2019-02-18 19:58
浏览 122
已采纳

用C#解码PHP中的GZIP流

I am having some troubles decoding gzip response from an external API. This external API is returning a base64 gziped string back, which I am having problems to decode with PHP.

This are the response headers from the API:

< Content-Length: 148
< Content-Type: application/json
< Content-Encoding: gzip
< Expires: -1
< Server: Microsoft-IIS/7.5

Now this is the body that I get from the API:

gAAAAB+LCAAAAAAABAA9jM0KQEAURs+zzFrJz8p2rC0oWwlJMVOMlby7L8nu9J177oXB4hmZRAU5CSmRuKVn5fx2Qy2aWTgI7HJB7HE02oN89N54FeFvLJW+ZcSijlJ2kN/k3dvcPFC95lSAAAAAgAAAAB+LCAAAAAAABAA9jM0KQEAURs+zzFrJz8p2rC0oWwlJMVOMlby7L8nu9J177oXB4hmZRAU5CSmRuKVn5fx2Qy2aWTgI7HJB7HE02oN89N54FeFvLJW+ZcSijlJ2kN/k3dvcPFC95lSAAAAAgAAAAB+LCAAAAAAABAA9jM0KQEAURs+zzFrJz8p2rC0oWwlJMVOMlby7L8nu9J177oXB4hmZRAU5CSmRuKVn5fx2Qy2aWTgI7HJB7HE02oN89N54FeFvLJW+ZcSijlJ2kN/k3dvcPFC95lSAAAAA

I suspect that this is the base64 string, so when I decode it, I get this:

8000 0000 1f8b 0800 0000 0000 0400 3d8c
cd0a 4040 1446 cfb3 cc5a c9cf ca76 ac2d
285b 0949 3153 8c95 bcbb 2fc9 eef4 9d7b
ee85 c1e2 1999 4405 3909 2991 b8a5 67e5
fc76 432d 9a59 3808 ec72 41ec 7134 da83
7cf4 de78 15e1 6f2c 95be 65c4 a28e 5276
90df e4dd dbdc 3c50 bde6 5480 0000 00

The problem is, that I do not know how to decode this further. I have tried gzdecode, zlib_decode and numerous other functions to decode, with no success.

I am stuck here as I do not know what is the next step.

This is decompression method written in C# that the API developers have provided:

private string Decompress(string aValue)
{
    byte[] gzBuffer = Convert.FromBase64String(aValue);
    using (MemoryStream ms = new MemoryStream())
    {
        int msgLength = BitConverter.ToInt32(gzBuffer, 0);
        ms.Write(gzBuffer, 4, gzBuffer.Length - 4);

        byte[] buffer = new byte[msgLength];

        ms.Position = 0;
        using (System.IO.Compression.GZipStream zip = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress))
        {
            zip.Read(buffer, 0, buffer.Length);
        }

        return System.Text.Encoding.Unicode.GetString(buffer, 0, buffer.Length);
    }
}

I would appreciate any guidance or tips to solve this problem.

  • 写回答

1条回答 默认 最新

  • doubo1883 2019-02-18 21:12
    关注

    Curious one, I usually look up WordPress core for handling these things, since they got most of edge cases caught over years. Not this one though.

    Rather than trying to rebuild that C# code I have simply brute forced through multiple combinations of related functions and offsets.

    This got it done:

    var_dump( gzdecode( substr( base64_decode( $data ), 4 ) ) );
    
    // string(128) "{"Code":412,"Value":"RegistrationSet","Route":"CN23/C_Document"}"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码