当前位置: 首页 > news >正文

开放 mm1接口发送彩信(也就是GPRS modem发送彩信) 代码 c#版

mm1接口也就是手机终端和mmsc(中国移动就是http: // mmsc.monternet.com)用来发送mms   message的接口,   GPRS   modem当然也是可以使用这一接口发送的.   
    
  
using    System;   
  
using    System.Net;   
  
using    System.IO;   
  
using    System.Diagnostics;   
  
using    System.Threading;   
  
using    System.Collections;   
  
using    System.Text;   
    
  
namespace    MMSLib   
  
{   
  
/**////   <summary>   
  
///   MMSender   的摘要说明。   
  
///     
  
///   </summary>   

  public   class   MMSender   
  
{   
  
//   设置参数   
  string   sMmscUrl="http://mmsc.monternet.com";   
  
string   sProxyUrl="10.0.0.172:80";   
    
  
public   MMSender()   
  
{   
  
//   
  
//   TODO:   在此处添加构造函数逻辑   
  
//   
  }
   
  
public   void   SetMMSC(string   szUrl)   
  
{   
  sMmscUrl   
=szUrl;   
  }
   
  
public   void   SetProxy(string   szUrl)   
  
{   
  sProxyUrl   
=   szUrl;   
  }
   
    
    
  
/**//*   发送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("13825271511");         //   添加接收号码,调用一次添加一个接收号码   
      mm.AddFile("FileName");           //   添加发送文件,包含文件路径,调用一次添加一个发送文件   
  5>   发送消息   
        string   szReult   =ms.Send(mm);   
          
  6>   继续发送其他号码   
      mm.ClearTo();   
      mm.AddTo("13812345678");   
      ms.Send(mm);             
  
*/
   
    
    
  
/**//*   避免协议冲突的设置   
  <configuration>   
  <system.net>   
  <settings>   
  <httpWebRequest   useUnsafeHeaderParsing="true"/>   
  </settings>   
  </system.net>   
  </configuration>   
          
*/
   
  
public   string   Send(MMessage   mm)   
  
{   
  
try   
  
{   
  
//   验证参数有效性   
  WebRequest   wReq   =   WebRequest.Create(sMmscUrl);   
  HttpWebRequest   hReq   
=   (HttpWebRequest)wReq;   
  wReq.Headers.Clear();   
  
if(   sProxyUrl.Length   >0)   
  wReq.Proxy   
=   new   WebProxy(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.0   Series60/2.6   Profile/MIDP-2.0   Configuration/CLDC-1.1";   
  
//   Write   Post   Dat   
  byte[]   byMM   =   mm.GetContent();   
  hReq.ContentLength   
=   byMM.Length;   
  Stream   sReq   
=   wReq.GetRequestStream();   
  sReq.Write(byMM,
0,byMM.Length);   
  sReq.Close();   
  
//   Http   Request   
  WebResponse   wRes=   wReq.GetResponse();   
  HttpWebResponse   hRes   
=   (HttpWebResponse)wRes;   
  
if   (   hRes.StatusCode   ==   HttpStatusCode.OK)   
  
{   
  Stream   sRes   
=   wRes.GetResponseStream();   
  StreamReader   sr   
=   new   StreamReader(sRes);   
  
string   szResult   =sr.ReadToEnd();     //   发送结果   
  
//   Parse   result   sring   
  return   szResult;   
  }
   
  }
   
  
catch(Exception   e)   
  
{   
  
throw   new   Exception(e.Message);   
  }
   
  
return   string.Empty;   
  }
   
  }
   
  }
   
  
 
  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   AddFile(string   File)   
  
{   
  lFile.Add(File);   
  }
   
    
  
public   void   ClearTo()   
  
{   
  lDest.Clear();   
  }
   
    
  
//   得到二进制编码字节   
  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);   
  }
   
  }
   


原文: http://topic.csdn.net/t/20061128/00/5189609.html

相关文章:

  • ActiveMQ 与 Spring
  • 菜鸟学SQL注入 --- 一个简单的教学案例
  • 块级元素和内联元素的区别与转换
  • SQL Server 2005 触发器应用
  • hadoop-hdfs文件系统的安装与部署
  • 第四篇:数据预处理(一) - 缺失值处理
  • HDU 2586 How far away ?(LCA模板 近期公共祖先啊)
  • Mocha BSM产品亮点——报告管理
  • Spring-data-jpa详解,全方位介绍。
  • 互联网DNS存在重大漏洞 黑客可能控制网络流量
  • 基于 POI 封装 ExcelUtil 精简的 Excel 导入导出
  • 发布Web服务器上的虚拟主机:ISA2006系列之十一
  • NFS部署及优化(二)
  • java枚举与.net中的枚举区别
  • 在Hibernate中配置多对多连接表
  • 【css3】浏览器内核及其兼容性
  • 【Under-the-hood-ReactJS-Part0】React源码解读
  • 【挥舞JS】JS实现继承,封装一个extends方法
  • CentOS7 安装JDK
  • download使用浅析
  • js正则,这点儿就够用了
  • js作用域和this的理解
  • LeetCode29.两数相除 JavaScript
  • 关于springcloud Gateway中的限流
  • 前端相关框架总和
  • 如何将自己的网站分享到QQ空间,微信,微博等等
  • 如何在GitHub上创建个人博客
  • 微服务入门【系列视频课程】
  • # Swust 12th acm 邀请赛# [ K ] 三角形判定 [题解]
  • ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
  • (145)光线追踪距离场柔和阴影
  • (附源码)ssm基于jsp的在线点餐系统 毕业设计 111016
  • (更新)A股上市公司华证ESG评级得分稳健性校验ESG得分年均值中位数(2009-2023年.12)
  • (六)软件测试分工
  • (论文阅读32/100)Flowing convnets for human pose estimation in videos
  • (免费领源码)python#django#mysql公交线路查询系统85021- 计算机毕业设计项目选题推荐
  • (转)JVM内存分配 -Xms128m -Xmx512m -XX:PermSize=128m -XX:MaxPermSize=512m
  • ./indexer: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object fil
  • .net 生成二级域名
  • .NET 使用 XPath 来读写 XML 文件
  • .NET开发者必备的11款免费工具
  • /var/spool/postfix/maildrop 下有大量文件
  • @AutoConfigurationPackage的使用
  • @FeignClient 调用另一个服务的test环境,实际上却调用了另一个环境testone的接口,这其中牵扯到k8s容器外容器内的问题,注册到eureka上的是容器外的旧版本...
  • @Repository 注解
  • []常用AT命令解释()
  • [2018-01-08] Python强化周的第一天
  • [2019.3.20]BZOJ4573 [Zjoi2016]大森林
  • [Android Pro] AndroidX重构和映射
  • [C/C++]_[初级]_[关于编译时出现有符号-无符号不匹配的警告-sizeof使用注意事项]
  • [echarts] y轴不显示0
  • [EWS]查找 文件夹
  • [jQuery]使用jQuery.Validate进行客户端验证(中级篇-上)——不使用微软验证控件的理由...
  • [LeetCode] 148. Sort List 链表排序
  • [New Portal]Windows Azure Virtual Machine (3) 在VM上挂载磁盘