Jakub Szułakiewicz
Jakub Szułakiewicz

Reputation: 840

"Unexpected end of request content" when trying to read request body

We have Hikvision camera, with automatic number plate recognition (model DS-2CD4A26FWD-IZS/P)

This is what currently camera sends to our listening server (note the 0-bytes image) :

https://pastebin.com/raw/z5Bzjthh

I don't know, why it's trying to send empty image - but i don't care, because only important data for me is actual licence plate number.

This is test endpoint, we are trying to use to receive this data:

    [HttpPost]
    [Route("/camera/event")]
    [SwaggerOperation("EventReceived")]
    [SwaggerResponse(statusCode: 200, type: typeof(Response<string>), description: "OK")]
    public async Task<string> EventReceived()
    {
        try
        {
            var path = @$"c:\Temp\camera-{DateTime.UtcNow:yyyy-MM-dd-HH-mm-ss-fff}-data.log";
            using var fileStream = System.IO.File.Create(path);

            await Request.Body.CopyToAsync(fileStream);
            return $"File {path} created";
        }
        catch (Exception ex)
        {
            _log.Error($"Error parsing request: {ex.Message}");
            return $"Error parsing request: {ex.Message}";
        }
    }

However, I receive exception "Unexpected end of request content". This error is not occasional, not caused by some random network problems - it occurs always, in every single connection.

I am not able to reproduce this problem without actual camera - I even tried doing a http session manually via Putty, sending the same data - and everything works.

I also tried:

    var boundary = Request.GetMultipartBoundary();
    var reader = new MultipartReader(boundary, HttpContext.Request.Body);
    var section = await reader.ReadNextSectionAsync()
            await section.Body.CopyToAsync(fileStream);

With the same result.

As far as I've tested - every attempt to read Request.Body causes this exception. Even manually reading the first byte from the stream:

await Request.Body.ReadAsync(buffer, 0, 1)

Trying to figure out source of problem, I installed Fiddler and configure it to work as a proxy - and suddenly everything works as expected, and all content of message has been properly logged without any errors.

I cannot understand, why direct connection to webapi ends with exception, while connection through Fiddler proxy magicaly fixes it. Any help?

Upvotes: 0

Views: 63

Answers (0)

Related Questions