【问题标题】:CURL form post (-F) to HttpClient postCURL 表单发布 (-F) 到 HttpClient 发布
【发布时间】:2018-11-30 18:18:32
【问题描述】:

我需要使用 c# 的 HttpClient 将 apk 上传到 hockeyApp,

上传 apk 的 cUrl 如下:

curl \
  -F "status=2" \ 
  -F "notify=1" \ 
  -F "ipa=@hockeyapp.ipa" \ 
  -H "X-HockeyAppToken: 4567abcd8901ef234567abcd8901ef23" \
   https://rink.hockeyapp.net/api/2/apps/upload

我尝试用 c# 做同样的事情:

var stream = await File.ReadAllBytesAsync(apkFilePath);
var bytes = new ByteArrayContent(stream);
bytes.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
var multipartFormDataContent = new MultipartFormDataContent
{
    //send form text values here
    {new StringContent("2"), "status"},
    {new StringContent("0"), "notify"},
    // send file Here
    {bytes, "ipa"}
};

var uri = "https://rink.hockeyapp.net/api/2/apps/upload";

multipartFormDataContent.Headers.Add("X-HockeyAppToken", "++token_here++");

var response = await _client.PostAsync(uri, multipartFormDataContent);

但我得到的响应(经过很长一段时间)是 422 无法处理的实体

【问题讨论】:

    标签: c# asp.net .net-core dotnet-httpclient hockeyapp


    【解决方案1】:

    因为 Hockey App 将被 App Center 取代,所以我也遇到了 422 响应的同样问题。所以我与支持人员交谈并获得了这个非常好的代码示例,也许它会有所帮助。

     // TODO: Update with your info
        private static string apiKey = "<Your API Token>";
        private static string uploadUrl = "https://api.appcenter.ms/v0.1/apps/<Owner Name>/<App Name>/release_uploads";
        private static string releaseUrl = "https://api.appcenter.ms/v0.1/apps/<Owner Name>/<App Name>/release_uploads/";
        private static string fileToUpload = "<Path to your IPA>";
        private static string fileName = "<Your File Name>";
    
        static async Task Main(string[] args)
        {
            // Get upload url
            var client = new HttpClient();
            var requestMessage = new HttpRequestMessage(HttpMethod.Post, uploadUrl);
            requestMessage.Content = new StringContent("", Encoding.UTF8, "application/json");
            requestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            requestMessage.Headers.Add("X-API-Token", apiKey);
            var response = await client.SendAsync(requestMessage);
            var value = await response.Content.ReadAsAsync<UploadResponse>();
            Console.WriteLine($"Upload ID: {value.UploadId}");
            Console.WriteLine($"Upload URL: {value.UploadUrl}");
    
            // Upload file
            var uploadRequestMessage = new HttpRequestMessage(HttpMethod.Post, value.UploadUrl);
            HttpContent fileStreamContent = new StreamContent(File.OpenRead(fileToUpload));
            using (var formDataContent = new MultipartFormDataContent())
            {
                formDataContent.Add(fileStreamContent, "ipa", fileName);
                uploadRequestMessage.Content = formDataContent;
                var uploadResponse = await client.SendAsync(uploadRequestMessage);
                Console.WriteLine($"Upload Response: {uploadResponse.StatusCode}");
            }
    
            // Set to committed
            var uri = $"{releaseUrl}{value.UploadId}";
            var updateStatusMessage = new HttpRequestMessage(HttpMethod.Patch, uri);
            updateStatusMessage.Content = new StringContent("{ \"status\": \"committed\" }", Encoding.UTF8, "application/json");
            updateStatusMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            updateStatusMessage.Headers.Add("X-API-Token", apiKey);
            var updateResponse = await client.SendAsync(updateStatusMessage);
            var releaseResponse = await updateResponse.Content.ReadAsAsync<ReleaseResponse>();
            Console.WriteLine($"Release Response Id: {releaseResponse.ReleaseId}");
            Console.WriteLine($"Release Response URL: {releaseResponse.ReleaseUrl}");
    

    【讨论】:

      【解决方案2】:

      解决了,

      问题出在边界上

      cUrl 命令以boundary=xxxxxxxxxxx(无引号)的形式生成边界

      但 multipartFormDataContent 在此表单中有边界 boundary="xxxxxxxxxxx"(带引号)

      当我删除引号时它工作正常:

      // Fix boundary problem (boundary is quoted)
      var boundary = multipartFormDataContent.Headers.ContentType.Parameters.First(o => o.Name == "boundary");
      boundary.Value = boundary.Value.Replace("\"", string.Empty);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多