2013-05-22 2 views
2

Я написал приложение C# ниже, чтобы обновить существующее приложение с помощью API сборки PhoneGap. Я заметил, что это работает, когда мой .ZIP-файл 127kb или меньше. Как только он достигает 128kb, я получаю ответ HTTP 500. К сожалению, API не возвращает никаких подробностей об ошибке, только код ответа 500. Любая помощь в этом вопросе будет принята с благодарностью. Обратите внимание, что заполнители для токена аутентификации, appId и .zip. Благодарю.Ошибка PhoneGap Build API при отправке .ZIP-файла Больше 127kb

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(); 
     } 
    } 
} 

ответ

2

Я понял. Я использовал скрипач, чтобы захватить запрос из моего приложения и 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(); 
     } 
    } 
}