1
votes

I'm using HttpWebRequest to POST to an MMS API. The body of the post contains XML data about the delivery and the MMS message as a MIME-multipart attachment that needs to be Base64 encoded.

The post is successful, but I receive only the text, not the image.

When looking at my code, it seems to build the form data okay, but when I convert it back to a string, the file data is missing.

CONTENTS OF str VARIABLE:

------------f2de17263b724d5a919b14a6834c489f Content-Disposition: form-data; name="username"

trilogy ------------f2de17263b724d5a919b14a6834c489f Content-Disposition: form-data; name="password"

ZBo8KE6m ------------f2de17263b724d5a919b14a6834c489f Content-Disposition: form-data; name="number"

61402720898 ------------f2de17263b724d5a919b14a6834c489f Content-Disposition: form-data; name="subject"

Test Message Subject

------------f2de17263b724d5a919b14a6834c489f Content-Disposition: form-data; name="message"

Test message body.

------------f2de17263b724d5a919b14a6834c489f Content-Disposition: form-data; name="type0"

image/jpeg ------------f2de17263b724d5a919b14a6834c489f Content-Disposition: form-data; name="type1"

image/jpeg ------------f2de17263b724d5a919b14a6834c489f Content-Disposition: form-data; name="name0"

Voucher.png ------------f2de17263b724d5a919b14a6834c489f Content-Disposition: form-data; name="name1"

QRCode.png ------------f2de17263b724d5a919b14a6834c489f Content-Disposition: form-data; name="attachment0"; filename="Voucher.png" Content-Type: image/jpeg

�PNG

    private static readonly Encoding encoding = Encoding.UTF8;
    public static HttpWebResponse MultipartFormDataPost(string postUrl, string userAgent, Dictionary<string, object> postParameters)
    {
        string formDataBoundary = String.Format("----------{0:N}", Guid.NewGuid());
        string contentType = "multipart/form-data; boundary=" + formDataBoundary;

        byte[] formData = GetMultipartFormData(postParameters, formDataBoundary);
        var str = Encoding.UTF8.GetString(formData);

        return PostForm(postUrl, userAgent, contentType, formData);
    }

    private static byte[] GetMultipartFormData(Dictionary<string, object> postParameters, string boundary)
    {
        Stream formDataStream = new System.IO.MemoryStream();
        byte[] formData = new byte[0];
        bool needsCLRF = false;
        try
        {
            foreach (var param in postParameters)
            {
                // add a CRLF to allow multiple parameters to be added (skip it on the 1st parameter)
                if (needsCLRF)
                    formDataStream.Write(encoding.GetBytes("\r\n"), 0, encoding.GetByteCount("\r\n"));

                needsCLRF = true;

                if (param.Value is FileParameter)
                {
                    FileParameter fileToUpload = (FileParameter)param.Value;

                    // add just the first part of this param, since we will write the file data directly to the stream
                    string header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"\r\nContent-Type: {3}\r\n\r\n",
                        boundary,
                        param.Key,
                        fileToUpload.FileName ?? param.Key,
                        fileToUpload.ContentType ?? "application/octet-stream");

                    formDataStream.Write(encoding.GetBytes(header), 0, encoding.GetByteCount(header));

                    // write the file to the stream
                    string str = Convert.ToBase64String(fileToUpload.File);
                    byte[] myBytes = Convert.FromBase64String(str);
                    formDataStream.Write(myBytes, 0, myBytes.Length);
                                        }
                else
                {
                    string postData = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}",
                        boundary,
                        param.Key,
                        param.Value);
                    formDataStream.Write(encoding.GetBytes(postData), 0, encoding.GetByteCount(postData));
                }
            }

            // add the end of the request. Start with a newline
            string footer = "\r\n--" + boundary + "--\r\n";
            formDataStream.Write(encoding.GetBytes(footer), 0, encoding.GetByteCount(footer));

            // dump stream into byte array
            formDataStream.Position = 0;
            formData = new byte[formDataStream.Length];
            formDataStream.Read(formData, 0, formData.Length);
            formDataStream.Close();
        }
        catch (Exception ex)
        {
            gFunc.ProcessError(true, ex.ToString(), "Post Data");
        }
        return formData;
    }
1
I managed to fix part of the problem by changing byte[] myBytes = Convert.FromBase64String(str); to byte[] myBytes = encoding.GetBytes(str); but when I receive the MMS I still get only the text and no picture.Ross Kelly

1 Answers

0
votes

It turns out that the problem was with the post data.

After removing the additional fields in the "Content-Distribution" for the File parameter, it worked.

PREVIOUSLY: Content-Disposition: form-data; name="attachment0"; filename="Voucher.png" Content-Type: image/jpeg

NOW: Content-Disposition: form-data; name="attachment0"