1

client side is using MultipartFormDataContent with two part of data, one is a file, the other one is some metadata. In each 3 request, there will be 2 request failed due to FileData empty.

Client:

var client = new HttpClient(new WebRequestHandler());
using (var content = new MultipartFormDataContent())
{
    var guid = Guid.NewGuid().ToString();
    var tmpFileName = string.Format("{0}{1}", guid, Path.GetExtension(fileName));
    var dataContent = new ByteArrayContent(data);
    content.Add(dataContent, guid, tmpFileName);
    var optionContent = new ByteArrayContent(optionData);
    optionContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("parameter") { Name = "optionsStr" };
    content.Add(optionContent);
    var response = client.PostAsync("http://test.com", content).Result;
}

Server:

[HttpPost]
public async Task<HttpResponseMessage> UploadDocument(string dataStr)
{
    string rootPath = System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Documents");
    MultipartFormDataStreamProvider provider = new MultipartFormDataStreamProvider(rootPath);
    await Request.Content.ReadAsMultipartAsync(provider);
    if (provider.FileData == null || provider.FileData.Count == 0)
        throw new Exception("There is no file in the current request of httpcontext.");
}

After sniffing using Wiresharks, found out the actual file data was misplaced in the preamble section of the multi-part structure:

error request:

wireshark snapshot1

correct request:

wireshark snapshot2

any clue what may cause such behavior?

Eason Yang
  • 23
  • 4

0 Answers0