开放 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;
}
}
}