0

i have a problem during recive of multipart/form-data POST request, but i can't fuond a problem inside my Body. After send request, server response me With this error

Unexpected end of MIME multipart stream. MIME multipart message is not complete.

Follow the Body of the request.

-------BoundaryNg6f698dj968io34abg32rfa3

Content-Disposition: form-data; name="json"; filename="json"; Content-Type:application/json

{"key":"value","key":"value","key":"value","key":"value","key":"value"}

-------BoundaryNg6f698dj968io34abg32rfa3

Content-Disposition: form-data; name="style"; filename="style.css"; Content-Type:text/css

body {
    width: 100%;
    margin: 0;
    padding: 0;
    [etc etc]}


-------BoundaryNg6f698dj968io34abg32rfa3

Content-Disposition: form-data; name="play_circle"; filename="play_circle.png"; Content-Type:image/png


[Image]

-------BoundaryNg6f698dj968io34abg32rfa3

in my opinion all the body seems to be correct but i'm tired, maybe some one can help me with my problem?

@edit

Follow you can see how i build the multypart Body:

this part build the Css and Json Part.

public byte[] bodyBuilder()
        {
            byte[] bodyAtt = new byte[1];
            StringBuilder sb = new StringBuilder();
            foreach (PostData p in postData)
            {
                sb.Append(string.Format("\r\n{0}", boundary));
                sb.AppendLine("\r\n");
                sb.Append(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"; Content-Type:{2}", p.Name,p.FileName, p.Type));
                sb.AppendLine("\r\n");
                sb.Append(string.Format("{0}\r\n", p.Value));
            }

            bodyAtt = encoding.GetBytes(sb.ToString());
            if (content_Type.Contains("multipart/form-data"))
            {
                bodyAtt = Combine(bodyAtt, setAttachPostData());
            }

            var str = Encoding.Default.GetString(bodyAtt);

            string pathFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Log.txt");
            File.WriteAllText(pathFile, str);

            return bodyAtt;
        }

This is for the image.

public byte[] setAttachPostData()
        {

            byte[] bodyAtt = new byte[1];

            foreach (PostDataParam p in postDataParams)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("\r\n");
                sb.Append(string.Format("{0}", boundary));
                sb.AppendLine("\r\n");
                sb.AppendLine(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"; Content-Type:{2}", p.Name, p.FileName, p.Type));
                sb.AppendLine("\r\n");
                bodyAtt = Combine(bodyAtt, encoding.GetBytes(sb.ToString()));                     bodyAtt = Combine(bodyAtt, p.data);
            }
            bodyAtt = Combine(bodyAtt, encoding.GetBytes(string.Format("\r\n\r\n{0}\r\n", boundary)));

            return bodyAtt;
        }

and the combine method is simply :

 public static byte[] Combine(byte[] first, byte[] second)
        {
            byte[] ret = new byte[first.Length + second.Length];
            Buffer.BlockCopy(first, 0, ret, 0, first.Length);
            Buffer.BlockCopy(second, 0, ret, first.Length, second.Length);
            return ret;
        }
Eloreden
  • 1,745
  • 2
  • 12
  • 14

1 Answers1

0

I have solved this problem using this part of code:

public async Task<string> BuildMultipartAsync()
        {
            try
            {
                MultipartFormDataContent form = new MultipartFormDataContent();

                foreach (PostData p in postData)
                {
                    form.Add(new StringContent(p.Value), p.Name, p.FileName);
                }

                foreach (PostDataParam p in postDataParams)
                {
                    form.Add(new ByteArrayContent(p.data, 0, p.data.Length), p.Name, p.FileName);
                }

                HttpResponseMessage response = await httpClient.PostAsync(URLCall, form);

                response.EnsureSuccessStatusCode();
                httpClient.Dispose();
                string sd = response.Content.ReadAsStringAsync().Result;
                return sd;
            }catch (Exception e)
            {
                return e.Message;
            }
        }

referenced from :

How to upload file to server with HTTP POST multipart/form-data

Hope can help.

Eloreden
  • 1,745
  • 2
  • 12
  • 14