多部分内容类型响应解析

时间:2017-01-12 10:05:15

标签: c# multipart dotnet-httpclient

从服务器到客户端的响应以多部分格式发送。服务器响应格式化程序类:

class MyMultipartFromatter : BufferedMediaTypeFormatter
{
    ...
    public override void WriteToStream(Type type, object value, Stream writeStream, HttpContent content)
    {
       ...
       var multipartFormData = new MultipartFormDataContent("----abc123");
       content.Headers.TryAddWithoutValidation("Content-Type", "boundary=----abc123");
       ... // Adding data
       multipartFormData.ReadAsStreamAsync().Result.CopyTo(writeStream);
    }
}

在客户端解析响应:

var response = httpClient.PostAsync(requestUri, content).Result;
var multipart = response.Content.ReadAsMultipartAsync().Result;

ReadAsMultipartAsync抛出此异常:

{"Invalid 'HttpContent' instance provided. It does not have a content-type header value. 'HttpContent' instances must have a content-type header starting with 'multipart/'.\r\nParameter name: content"}

我猜问题是response.Content没有响应头,其中是内容类型和边界设置。如何正确格式化/解析多部分响应?

0 个答案:

没有答案