无法使用c#从自动摄取下载iTunes Connect每周选择加入报告(GZip头中的幻数不正确错误)

无法使用c#从自动摄取下载iTunes Connect每周选择加入报告(GZip头中的幻数不正确错误),c#,app-store-connect,C#,App Store Connect,我们正在成功地从iTunes自动摄取端点获取许多报告: https://reportingitc.apple.com/autoingestion.tft 但是,有些报告不是直接在正文中返回数据,而是返回一个ZIP文件,每当我们尝试读取响应时,我们都会得到: GZip头中的幻数不正确。确保您正在通过GZip流。 响应头如下所示,表示zip文件: { 内容编码: 内容配置:附件;文件名=O_S_W_XXXXXXXX_20120805.zip 文件名:O_S_W_XXXXXXXX_20120805.z

我们正在成功地从iTunes自动摄取端点获取许多报告: https://reportingitc.apple.com/autoingestion.tft

但是,有些报告不是直接在正文中返回数据,而是返回一个ZIP文件,每当我们尝试读取响应时,我们都会得到:

GZip头中的幻数不正确。确保您正在通过GZip流。

响应头如下所示,表示zip文件:

{
内容编码:
内容配置:附件;文件名=O_S_W_XXXXXXXX_20120805.zip
文件名:O_S_W_XXXXXXXX_20120805.zip
传输编码:分块
内容类型:应用程序/a-gzip
日期:2012年8月29日星期三08:54:35 GMT
设置Cookie:JSESSIONID=xxxxxxxxxxxxxx;路径=/
服务器:ApacheCoote/1.1
}

但是我真的不知道如何访问这个附件,我认为错误可能与我试图在一个操作中准备好附件和响应的其他部分有关,而实际上只有响应的一部分被压缩

代码如下所示:

HttpWebRequest w = (HttpWebRequest)WebRequest.Create(url);
w.Method = "POST";

byte[] byteArray = Encoding.UTF8.GetBytes(postData);

w.ContentLength = byteArray.Length;

using (Stream dataStream = w.GetRequestStream())
{

dataStream.Write(byteArray, 0, byteArray.Length);

dataStream.Close();

using (WebResponse resp = w.GetResponse())
{

    using (System.IO.Compression.GZipStream s = new System.IO.Compression.GZipStream(resp.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress))
    {
    // Just trying to read one byte causes the error.
    s.ReadByte();
    }

}
非常感谢您的任何想法。

看看这个

用于截取导线上的字节(请参见Inspectors选项卡)

正如API所示:流不是gzip的,而是压缩的

在c中寻找一个解压流的库#

在应用程序开始时,添加到:

ServicePointManager.Expect100Continue=true

在获得WebResponse之后,您将按照如下方式实现解压缩:

using (ZipInputStream s = new ZipInputStream(resp.GetResponseStream()) {

    ZipEntry theEntry;
    while ((theEntry = s.GetNextEntry()) != null) {

        Console.WriteLine(theEntry.Name);

        string directoryName = Path.GetDirectoryName(theEntry.Name);
        string fileName      = Path.GetFileName(theEntry.Name);

        // create directory
        if ( directoryName.Length > 0 ) {
            Directory.CreateDirectory(directoryName);
        }

        if (fileName != String.Empty) {
            using (FileStream streamWriter = File.Create(theEntry.Name)) {

                int size = 2048;
                byte[] data = new byte[2048];
                while (true) {
                    size = s.Read(data, 0, data.Length);
                    if (size > 0) {
                        streamWriter.Write(data, 0, size);
                    } else {
                        break;
                    }
                }
            }
        }
    }
}

终于弄明白了

我需要设置:

webRequest.AutomaticDecompression = DecompressionMethods.None;
之前,我将其设置为尝试:

w.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
在我还没来得及阅读之前,.NET就试图解压响应,但失败了


我现在可以读取字节数组,并在文件保存到磁盘后对其进行解密。

在没有看到http正文的情况下,文件base64不是编码的吗?如果是,首先将base64解码为字节流,然后再进行dedcompress.Hi Rene-我甚至不知道如何获取正文-每当我尝试读取它时,我都会出错。您有任何建议我可以尝试使用resp对象的代码吗?谢谢您的建议-但是仍然没有运气-当我使用上面的代码时,我在s.GetNextEntry()调用中遇到此错误:*不支持此操作。**

,当我尝试在Fiddler中解码响应时,我收到以下错误:
**-----------会话5的utilDecodeResponse失败-------HTTP响应正文格式错误。

HTTP错误:分块内容已损坏。在预期位置找不到块长度。偏移量:0

类型:System.IO.InvalidDataException**抱歉-我看不出如何格式化上述内容,尝试5分钟后,我被锁定!这样做:启动fiddler,使用java类,如我提到的问题的答案所示,以获得有效的结果,让fiddler模拟该结果,调整.net代码直到成功。