⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mmslib.cs

📁 实现了mm1接口. GPRS modem通过此库可以发送彩信.
💻 CS
字号:
using System;
using System.Collections;
using System.Text;
using System.IO;

namespace MmsLib
{
	/// <summary>
	/// MmsCnt 的摘要说明。
	/// </summary>
	public class MmsCnt
	{
		// 设置参数
		string sMmscUrl="http://mmsc.monternet.com";
		string sProxyUrl="http://10.0.0.172:80";
		// HttpCnt
		private Http.HttpCnt hc=new Http.HttpCnt();
		
		private string[,] UaList=new string[,]{   {"NokiaN73-1/2.0628.0.0.1 S60/3.0 Profile/MIDP-2.0 Configuration/CLDC-1.1","http://nds.nokia.com/uaprof/NN73-1r100.xml"},
												  {"NokiaN70-1/5.0609.2.0.1 Series60/2.8 Profile/MIDP-2.0 Configuration/CLDC-1.1","http://nds.nokia.com/uaprof/NN70-5r100.xml"},
												  {"Nokia6680/1.0 (3.04.37) SymbianOS/8.0 Series60/2.6 Profile/MIDP-2.0 Configuration/CLDC-1.1","http://nds.nokia.com/uaprof/N6680r100.xml"},
												  {"Nokia6681/2.0 (5.37.01) SymbianOS/8.0 Series60/2.6 Profile/MIDP-2.0 Configuration/CLDC-1.1","http://nds.nokia.com/uaprof/N6681r100.xml"},
												  {"Nokia7210/1.0 (4.74) Profile/MIDP-1.0 Configuration/CLDC-1.0","http://nds.nokia.com/uaprof/N7210r200.xml"},
												  {"Nokia3230/2.0 (5.0604.0ch) SymbianOS/7.0s Series60/2.1 Profile/MIDP-2.0 Configuration/CLDC-1.0","http://nds.nokia.com/uaprof/N3230r100.xml"},
												  {"Nokia3250/2.0 (3.21) SymbianOS/9.1 Series60/3.0 Profile/MIDP-2.0 Configuration/CLDC-1.1","http://nds.nokia.com/uaprof/N3250r200.xml"},
												  {"Nokia6630/1.0 (5.03.08) SymbianOS/8.0 Series60/2.6 Profile/MIDP-2.0 Configuration/CLDC-1.1","http://nds1.nds.nokia.com/uaprof/N6630r100.xml"}
											  };

		public MmsCnt()
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
			hc.hHeaders.Add("Accept","text/html, application/vnd.wap.xhtml+xml, application/xhtml+xml, text/css, multipart/mixed, text/vnd.wap.wml, application/vnd.wap.wmlc, application/vnd.wap.wmlscriptc, application/java-archive, application/java, application/x-java-archive, text/vnd.sun.j2me.app-descriptor, application/vnd.met.ticket, application/x-wallet-appl.user-data-provision, application/vnd.oma.drm.message, application/vnd.oma.drm.content, application/vnd.wap.mms-message, application/vnd.wap.sic, text/x-co-desc, application/vnd.oma.dd+xml, application/x-javascript, text/ecmascript, */*");
			hc.hHeaders.Add("Accept-Charset","iso-8859-1, utf-8, iso-10646-ucs-2; q=0.6");
			//InsertHttpHeader("Accept-Encoding","gzip,deflate,identity;q=0.9");
			hc.hHeaders.Add("Accept-Language","zh-cn, zh");
			// 随机选取一个Ua
			Random rn = new Random();
			int at = rn.Next(UaList.Length/2);
			hc.hHeaders.Add("User-Agent",UaList[at,0]);
			hc.hHeaders.Add("x-wap-profile",UaList[at,1]);
			hc.hHeaders.Add("Connection","Keep-Alive");
		}
		
		public void SetMMSC(string szUrl)
		{
			sMmscUrl =szUrl;
		}
		public void SetProxy(string szUrl)
		{
			sProxyUrl = szUrl;
		}
		public void SetLocalConn(string szLocal)
		{
			hc.sLocalConn = szLocal;
		}
		// 发送mms
		/* 发送MMS的过程
		1> 创建消息发送接口
			  MMSender ms = new MMSender();
		2> 设置参数属性
			  默认属性已经是中国移动参数,因此如果是中国移动用户,以下两个操作可以不需要
			  ms.SetMMSC("http://mmsc.monternet.com");
			  ms.SetProxy("10.0.0.172:80");
		3> 创建消息
			  MMessage mm= new MMessage();
		4> 设置消息内容
			  mm.SetSubject("标题");      // 设置标题
			  mm.AddTo("13823456789");    // 添加接收号码,调用一次添加一个接收号码
		      mm.AddFile("FileName");     // 添加发送文件,包含文件路径,调用一次添加一个发送文件
		5> 发送消息
			   string szReult =ms.Send(mm);
			   
		6> 继续发送其他号码
		      mm.ClearTo();
			  mm.AddTo("13812345678");
			  ms.Send(mm);		     
		*/

		public string Send(MMessage mm)
		{
			string szReturn=string.Empty;
			try
			{
				szReturn=hc.Req(sMmscUrl,mm.GetContent(),"POST",sProxyUrl);
				Console.WriteLine(szReturn);
			}
			catch(Exception e)
			{
				Console.WriteLine(e.Message);
			}
			return szReturn;
		}
		
		public string Recv(string Url)
		{
			
			return string.Empty;
		}
	}
	
	/// <summary>
	/// Message 的摘要说明。
	/// </summary>
	public class MMessage
	{
		string Subject ="";
		int nSeconds =0; // 设置送达的时间,当前相对时间,以秒为单位
		ArrayList lFile = new ArrayList();   // 彩信文件列表
		ArrayList lDest = new ArrayList();   // 发送号码集合
		
		static long nSeq =0;
		
		public MMessage()
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
		}
		
		public void SetSubject(string szSubject)
		{
			Subject =szSubject;
		}
		public void SetDeliverTime(int nSec)
		{
			nSeconds = nSec;
		}
		// 
		public void AddTo(string Dest)
		{
			lDest.Add(Dest);
		}
		
		public void ClearTo()
		{
			lDest.Clear();
		}
		public void AddFile(string File)
		{
			lFile.Add(File);
		}

		// 得到二进制编码字节
		public byte[] GetContent()
		{
			
			byte[] byMms = new byte[0];
			// 消息头开始
			//X-Mms-Message-Type
			byMms = AppendOct(new byte[]{0x8C,0x80},byMms);
			//X-Mms-Transaction-ID
			byMms = AppendOct(new byte[]{0x98},byMms);
			byMms= AppendOct(nSeq.ToString(),byMms);
			nSeq++; // 序列号加1

			byMms= AppendOct(new byte[]{0x0},byMms);
			
			//X-Mms-MMS-Version
			byMms = AppendOct(new byte[]{0x8D,0x90},byMms);
			//Date
			
			//From,设置为 Insert-address-token
			byMms = AppendOct(new byte[]{0x89,0x01,0x81},byMms);
			
			//To
			for(int i=0;i<lDest.Count;i++)
			{
				byMms= AppendOct(new byte[]{0x97},byMms);
				byMms= AppendOct("+86"+ (string)lDest[i] +"/TYPE=PLMN",byMms);
				byMms= AppendOct(new byte[]{0x0},byMms);
			}
			//Subject
			if(Subject.Length >0)  // 使用Utf8编码
			{				
				byMms= AppendOct(new byte[]{0x96},byMms);
				// Value-length Char-set Text -string
				byte[] byLen= new byte[1];
				byLen[0] = (byte) (Encoding.UTF8.GetByteCount(Subject) +2);
				byMms= AppendOct(byLen,byMms);
				// Char-set 为utf-8
				byMms= AppendOct(new byte[]{0xEA},byMms);
				byMms= AppendOct(Encoding.UTF8.GetBytes(Subject),byMms);
				byMms= AppendOct(new byte[]{0x0},byMms);
			}
			// X-Mms-Delivery-Time,递送时间 = Relative-token Delta-seconds-value
			// Relative-token = 0x81
			// Delta-seconds-value =Long-integer
			// Long-integer = Short-length Multi-octet-integer
			if( nSeconds >0)
			{
				byMms= AppendOct(new byte[]{0x87},byMms);
				byte[] bfTime = BitConverter.GetBytes(nSeconds);  // 默认使用Big-endian,需用改为Little-endian
				// bfTime改为Little-endian
				Array.Reverse(bfTime);		
				byte[] bfTimeLen = new byte[3];
				bfTimeLen[0] = (byte) (bfTime.Length + 2);
				bfTimeLen[1] = 0x81;  // 相对时间格式
				bfTimeLen[2] = (byte)bfTime.Length;
				byMms= AppendOct(bfTimeLen,byMms);
				byMms= AppendOct(bfTime,byMms);
			}

			//Content-Type:application/vnd.wap.multipart.mixed
			byMms = AppendOct(new byte[]{0x84,0xA3},byMms);	
			// 消息体开始(MIME multipart)
			// 8.5.2 Multipart Header
			// nEntries Uintvar The number of entries in the multipart entity
			byte[] byFileCount = new byte[1];
			byFileCount[0] = (byte)lFile.Count;
			byMms = AppendOct(byFileCount,byMms);
			// 8.5.3 Multipart Entry,逐个加入媒体文件
			for(int j=0;j<lFile.Count;j++)
			{
				byMms = AppendOct(GetMmsContent(lFile[j].ToString()),byMms);
			}
			return byMms;
		}		

		// Tools
		// 加入媒体文件到彩信内容中去
		private byte[] GetMmsContent(string FileName)
		{
			// 每一个Multipart Entry由5个部分组成
			/* HeadersLen
			 * DataLen
			 * ContentType
			 * Headers
			 * Data
			 * */			
			byte[] byHeaders = new byte[0];   // ContentType和Headers组合
			byte[] byData = ReadFromFile(FileName);
			
			string FileID = getContentId(FileName);
			// Set content-type
			if( FileName.EndsWith(".txt"))
			{
				byHeaders = new byte[1];
				byHeaders[0] = (byte) (Encoding.ASCII.GetByteCount(FileID)+5);
				byHeaders = AppendOct(new byte[]{0x83,0x85},byHeaders);   // Utf-8
				byHeaders = AppendOct(Encoding.ASCII.GetBytes(FileID),byHeaders);
				byHeaders = AppendOct(new byte[]{0x00},byHeaders);
				byHeaders = AppendOct(new byte[]{0x81,0xEA},byHeaders);
			}
			else if( FileName.EndsWith(".gif"))
			{
				byHeaders = new byte[]{0x9D};
			}
			else if( FileName.EndsWith(".mid") || FileName.EndsWith(".midi"))
			{
				byHeaders = Encoding.ASCII.GetBytes("audio/midi");
				byHeaders = AppendOct(new byte[]{0x00},byHeaders);  // 文本需要以0x00结尾
			}
			
			// 加入Content-ID和Content-Location
			byHeaders = AppendOct(new byte[]{0xC0,0x22,0x3C},byHeaders);
			byHeaders = AppendOct(Encoding.ASCII.GetBytes(FileID),byHeaders);
			byHeaders = AppendOct(new byte[]{0x3E,0x00},byHeaders);
			//加入Content-Location
			byHeaders = AppendOct(new byte[]{0x8E},byHeaders);
			byHeaders = AppendOct(Encoding.ASCII.GetBytes(FileID),byHeaders);
			byHeaders = AppendOct(new byte[]{0x00},byHeaders);

			byte[] byHeaderLen = encodeUintvar( byHeaders.Length);
			byte[] byDataLen = encodeUintvar(byData.Length);
			
			byte[] byMmc = new byte[ 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);

			return byMmc;
		}
		
		private byte[]  encodeUintvar(int n)
		{
			byte[] buf = new byte[8];
			int l=0;
			while(n >=128)
			{
				byte b = (byte)(n &0x7F);
				n = n >> 7;
				buf[l++] =b;
			}
			buf[l++]= (byte)n;

			byte[] retBys = new byte[l];
			for(int i=0;i<l;++i)
			{
				retBys[i] = (byte)(buf[l-i-1]|0x80);
			}
			retBys[l-1] &= 0x7F;
			return retBys;

		}
		// 从文件中读取字节
		private byte[] ReadFromFile(string FileName)
		{
			byte[] bf = new byte[0];
			FileStream fs = null;
			try
			{
				fs= new FileStream(FileName,FileMode.Open,FileAccess.ReadWrite,FileShare.None);  // 没有设定Buffsize
			}
			catch(Exception e)
			{
				Console.WriteLine(e.ToString());
			}
			if( fs!=null)
			{
				bf = new byte[fs.Length];
				fs.Read(bf,0,(int)fs.Length);
				fs.Close();
			}			
			return bf;
		}
		// 得到文件名(不包含文件夹部分)
		private string getContentId(string FileName)
		{
			int at =FileName.LastIndexOf("\\");
			if( at <0)
				return FileName;
			else
				return FileName.Substring(at+1);
		}
		// 增加字节
		private byte[] AppendOct(byte[] bys,byte[] byDest)
		{
			byte[] bysNew =new byte[byDest.Length +bys.Length];
			try
			{
				Array.Copy(byDest,bysNew,byDest.Length);
				Array.Copy(bys,0,bysNew,byDest.Length,bys.Length);
			}
			catch(Exception e)
			{
				Console.WriteLine(e);
			}
			return bysNew;
		}
		// 增加字符串
		private byte[] AppendOct(string sz,byte[] byDest)
		{
			return AppendOct(Encoding.Default.GetBytes(sz),byDest);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -