c# - 发布大于 127kb 的 .ZIP 文件时 PhoneGap 构建 API 出错

标签 c# phonegap-build

我编写了下面的 C# 应用程序,以通过 PhoneGap Build API 更新现有应用程序。我注意到当我的 .ZIP 文件大小为 127kb 或更少时它可以工作。一旦达到 128kb,我就会收到 500 HTTP 响应。抱歉,API 不会返回有关错误的任何详细信息,仅返回 500 响应代码。任何有关此问题的帮助将不胜感激。请注意身份验证 token 、appId 和 .zip 文件位置的占位符。谢谢。

using System;
using System.IO;
using System.Net;

namespace PhoneGapBuildQuestion
{
    class Program
    {
        static void Main(string[] args)
        {
            string token = "<add your token here>";
            string appId = "<add your appId here>";
            string zipFile = "<add full path to the application .zip file here>";
            var info = new FileInfo(zipFile);
            var request = (HttpWebRequest)WebRequest.Create(string.Format("https://build.phonegap.com/api/v1/apps/{0}?auth_token={1}", appId, token));
            request.ContentType = "application/zip";
            request.Headers["Content-disposition"] = string.Format("attachment; filename=\"{0}\"", info.Name);
            request.Method = "PUT";

            var reqStream = request.GetRequestStream();
            var file = new FileStream(zipFile, FileMode.Open);
            var bytes = new byte[32768];
            int len = 0;

            while((len = file.Read(bytes, 0, bytes.Length)) > 0)
                reqStream.Write(bytes, 0, len);

            reqStream.Close();
            var response = new StreamReader(request.GetResponse().GetResponseStream());
            string responseText = response.ReadToEnd();
            Console.WriteLine(responseText);
            Console.ReadLine();
        }
    }
}

最佳答案

我明白了。我使用 fiddler 捕获来 self 的应用程序和 cURL 的请求,比较两者并进行相应调整。这是我最终得到的代码:

using System;
using System.IO;
using System.Net;

namespace PhoneGapBuildQuestion
{
    class Program
    {
        static void Main(string[] args)
        {
            string appId = "[your appId here]";
            string fileName = "[absolute path to .zip file here]";
            string token = "[authentication token here]";

            string boundry = "----------------------------7b053ae48e94";
            var encoding = new System.Text.ASCIIEncoding();
            var fileInfo = new FileInfo(fileName);
            var ms = new MemoryStream();
            long totalBytes = 0;

            string txt = string.Format("--{0}{2}Content-Disposition: form-data; name=\"file\"; filename=\"{1}\"{2}Content-Type: application/octet-stream{2}{2}", boundry, fileInfo.Name, Environment.NewLine);
            int bytesRead = 0;
            var buffer = new byte[32768];

            bytesRead = encoding.GetBytes(txt, 0, txt.Length, buffer, 0);
            totalBytes += bytesRead;
            ms.Write(buffer, 0, bytesRead);

            // read/write file contents to the stream
            var fs = new FileStream(fileName, FileMode.Open);
            while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, bytesRead);
                totalBytes += bytesRead;
            }

            txt = Environment.NewLine + "--" + boundry + "--" + Environment.NewLine;
            bytesRead = encoding.GetBytes(txt, 0, txt.Length, buffer, 0);
            totalBytes += bytesRead;
            ms.Write(buffer, 0, bytesRead);
            ms.Position = 0;

            var request = (HttpWebRequest)WebRequest.Create(string.Format("https://build.phonegap.com/api/v1/apps/{0}?auth_token={1}", appId, token));
            request.ContentLength = totalBytes;
            request.Method = "PUT";
            request.ContentType = "multipart/form-data; boundary=" + boundry;
            var requestStream = request.GetRequestStream();

            while ((bytesRead = ms.Read(buffer, 0, buffer.Length)) > 0)
                requestStream.Write(buffer, 0, bytesRead);

            requestStream.Close();

            Console.WriteLine(new StreamReader(request.GetResponse().GetResponseStream()).ReadToEnd());
            Console.ReadLine();
        }
    }
}

关于c# - 发布大于 127kb 的 .ZIP 文件时 PhoneGap 构建 API 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16701007/

相关文章:

java - 从 native 后台服务启动屏幕

xml - Cordova 与 PhoneGap Build 中的 config.xml 位置

javascript - Google map API 不适用于 PhoneGap - 生成空白页

cordova - 如果我只有项目的 www 目录,如何使用 CLI 初始化 Cordova 项目?

c# - 组合框的 SelectedValue 不反射(reflect)传递给空项时的更改

c# - 使用 Json.NET 的自定义映射

C# 如何创建和保存 resx 文件

c# - 具有不同参数数量的方法的委托(delegate)

cordova - 从 PhoneGap Build 转换为本地 Cordova CLI

javascript - PhoneGap/Cordova 编译的 Android 应用程序不加载外部 URI