Restsharp: Carga de varias partes: longitud de contenido incorrecta

Creado en 28 mar. 2017  ·  3Comentarios  ·  Fuente: restsharp/RestSharp

Hola,

Estoy tratando de que la carga multiparte funcione con v105.2.3, pero parece que hay algún problema con Content-Length.

using (FileStream source = File.Open(path, FileMode.Open))
{
    RestSharp.RestRequest request2 = new RestSharp.RestRequest();
    request.Resource = $"/files/uploads/{UploadId}";
    request.Method = Method.POST;
    request.AddFile(filename, source.CopyTo, filename);
    request.AlwaysMultipartFormData = true;

    IRestResponse<UploadResponse> response2 = this.Client.Execute<UploadResponse>(request);
    if ((int)response2.StatusCode != 201)
    {
        throw new Exception(response2.StatusDescription);
    }
}

La respuesta2 tiene este mensaje de error (está en alemán y significa que la solicitud fue cancelada):
_Die Anfrage wurde abgebrochen: Die Anfrage wurde abgebrochen .._

Excepción interna (no se puede cerrar la secuencia hasta que se escriban todos los bytes):
_Stream kann nicht geschlossen werden, bevor alle Bytes geschrieben wurden._

En Fiddler obtengo esto:

Solicitud - Entidad

Content-Length: 210
Content-Type: multipart/form-data; boundary=-----------------------------28947758029299

Respuesta

HTTP/1.1 408 Request body incomplete
The request body did not contain the specified number of bytes. Got 159, expected 210

Necesito usar el paquete Nuget con estas versiones ya que tengo otros paquetes (como RestSharp.Newtonsoft.Json) que requieren esta versión.
Parece el mismo problema aquí:
https://github.com/restsharp/RestSharp/issues/742

Atentamente,
Alex

Comentario más útil

Tuve este mismo problema y lo resolví usando el método request.Files.Add () en lugar de AddFile () así:

                request.Files.Add(new FileParameter
                {
                    Name = "fileAttach",
                    Writer = (s) =>
                    {
                        FileStream stream = File.Open(_attachment.path, FileMode.Open);
                        stream.CopyTo(s);
                        stream.Dispose();
                    },
                    FileName = _attachment.name,
                    ContentType = _attachment.contentType,
                    ContentLength = _attachment.size
                });

Todos 3 comentarios

Hola, Alex,

Tengo el mismo problema. ¿Lo ha resuelto o trabajado en torno a él?

Atentamente,
Maurice

Tuve este mismo problema y lo resolví usando el método request.Files.Add () en lugar de AddFile () así:

                request.Files.Add(new FileParameter
                {
                    Name = "fileAttach",
                    Writer = (s) =>
                    {
                        FileStream stream = File.Open(_attachment.path, FileMode.Open);
                        stream.CopyTo(s);
                        stream.Dispose();
                    },
                    FileName = _attachment.name,
                    ContentType = _attachment.contentType,
                    ContentLength = _attachment.size
                });

Puede usar AddFile pero necesita especificar la longitud del contenido, hay un parámetro para eso.

¿Fue útil esta página
0 / 5 - 0 calificaciones

Temas relacionados

vDeggial picture vDeggial  ·  6Comentarios

thomasd3 picture thomasd3  ·  5Comentarios

ChenJasonGit picture ChenJasonGit  ·  5Comentarios

wojciechrak picture wojciechrak  ·  3Comentarios

instriker picture instriker  ·  7Comentarios