RFC 1867 httpUpload method

Status
Not open for further replies.

ssadoglu

Wise, Aged Ars Veteran
119
Hi,

I am uploading two files to API of the Mobile Phone Service Company to create MMS Message such as one png and one audio. it's created MMS Message , but it's not working my audio file.Otherwise,I can upload 4 pieces image same those codes.Where I wrong?

Here is my code.

Code:
public byte[] UploadFiles(string address, IEnumerable<UploadFile> 
 files,NameValueCollection values)
     {
        var request = HttpWebRequest.Create(address);
        request.Method = "POST";
        var boundary = "---------------------------5646198165461";
        sendInfoLog(request.Method, "", "");
        sendInfoLog(boundary,"","");
        sendInfoLog(request.ContentType, "", "");
        boundary = "--" + boundary;

        try
        {
            using (var requestStream = request.GetRequestStream())
            {
                // Write the values
                foreach (string name in values.Keys)
                {
                    var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
                    requestStream.Write(buffer, 0, buffer.Length);
                    buffer = Encoding.ASCII.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"{1}{1}", name, Environment.NewLine));
                    requestStream.Write(buffer, 0, buffer.Length);
                    buffer = Encoding.UTF8.GetBytes(values[name] + Environment.NewLine);
                    requestStream.Write(buffer, 0, buffer.Length);
                    sendInfoLog(string.Format("Content-Disposition: form-data; name=\"{0}\"{1}{1}\n{2}",name, Environment.NewLine, values[name] + Environment.NewLine),"","");
                }

                // Write the files
                int k = 0;

                foreach (var file in files)
                {
                    int c = 0;
                    foreach (var count in file.ContentType)
                    {
                        var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
                        requestStream.Write(buffer, 0, buffer.Length);
                        buffer = Encoding.UTF8.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}", file.Name[c], file.Filename[c], Environment.NewLine));
                        sendInfoLog(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}", file.Name[c], file.Filename[c], Environment.NewLine), "", "");
                        requestStream.Write(buffer, 0, buffer.Length);
                        buffer = Encoding.ASCII.GetBytes(string.Format("Content-Type: {0}{1}{1}", file.ContentType[c], Environment.NewLine));
                        sendInfoLog(string.Format("Content-Type: {0}{1}{1}", file.ContentType[c], Environment.NewLine), "", "");
                        requestStream.Write(buffer, 0, buffer.Length);

                        CopyTo(file.Stream, requestStream);

                        buffer = Encoding.ASCII.GetBytes(Environment.NewLine);
                        requestStream.Write(buffer, 0, buffer.Length);

                        ++c;
                    }
                    ++k;
                }

                var boundaryBuffer = Encoding.ASCII.GetBytes(boundary+"--\n");
                requestStream.Write(boundaryBuffer, 0, boundaryBuffer.Length);

            }

        }
        catch (Exception x)
        {
            sendErrorLog("Uploading Error","",x.Message,new Exception(""));
            Console.Write("Error >" + x.Message);
        }
        using (var response = request.GetResponse())
        using (var responseStream = response.GetResponseStream())
        using (var stream = new MemoryStream())
        {
            CopyTo(responseStream, stream);
            return stream.ToArray();
        }
    }

    public static void CopyTo(Stream input, Stream output)
    {
        byte[] buffer = new byte[16 * 1024];
        int bytesRead;

        while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            output.Write(buffer, 0, bytesRead);
        }
    }

    public class UploadFile
    {
        public UploadFile(Stream stream)
        {
            Name = new List<string>();
            ContentType = new List<string>();
            Filename = new List<string>();
            this.Stream = stream;
        }
        public readonly List<string> Name;
        public readonly List<string> Filename;
        public readonly List<string> ContentType;
        public readonly Stream Stream;
    }


      public string CreateMMS(List<List<string>> Pathes,string name, string framecount,  List<string> Frame_N_Text,List<string> Frame_N_Duration, List<List<string>> ContentType, List<List<string>> Names,string ProcessType)
     {  
      //return ">>name"+name+"count="+" "+framecount+"text"+Frame_N_Text[0]+" "+Frame_N_Duration[0];

        List<UploadFile> ufs = new List<UploadFile>();
        int N=1;


        try
        {           
            for (int i = 0; i < Names.Count; ++i)
            {

                for (int j = 0; j < ContentType[i].Count; j++)
                {
                    FileStream stream = File.Open(Pathes[i][j], FileMode.Open);
                    UploadFile uf = new UploadFile(stream);
                    uf.ContentType.Add(ContentType[i][j]);
                    uf.Name.Add(Names[i][j]);
                    uf.Filename.Add(Pathes[i][j]);
                    ufs.Add(uf);
                }

                } 

            NameValueCollection FrameValues = new NameValueCollection();
            FrameValues.Add("token", Token);
            FrameValues.Add("name", name);
            FrameValues.Add("frame_count",framecount);
            int loop = 0;

            int forloop = Frame_N_Text.Count;
            if (ProcessType == "4")
            {
               forloop=forloop/2;
            }
            for (int i = 0; i < forloop; i++)
            {
                FrameValues.Add("frame_" + N + "_text", Frame_N_Text[i]);
                N++;
            }

            N = 1;
            for (int i = 0; i < forloop; i++)
            {
                FrameValues.Add("frame_" + N + "_duration", Frame_N_Duration[i]);
                N++;
            }

       byte[] RetMMSID = UploadFiles("https://api.xxx.xxx.com/mms.xml", ufs, FrameValues);



            for (int i = 0; i < Names.Count; ++i)
            {
                ufs[i].Stream.Close();
            }


            string RetVal = System.Text.ASCIIEncoding.ASCII.GetString(RetMMSID);
            return RetVal;

        }


        catch (Exception ex)
        {

            return "Uploading Error"+ex.Message;
        }

    }
 

JTM

Ars Praetorian
401
Code:
var boundary = "---------------------------5646198165461";
.
.
.        
boundary = "--" + boundary;

One thing that might help is to use a different boundary. The boundary is supposed to be a string that you're not going to find in your POST data. Audio files are quite large and that string might be found in the data of the audio file. An MD5sum of the files you're sending might work better as a boundary.
 

ssadoglu

Wise, Aged Ars Veteran
119
First of all,thanks for your interesting.I know that's quite a situation.Also,I probably have a boundary problem.
Because, I can upload 4 pieces image same these codes to company.But , I can't upload an audio file.
As you mentioned,I'm sending sound/AMR as content type.

  • CreateMMS Method:

    CreateMMS: the properties of MMS
    Pathes: the address of each file
    name: MMS header
    frame_count: which created from how much frame of MMS Message
    frame_n_text: subtitle info of each image or sound
    frame_n_duration: display time of each frame
    Content_type: Data type (image/gif)
    Names: data type ve text that's hold order name (frame_N_visual,frame_N_audio)
    ProcessType: created type of MMS (Sound&Image&Text or Image&Text)
 

JTM

Ars Praetorian
401
ssadoglu":e66na59k said:
As you mentioned,I'm sending sound/AMR as content type.

Is the content-type being set to "sound/AMR" exactly? I think it may need to be "audio/AMR". Also, do you know that

1) the MMS protocol accepts this content-type?
2) the server recognizes this content-type and/or would complain if it didn't?
3) the phone you're sending the MMS to can play this type of file?
 

ssadoglu

Wise, Aged Ars Veteran
119
my post process ;

Code:
Content-Disposition: form-data; name="frame_1_audio"; filename="C:\Inetpub\vhost\xx.xx.com\MMSuploads\file_35000_ID=692\audio.amr"


Content-Type: sound/amr


#!AMR

<....fx.........................<.........

......

......

long data...

... 

...

....

...


------5646198165461--

HTTP/1.1 200 OKDate: Fri, 03 Jun 2011 07:31:13 

GMTServer: Apache/2.2.14 (Ubuntu) PHP/5.3.2-1ubuntu4.9 with Suhosin-Patch mod_wsgi/2.8 Python/2.6.5X-Powered-By: PHP/5.3.2-1ubuntu4.9Vary: Accept-Encoding

Content-Length: 0

Content-Type: text/htmlX-Pad: avoid browser bug
 

JTM

Ars Praetorian
401
JTM":233sgk1p said:
1) the MMS protocol accepts this content-type?
2) the server recognizes this content-type and/or would complain if it didn't?
3) the phone you're sending the MMS to can play this type of file?


This page seems to imply AMR is the audio file type required by MMS and most phones have an AMR codec built into them. I think that answers all three of these questions. So, I think the problem lies in the content-type declaration and/or boundary.
 

ssadoglu

Wise, Aged Ars Veteran
119
1) the MMS protocol accepts this content-type?

Answer:yes, MMS protocol accepts sound/AMR as content-type.

2) the server recognizes this content-type and/or would complain if it didn't?

Answer: Mobile Phone Service Company recognizes this content-type.But, it is acting like sound never doesn't posted.

Where is wrong in my code? Is it possible the boundary problem?

Thanks,
 

JTM

Ars Praetorian
401
ssadoglu":32usjiji said:
Where is wrong in my code? Is it possible the boundary problem?

Thanks,

You can try changing your boundary, that's not too difficult. However, try changing "Content-Type: sound/amr" to "Content-Type: audio/amr" for this type of file. Unless MMS has different MIME types, RFC 2046 doesn't mention sound as a MIME type.
 

ssadoglu

Wise, Aged Ars Veteran
119
You can try changing your boundary, that's not too difficult. However, try changing "Content-Type: sound/amr" to "Content-Type: audio/amr" for this type of file. Unless MMS has different MIME types, RFC 2046 doesn't mention sound as a MIME type.

As you said,I changed as "content-type:audio/amr" in my boundary.But,it still doesn't working.Also ,I got a random value as boundary.

something like this; the start -> d29a0c638b540b23e9a29a3a9aebc900aeeb6a82
and the end -> d29a0c638b540b23e9a29a3a9aebc900aeeb6a82--

Thanks JTM,
 

Drizzt321

Ars Legatus Legionis
34,945
Subscriptor++
You know you can usually send an email (SMTP) directly to a phone if you know the carrier that has that phone. For example, with AT&T, it's <10 Digit Number>@mms.att.net. Other carriers generally have similar, emails for MMS and SMS messaging.

Here's a good list part-way down the page of common MIME types for MMS messages, although not all of them are supported on all phones. http://www.developershome.com/wap/detection/detection.asp?page=httpHeaders

For more information on general MMS stuff, look up OMA-MMS-CONF-V1_3-20050215-D, 3GPP TS 29.199-5 v6.5.0, or 3GPP TS 23.140 v6.16. Although the last one is more for the various MM1 through MM11 protocols. I know, I've implemented MM7 before, and it can be interesting.

I'm very curious as to know what the specs for that API are, if it's not MM7. Seems pretty weird if it's not MM7 (SOAP w/Attachements based) or SMTP, since those are generally the standards for sending/receiving MMS messages from 3rd parties outside of the carrier networks.
 

JTM

Ars Praetorian
401
ssadoglu":3r7e2w9s said:
As you said,I changed as "content-type:audio/amr" in my boundary.But,it still doesn't working.Also ,I got a random value as boundary.

something like this; the start -> d29a0c638b540b23e9a29a3a9aebc900aeeb6a82
and the end -> d29a0c638b540b23e9a29a3a9aebc900aeeb6a82--

Thanks JTM,

Hi ssadoglu. Sorry I couldn't be more help. Although it looks like Drizzt321 has some useful info.
 

Drizzt321

Ars Legatus Legionis
34,945
Subscriptor++
JTM":k1u0l0df said:
ssadoglu":k1u0l0df said:
As you said,I changed as "content-type:audio/amr" in my boundary.But,it still doesn't working.Also ,I got a random value as boundary.

something like this; the start -> d29a0c638b540b23e9a29a3a9aebc900aeeb6a82
and the end -> d29a0c638b540b23e9a29a3a9aebc900aeeb6a82--

Thanks JTM,

Hi ssadoglu. Sorry I couldn't be more help. Although it looks like Drizzt321 has some useful info.

I haven't really looked at his code or output all that closely, but it might be a MIME Multi-Part issue on his part, or something similar.

Yea, it was quite interesting when I was implementing the MM7 connection. The carriers platform only took phone numbers in +1XXXXXXXXXX format, 001XXXXXXXXXX or 1XXXXXXXXXX wouldn't work, it'd reject the MMS with an error. I work for a company that does a lot in the mobile industry, SMS & some MMS, and lots of toll-free interactivity.
 

ssadoglu

Wise, Aged Ars Veteran
119
Hi ssadoglu. Sorry I couldn't be more help. Although it looks like Drizzt321 has some useful info.

First of all, thanks for your reply JTM.

I haven't really looked at his code or output all that closely, but it might be a MIME Multi-Part issue on his part, or something similar.

Yea, it was quite interesting when I was implementing the MM7 connection. The carriers platform only took phone numbers in +1XXXXXXXXXX format, 001XXXXXXXXXX or 1XXXXXXXXXX wouldn't work, it'd reject the MMS with an error. I work for a company that does a lot in the mobile industry, SMS & some MMS, and lots of toll-free interactivity.

thanks for your interesting.As I mentioned,I try everything related MIME-Type.Unfortunately,I couldn't.So,Where exactly I make mistakes?Finally,The carriers posted sample post process to me.As they said,I must send as below.

Code:
POST /mms.xml HTTP/1.1

Host: api.turkcell.hedeflimesaj.com

Content-Type: multipart/form-data; boundary=mime-seperator

Content-Length: 1129

--mime-seperator

Content-Disposition: form-data; name="token"

1F023EE35F4D2B5713618A2958558229

--mime-seperator

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

deneme mms'i

--mime-seperator

Content-Disposition: form-data; name="frame_count"

2

--mime-seperator

Content-Disposition: form-data; name="frame_1_text"

Ornek mesaj metni frame 1

--mime-seperator

Content-Disposition: form-data; name="frame_1_duration"

4

--mime-seperator

Content-Disposition: form-data; name="frame_1_visual"; filename="test1.gif"

Content-type: image/gif

(binary data)

--mime-seperator

Content-Disposition: form-data; name="frame_1_audio"; filename="test1.amr"

Content-type: sound/amr

(binary data)

--mime-seperator

Content-Disposition: form-data; name="frame_2_text"

Ornek mesaj metni frame 2

--mime-seperator

Content-Disposition: form-data; name="frame_2_duration"

4

--mime-seperator

Content-Disposition: form-data; name="frame_2_visual"; filename="test2.gif"

Content-type: image/gif

(binary data)

--mime-seperator

Content-Disposition: form-data; name="frame_2_audio"; filename="test2.amr"

Content-type: sound/amr

(binary data)

--mime-seperator

Thanks again,
 
Status
Not open for further replies.