支持smil文件的MMS PDU打包方式.

本文介绍了一种实现MMS消息中支持SMIL格式的方法,解决了MMS中多个附件的排版问题,并分享了一个具体的工程实现案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mms如果不支持smil那就不能叫真正的MMS,mixed的格式对于多个附件的MMS来说不仅没有"排版"功能,

而且即使用你想一个文件一个文件"拆单了看",也不得不看一次手工播放一次,就象要手动"翻页".所以不支持

smil干脆就别玩MMS了.

但基于related格式的支持smil的PDU打包方式怎么也搜索不到,很多厂商和技术人员都故作神秘,不就是一

堆规范的实现吗?只好下了几个文档来研究,经过反复调试,其中用nowSMS的mmscomp打包出来的格式竟然

是错误的,最后多次抓包比较,总算成功了.在『别人原来的mixed方式』的基础上修改成related方式的.有需要的

就自己用去吧.(注意基础类是别人原来提供的,我只是提供了related方式的打包逻辑)

工程文件在:http://dl2.youkuaiyun.com/down4/20070706/06201955542.rar

usingSystem;
usingSystem.Net;
usingSystem.IO;
usingSystem.Diagnostics;
usingSystem.Threading;
usingSystem.Collections;
usingSystem.Text;
///<summary>
///Lib的摘要说明
///</summary>


namespaceMMSLib
{

publicclassMMessage
{
stringsubject="测试";
intdeliverTime=0;//多少秒以后开始投递
ArrayListinlineFiles=newArrayList();//文件列表
ArrayListdestinations=newArrayList();//目标号码

staticlongserialNumber=19700311L;//流水号
FileInfosmilFile;



publicvoidSetSubject(stringsubject)
{
this.subject=subject;
}
publicvoidSetDeliverTime(intdeliverTime)
{
this.deliverTime=deliverTime;
}
//
publicvoidAddTo(stringdest)
{
destinations.Add(dest);
}

publicvoidAddFile(stringfile)
{

if(file.ToLower().EndsWith(".smil"))
{
if(this.smilFile!=null)
thrownewException("Thesmilfilehasexisted!");
this.smilFile=newFileInfo(file);
}
inlineFiles.Add(file);
}

publicvoidClearTo()
{
destinations.Clear();
}

//得到二进制编码字节
publicbyte[]MakeMMSContent()
{

if(this.smilFile==null)thrownewException("Thesmilfilenotfound!");

byte[]MMSContent=newbyte[0];

//X-Mms-Message-Type
MMSContent=appendContent(newbyte[]{0x8C,0x80},MMSContent);


//X-Mms-Transaction-ID
MMSContent=appendContent(newbyte[]{0x98},MMSContent);
MMSContent
=appendContent(serialNumber.ToString(),MMSContent);
serialNumber
++;

MMSContent
=appendContent(newbyte[]{0x0},MMSContent);

//X-Mms-MMS-Version
MMSContent=appendContent(newbyte[]{0x8D,0x90},MMSContent);

//Date
MMSContent=appendContent(newbyte[]{0x85},MMSContent);
TimeSpants
=DateTime.Now-newDateTime(1970,1,1,0,0,0);
intsec=(int)ts.TotalSeconds;
byte[]bySec=BitConverter.GetBytes(sec);
MMSContent
=appendContent(newbyte[]{(byte)bySec.Length},MMSContent);
Array.Reverse(bySec);
MMSContent
=appendContent(bySec,MMSContent);

if(deliverTime>0)
{
MMSContent
=appendContent(newbyte[]{0x87},MMSContent);
byte[]bfTime=BitConverter.GetBytes(deliverTime);
Array.Reverse(bfTime);
byte[]bfTimeLen=newbyte[3];
bfTimeLen[
0]=(byte)(bfTime.Length+2);
bfTimeLen[
1]=0x81;//相对时间格式
bfTimeLen[2]=(byte)bfTime.Length;
MMSContent
=appendContent(bfTimeLen,MMSContent);
MMSContent
=appendContent(bfTime,MMSContent);
}


//From,Len=0x01,一个以0x81为标记的占位符,发送时自动插入发送号码.
MMSContent=appendContent(newbyte[]{0x89,0x01,0x81},MMSContent);

//To
for(inti=0;i<destinations.Count;i++)
{
MMSContent
=appendContent(newbyte[]{0x97},MMSContent);
MMSContent
=appendContent("+86"+(string)destinations[i]+"/TYPE=PLMN",MMSContent);
//MMSContent=appendContent(newbyte[]{0x20,0x20,0x0},MMSContent);
MMSContent=appendContent(newbyte[]{0x0},MMSContent);
}

//subject
if(subject.Length>0)//使用Utf8编码
{
MMSContent
=appendContent(newbyte[]{0x96},MMSContent);
byte[]byLen=newbyte[1];
byLen[
0]=(byte)(Encoding.UTF8.GetByteCount(subject)+2);
MMSContent
=appendContent(byLen,MMSContent);
MMSContent
=appendContent(newbyte[]{0xEA},MMSContent);
MMSContent
=appendContent(Encoding.UTF8.GetBytes(subject),MMSContent);
MMSContent
=appendContent(newbyte[]{0x0},MMSContent);
}


MMSContent
=appendContent(newbyte[]{0x84},MMSContent);
intctLen=2//0xB3,0x89
+"application/smil".Length
+3//0x000x8A0x3c
+smilFile.Name.Length
+2;//0x3c,0x00

byte[]cl=uintToBytes(ctLen);

if(cl[0]>=0x1F)
MMSContent
=appendContent(newbyte[]{0x1F},MMSContent);
MMSContent
=appendContent(cl,MMSContent);
MMSContent
=appendContent(newbyte[]{0xB3},MMSContent);
//0xB3Content-Type:application/vnd.wap.multipart.related

MMSContent
=appendContent(newbyte[]{0x89},MMSContent);
MMSContent
=appendContent(Encoding.ASCII.GetBytes("application/smil"),MMSContent);
MMSContent
=appendContent(newbyte[]{0x0},MMSContent);

MMSContent
=appendContent(newbyte[]{0x8A,0x3C},MMSContent);//0x8A:Start,0x3C:<
MMSContent=appendContent(Encoding.ASCII.GetBytes(smilFile.Name),MMSContent);
MMSContent
=appendContent(newbyte[]{0x3E,0x0},MMSContent);//0x3E:>





byte[]byFileCount=newbyte[1];
byFileCount[
0]=(byte)inlineFiles.Count;
MMSContent
=appendContent(byFileCount,MMSContent);

intchLen="application/smil".Length
+4//0x000xc00x220x3c:cid,",<
+smilFile.Name.Length
+3//0x3E,0x00,0x8E
+smilFile.Name.Length
+1;//0x00

for(intj=0;j<inlineFiles.Count;j++)
{
MMSContent
=appendContent(GetFileContent(inlineFiles[j].ToString()),MMSContent);
}
returnMMSContent;
}


//打包文件
privatebyte[]GetFileContent(stringFileName)
{

byte[]byHeaders=newbyte[0];//ContentType和Headers组合
byte[]byData=readFile(FileName);

stringFileID=getContentId(FileName);
if(FileName.EndsWith(".txt"))
{
byHeaders
=newbyte[1];
byHeaders[
0]=(byte)(Encoding.ASCII.GetByteCount(FileID)+5);
byHeaders
=appendContent(newbyte[]{0x83,0x85},byHeaders);//Utf-8
byHeaders=appendContent(Encoding.ASCII.GetBytes(FileID),byHeaders);
byHeaders
=appendContent(newbyte[]{0x00},byHeaders);
byHeaders
=appendContent(newbyte[]{0x81,0xEA},byHeaders);

}
elseif(FileName.EndsWith(".gif"))
{
byHeaders
=newbyte[]{0x9D};
}
elseif(FileName.EndsWith(".mid")||FileName.EndsWith(".midi"))
{
byHeaders
=Encoding.ASCII.GetBytes("audio/midi");
byHeaders
=appendContent(newbyte[]{0x00},byHeaders);
}
elseif(FileName.EndsWith(".smil"))
{
byHeaders
=Encoding.ASCII.GetBytes("application/smil");
byHeaders
=appendContent(newbyte[]{0x00},byHeaders);
}

//加入Content-ID
byHeaders=appendContent(newbyte[]{0xC0,0x22,0x3C},byHeaders);
byHeaders
=appendContent(Encoding.ASCII.GetBytes(FileID),byHeaders);
byHeaders
=appendContent(newbyte[]{0x3E,0x00},byHeaders);

//加入Content-Location
byHeaders=appendContent(newbyte[]{0x8E},byHeaders);
byHeaders
=appendContent(Encoding.ASCII.GetBytes(FileID),byHeaders);
byHeaders
=appendContent(newbyte[]{0x00},byHeaders);

byte[]byHeaderLen=uintToBytes(byHeaders.Length);
byte[]byDataLen=uintToBytes(byData.Length);

byte[]byMmc=newbyte[byHeaderLen.Length+byDataLen.Length+byHeaders.Length+byData.Length];
Array.Copy(byHeaderLen,byMmc,byHeaderLen.Length);
Array.Copy(byDataLen,
0,byMmc,byHeaderLen.Length,byDataLen.Length);
Array.Copy(byHeaders,
0,byMmc,byHeaderLen.Length+byDataLen.Length,byHeaders.Length);
Array.Copy(byData,
0,byMmc,byHeaderLen.Length+byDataLen.Length+byHeaders.Length,byData.Length);

returnbyMmc;
}

privatebyte[]uintToBytes(intn)
{
byte[]buf=newbyte[8];
intl=0;
while(n>=128)
{
byteb=(byte)(n&0x7F);
n
=n>>7;
buf[l
++]=b;
}
buf[l
++]=(byte)n;

byte[]retBys=newbyte[l];
for(inti=0;i<l;++i)
{
retBys[i]
=(byte)(buf[l-i-1]|0x80);
}
retBys[l
-1]&=0x7F;
returnretBys;

}

//读取文件
privatebyte[]readFile(stringFileName)
{
if(FileName.EndsWith(".txt")){

StreamReadersr
=null;
try{
sr
=newStreamReader(FileName,Encoding.Default);
stringtext=sr.ReadToEnd();
byte[]bf=Encoding.UTF8.GetBytes(text);
returnbf;
}
catch{
returnnewbyte[0];
}
finally{
if(sr!=null)sr.Close();
}
}
FileStreamfs
=null;
try
{
fs
=newFileStream(FileName,FileMode.Open,FileAccess.ReadWrite,FileShare.None);//没有设定Buffsize
byte[]bf=newbyte[fs.Length];
fs.Read(bf,
0,(int)fs.Length);
returnbf;
}
catch{
returnnewbyte[0];
}
finally{
if(fs!=null)fs.Close();
}
}

privatestringgetContentId(stringFileName)
{
intat=FileName.LastIndexOf("\");
if(at<0)
at
=FileName.LastIndexOf("/");
returnFileName.Substring(at+1);
}

privatebyte[]appendContent(byte[]srcBys,byte[]destBys)
{
Array.Resize(
refdestBys,srcBys.Length+destBys.Length);
Array.Copy(srcBys,
0,destBys,destBys.Length-srcBys.Length,srcBys.Length);
returndestBys;
}

privatebyte[]appendContent(stringsz,byte[]byDest)
{
returnappendContent(Encoding.Default.GetBytes(sz),byDest);
}
}

///<summary>
///MMSender的摘要说明。
///
///</summary>
publicclassMMSender
{
//设置参数
stringsMmscUrl="http://mmsc.monternet.com";
stringsProxyUrl="10.0.0.172:80";

publicMMSender()
{
//
//TODO:在此处添加构造函数逻辑
//
}
publicvoidSetMMSC(stringszUrl)
{
sMmscUrl
=szUrl;
}
publicvoidSetProxy(stringszUrl)
{
sProxyUrl
=szUrl;
}


/*发送MMS的过程
1>创建消息发送接口
MMSenderms=newMMSender();
2>设置参数属性
默认属性已经是中国移动参数,因此如果是中国移动用户,以下两个操作可以不需要
ms.SetMMSC("
http://mmsc.monternet.com");
ms.SetProxy("10.0.0.172:80");
3>创建消息
MMessagemm=newMMessage();
4>设置消息内容
mm.SetSubject("标题");//设置标题
mm.AddTo("13810034500");//添加接收号码,调用一次添加一个接收号码
mm.AddFile("FileName");//添加发送文件,包含文件路径,调用一次添加一个发送文件
5>发送消息
stringszReult=ms.Send(mm);

6>继续发送其他号码
mm.ClearTo();
mm.AddTo("13812345678");
ms.Send(mm);
*/


/*避免协议冲突的设置
<configuration>
<system.net>
<settings>
<httpWebRequestuseUnsafeHeaderParsing="true"/>
</settings>
</system.net>
</configuration>
*/




publicstringSend(MMessagemm)
{
try
{
byte[]byMM=mm.MakeMMSContent();
if(byMM.Length>50*1024)
return"Thepackageistoolarge!";
//验证参数有效性
//FileStreamfs=newFileStream("d:/aaa.mms",FileMode.Create);
//fs.Write(byMM,0,byMM.Length);
//fs.Close();
//return"OK";
WebRequestwReq=WebRequest.Create(sMmscUrl);
HttpWebRequesthReq
=(HttpWebRequest)wReq;
wReq.Headers.Clear();
if(sProxyUrl.Length>0)
wReq.Proxy
=newWebProxy(sProxyUrl);

wReq.ContentType
="application/vnd.wap.mms-message";
hReq.Accept
="application/vnd.wap.mms-message,text/plain,*/*";
wReq.Method
="POST";
hReq.KeepAlive
=false;
hReq.UserAgent
="Nokia6681/2.0(4.00.15)SymbianOS/8.0Series60/2.6Profile/MIDP-2.0Configuration/CLDC-1.1";
//WritePostDat

hReq.ContentLength
=byMM.Length;
StreamsReq
=wReq.GetRequestStream();
sReq.Write(byMM,
0,byMM.Length);
sReq.Close();
WebResponsewRes
=wReq.GetResponse();
HttpWebResponsehRes
=(HttpWebResponse)wRes;
if(hRes.StatusCode==HttpStatusCode.OK)
{
StreamsRes
=wRes.GetResponseStream();
StreamReadersr
=newStreamReader(sRes);
stringszResult=sr.ReadToEnd();//发送结果
sr.Close();
returnszResult;
}
}
catch(Exceptione)
{
thrownewException(e.Message);
}
returnstring.Empty;
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值