简体   繁体   English

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

[英]WebApi - Response status code does not indicate success: 404 (Not Found)

I am trying to send image and its metadata (like name) via Newtonsoft.Json.Bson to a WebApi Restful Server from C# console client. 我正在尝试通过Newtonsoft.Json.Bson从C#控制台客户端向WebApi Restful Server发送图像及其元数据(例如名称)。

Client Code: 客户代码:

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

Server Code 服务器代码

// 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; }
    }

If I send an image of 28KB then server receive the image successfully. 如果我发送28KB的图像,则服务器成功接收到该图像。 But If I try to send image of 20MB then I get following error 但是,如果我尝试发送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

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

Why for large image its not able to find the service ? 为什么要放大图像找不到服务?

Put this in your web.config: 将其放在您的web.config中:

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

The maxAllowedContentLength specifies maximums content length in bytes and is represented by a uint, see reference . maxAllowedContentLength指定最大内容长度(以字节为单位),并由uint表示,请参阅参考资料 The value in my example is set to the greatest possible uint value (4 gb). 在我的示例中,该值设置为可能最大uint值 (4 gb)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在webapi asp.net中,我遇到以下错误。,响应状态代码未指示成功404(未找到) - In webapi asp.net I am getting following error.,response status code does not indicate success 404 (not found) API URL在浏览器中工作,但在REST中不工作。 它给出“响应状态代码不表示成功:404(未找到)。” - API URL Works in browser but not in REST. It gives “Response status code does not indicate success: 404 (Not Found).” 响应状态码不表示成功:401 Tumblr - Response status code does not indicate success: 401 Tumblr 获取响应状态代码不表示成功:如果文件太大,则为500 - Getting Response status code does not indicate success: 500 if file is too large Bot Framework C#Luis Getting&gt;异常:响应状态代码未指示成功:400(错误请求) - Bot Framework C# Luis Getting > Exception: Response status code does not indicate success: 400 (Bad Request) Cosmos DB:响应状态码不表示成功:BadRequest (400); 子状态:1001; 原因: (); - Cosmos DB: Response status code does not indicate success: BadRequest (400); Substatus: 1001; Reason: (); ASP.NET OpenIdConnectAuthenticationOptions - MVC 响应状态码不表示成功:400(错误请求) - ASP.NET OpenIdConnectAuthenticationOptions - MVC Response status code does not indicate success: 400 (Bad Request) C#中的Hadoop-响应状态代码未指示成功:500(服务器错误) - Hadoop in C# - Response status code does not indicate success: 500 (Server Error) HttpClient System.Net.Http.HttpRequestException:响应状态码不表示成功:401(未授权) - HttpClient System.Net.Http.HttpRequestException: Response status code does not indicate success: 401 (Unauthorized) 提交C#MapReduce作业Windows Azure HDInsight - 响应状态代码不表示成功:500(服务器错误) - Submit C# MapReduce Job Windows Azure HDInsight - Response status code does not indicate success: 500 (Server Error)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM