WebApi-响应状态代码未指示成功:404(未找到)

时间:2018-10-05 10:01:00

标签: c# json rest asp.net-web-api2 bson

我正在尝试通过Newtonsoft.Json.Bson将图像及其元数据(例如名称)从C#控制台客户端发送到WebApi Restful Server。

客户代码:

    public async Task SendRequestAsync(byte[] imageBytes, string fileName)
    {
        using (var stream = new MemoryStream())
        using (var bson = new Newtonsoft.Json.Bson.BsonWriter(stream))
        {
            var jsonSerializer = new JsonSerializer();

            var json = JObject.FromObject(new
            {
                name = fileName,
                content = imageBytes
            });

            jsonSerializer.Serialize(bson, json);

            var client = new HttpClient
            {
                BaseAddress = new Uri("http://localhost:1920")
            };

            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/bson"));

            var byteArrayContent = new ByteArrayContent(stream.ToArray());
            byteArrayContent.Headers.ContentType = new MediaTypeHeaderValue("application/bson");

            var result = await client.PostAsync(
                    "api/Files", byteArrayContent);
            try
            {
                HttpResponseMessage response = result.EnsureSuccessStatusCode();

                Console.Out.WriteLine(response.IsSuccessStatusCode);
                Console.Out.WriteLine(response.Headers);
                Console.Out.WriteLine(response.Content);
                Console.Out.WriteLine(response.StatusCode);
            }
            catch (Exception e)
            {
                Console.Out.WriteLine(e);
            }
        }
    }

服务器代码

// WebApi Controller
    public JsonResult<object> Post([FromBody]FileModel fileModel)
    {
        Console.Out.WriteLine(fileModel.name);
        Console.Out.WriteLine(fileModel.length);

        return Json<object>(new
        {
            status = HttpStatusCode.OK,
            length = fileModel.length,
            name = fileModel.name
        });
    }

// Model Class
    public class FileModel
    { 
        public string name { get; set; }
        public byte[] content { get; set; }
        public int length { get; set; }
    }

如果我发送28KB的图像,则服务器成功接收到该图像。但是,如果我尝试发送20MB的图片,则会出现以下错误

System.Net.Http.HttpRequestException: Response status code does not indicate success: 404 (Not Found).
   at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
   at FileUploadConsole.Program.<SendRequestAsync>d__1.MoveNext() in c:\users\Projects\FileUploadConsole\FileUploadConsole\Program.cs:line 58

第58行是HttpResponseMessage response = result.EnsureSuccessStatusCode();

为什么对于大图无法找到服务?

1 个答案:

答案 0 :(得分:0)

将其放在您的web.config中:

<system.webServer>
  <security>
    <requestFiltering>
      <requestLimits maxAllowedContentLength="4294967295" /> 
    </requestFiltering>
  </security>

maxAllowedContentLength指定最大内容长度(以字节为单位),并由uint表示,请参见reference。 在我的示例中,该值设置为greatest possible uint value(4 gb)。