How to resolve the error: FlurlHttpException: Call failed with status code 400 (Content-Length can't exceed 10485760 bytes (10MB).):

為{幸葍}努か 提交于 2020-03-25 19:11:25

问题


When I try to upload a .wav file that is a little over two mins(20 MB) it's thrown an error such as this:

Unhandled Exception: System.AggregateException: One or more errors occurred. ---> Flurl.Http.FlurlHttpException: Call failed with status code 400 (Content-Length can't exceed 10485760 bytes (10MB).):

Here is the Code:

using System;
using System.Net;
using System.Linq;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;
using Flurl;
using Flurl.Http;
using System.Collections.Generic;

namespace SpeakerRecognition
{
    public class IdentificationHelper
    {
        public async Task<string> CreateIdentificationProfile()
        {
            var url = Config.endPoint + "/identificationProfiles";
            return await url
                .WithHeader("Accept", "application/json")
                .WithHeader("Ocp-Apim-Subscription-Key", Config.key1)
                .PostJsonAsync(new { locale = "en-us" }).ReceiveString();
        }

        public async Task<bool> DeleteIdentificationProfile(string identificationProfileId)
        {
            var url = Config.endPoint + "/identificationProfiles/" + identificationProfileId;
            var result = await url
               .WithHeader("Accept", "application/json")
               .WithHeader("Ocp-Apim-Subscription-Key", Config.key1)
                .DeleteAsync().ReceiveJson();
            return true;
        }

        public async Task<string> CreateEnrollment(string fileName, string identificationProfileId)
        {
            var enrollmentUrl = Config.endPoint + "/identificationProfiles/" + identificationProfileId + "/enroll";

            var fileBytes = readFileAsBytes(fileName);

            var result = await enrollmentUrl
                .WithHeader("Accept", "application/octet-stream")
                .WithHeader("Ocp-Apim-Subscription-Key", Config.key1)
                .PostAsync(new ByteArrayContent(fileBytes));

            string checkLocation = result.Headers.GetValues("operation-location").FirstOrDefault();
            string checkStatus = "";

            do
            {
                RequestStatus checkResult = await checkLocation
                    .WithHeader("Accept", "application/json")
                    .WithHeader("Ocp-Apim-Subscription-Key", Config.key1)
                    .GetJsonAsync<RequestStatus>();

                checkStatus = checkResult.status;

                Console.WriteLine("Status:" + checkStatus);

                if (checkStatus == "succeeded")
                {
                    if (checkResult.processingResult.enrollmentStatus == "Enrolling") 
                    {
                        Console.WriteLine(
                            "Supply a little more audio, atleast 30 seconds. Currently at: " + 
                                checkResult.processingResult.speechTime);
                    }
                    else
                    {
                        Console.WriteLine("Done");
                    }
                }
                else
                {
                    Console.WriteLine("Waiting for enrollment to finish");
                }
                System.Threading.Thread.Sleep(1000);

            } while (checkStatus != "succeeded");

            return ("done");
        }

        public async Task<string> IdentifySpeaker(string fileName, string identificationProfileIds)
        {
            var identificationUrl = Config.endPoint + "/identify?identificationProfileIds=" + identificationProfileIds;

            var fileBytes = readFileAsBytes(fileName);

            var result = await identificationUrl
                .WithHeader("Accept", "application/octet-stream")
                .WithHeader("Ocp-Apim-Subscription-Key", Config.key1)
                .PostAsync(new ByteArrayContent(fileBytes));

            string checkLocation = result.Headers.GetValues("operation-location").FirstOrDefault();
            string checkStatus = "";

            do
            {
                RequestStatus checkResult = await checkLocation
                    .WithHeader("Accept", "application/json")
                    .WithHeader("Ocp-Apim-Subscription-Key", Config.key1)
                    .GetJsonAsync<RequestStatus>();


                // Console.WriteLine("checkResult:" + checkResult);

                checkStatus = checkResult.status;
                Console.WriteLine("Status:" + checkStatus);

                if (checkStatus == "succeeded")
                {
                    return checkResult.processingResult.identifiedProfileId;
                }
                else
                {
                    Console.WriteLine("Waiting for identification to finish");
                }
                System.Threading.Thread.Sleep(1000);
            } while (checkStatus != "succeeded"); 

            return ("done");
        }

        private byte[] readFileAsBytes(string fileName)
        {
            return System.IO.File.ReadAllBytes(fileName);
        }
    }

    public class RequestStatus
    {
        public string status;
        public DateTime createdDateTime;
        public DateTime lastActionDateTime;

        public ProcessingResult processingResult;
    }

    public class ProcessingResult
    {
        public string enrollmentStatus;
        public float remainingEnrollmentSpeechTime;
        public float speechTime;
        public float enrollmentSpeechTime;

        public string identifiedProfileId;
        public string confidence;
    }
}

The error occurs when I am calling this in program.cs:

string SeanID = "1cc13054-e81a-4b7a-9498-45658e8a43ae";

    string enrollmentResult = identificationHelper.CreateEnrollment(@"C:\Users\Sean\Desktop\test.wav", SeanID ).Result; (ERROR OCCURS ON THIS LINE) 

How can the code be changed to accept files larger than 10MB as input?

来源:https://stackoverflow.com/questions/60498481/how-to-resolve-the-error-flurlhttpexception-call-failed-with-status-code-400

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!