对TestFlight Api的POST请求失败,500

时间:2013-08-08 17:02:34

标签: c# http-post testflight

我正在尝试通过POST请求将文件上传到TestFlight Api。我尽量做到最简单,但我仍然得到错误500.

HttpWebRequest request = WebRequest
    .Create('http://testflightapp.com/api/builds.json') as HttpWebRequest;
request.KeepAlive = false;
request.Headers.Add("api_token", "####");
request.Headers.Add("team_token", "####");
request.Headers.Add("notes", "notes");
request.ContentType = "multipart/form-data";
request.Method = WebRequestMethods.Http.Post;
byte[] messageArray = File.ReadAllBytes(filePath);
request.ContentLength = messageArray.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(messageArray, 0, messageArray.Length);
requestStream.Close();

WebResponse response = request.GetResponse();
StreamReader responseReader = new StreamReader(response.GetResponseStream());
string result = responseReader.ReadToEnd();
responseReader.Close();
response.Close();
return result;

我做错了什么?

2 个答案:

答案 0 :(得分:0)

1看起来您缺少名称文件的标题

https://testflightapp.com/api/doc/#

2 contenttype

webRequest.ContentType = "application/json";

3个额外设置?

也许您还应该设置request.UserAgent和/或request.AllowWriteStreamBuffering

request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";

request.AllowWriteStreamBuffering = true;

答案 1 :(得分:0)

这不是以编程方式构建多部分表单的方法。您正在混合标题和内容,而不是遵循正确的多部分分隔符,甚至没有命名部件,错误地计算内容长度等。有关csharp的帮助,请参阅c# multipart/form-data submit programmatically

您可能希望使用更高级别的csharp构造(like MultipartFormDataContent if you have .NET 4.5)。在Java中,最终得到:https://github.com/jenkinsci/testflight-plugin/blob/master/src/main/java/testflight/TestflightUploader.java

如果你想自己做,我真的建议你,例如,使用curl进行上传和wiresharp以提取TCP对话,并比较curl正在做我们做什么。