Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

228
Vistas
Comillas dobles de límite HttpContent

Tengo este ejemplo de código que se publicó como respuesta a otra pregunta ( Enviar un archivo a través de HTTP POST con C# ). Funciona bien excepto por un problema. Rodea el límite en el encabezado HTTP con comillas dobles:

multiparte/datos de formulario; límite="04982073-787d-414b-a0d2-8e8a1137e145"

Esto está bloqueando el servicio web al que intento llamar. Los navegadores no tienen esas comillas dobles. Necesito alguna forma de decirle a .NET que los deje fuera.

 private System.IO.Stream Upload(string actionUrl, string paramString, Stream paramFileStream, byte [] paramFileBytes) { HttpContent stringContent = new StringContent(paramString); HttpContent fileStreamContent = new StreamContent(paramFileStream); HttpContent bytesContent = new ByteArrayContent(paramFileBytes); using (var client = new HttpClient()) using (var formData = new MultipartFormDataContent()) { formData.Add(stringContent, "param1", "param1"); formData.Add(fileStreamContent, "file1", "file1"); formData.Add(bytesContent, "file2", "file2"); var response = client.PostAsync(actionUrl, formData).Result; if (!response.IsSuccessStatusCode) { return null; } return response.Content.ReadAsStreamAsync().Result; } }
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Puede eliminar las comillas del límite utilizando el siguiente código:

 var boundary = formData.Headers.ContentType.Parameters.First(o => o.Name == "boundary"); boundary.Value = boundary.Value.Replace("\"", String.Empty);
over 4 years ago · Santiago Trujillo Denunciar

0

Ampliando la discusión en un problema similar en el repositorio de tiempo de ejecución de dotnet . Elegir no tener comillas implica dos pasos

  1. elige tu propio límite no cotizado
  2. elimine el encabezado ContentType generado automáticamente con uno sin comillas
 private System.IO.Stream Upload(string actionUrl, string paramString, Stream paramFileStream, byte [] paramFileBytes) { HttpContent stringContent = new StringContent(paramString); HttpContent fileStreamContent = new StreamContent(paramFileStream); HttpContent bytesContent = new ByteArrayContent(paramFileBytes); //Set your own boundary, otherwise the internal boundary between multiple parts remains with quotes string boundaryDelimiter = String.Format("----------{0}", DateTime.Now.Ticks.ToString("x")); using (var client = new HttpClient()) using (var formData = new MultipartFormDataContent(boundaryDelimiter)) { formData.Add(stringContent, "param1", "param1"); formData.Add(fileStreamContent, "file1", "file1"); formData.Add(bytesContent, "file2", "file2"); //remove quotes from ContentType Header _multiPartContent.Headers.Remove("Content-Type"); _multiPartContent.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=" + boundaryDelimiter); var response = client.PostAsync(actionUrl, formData).Result; if (!response.IsSuccessStatusCode) { return null; } return response.Content.ReadAsStreamAsync().Result; } }
over 4 years ago · Santiago Trujillo Denunciar

0

Esta es una pregunta antigua, pero tuve el mismo problema y lo resolví de la misma manera que @Luis Perez en su respuesta, pero usar eso tiene el inconveniente de que MultipartContent deja de calcular el tamaño del contenido, así que esta fue mi solución:

 var multipartContent = new MultipartFormDataContent(); var boundaryParameter = multipartContent.Headers.ContentType .Parameters.Single(p => p.Name == "boundary"); boundaryParameter.Value = boundaryParameter.Value.Replace("\"", ""); // --- Put the rest of content here var size = (await multipartContent.ReadAsStreamAsync()).Length; multipartContent.Headers.ContentLength = size;

Espero que esto ayude a alguien por ahí.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda