关闭

短消息发送代码

标签: stringencodingstreamexceptionservicebyte
736人阅读 评论(0) 收藏 举报
分类:

private void SendMsg(string strMobile,string strMsg,string strSvid,string strCPUID,string strPWD,string strSender)
//FeeType(资费类别)FeeCode(资费代码)ChargeTermID(计费号码)DestTermID(接收消息的目的号码)sReplyPath(用户回复号码)
//MsgContent(短消息内容)MsgType(消息类型)
{
   try
   {
 string postData="";
 Encoding encoding = Encoding.GetEncoding("utf-8");
    
 postData += "<linkrich-mt>";
 postData += "<version>200</version>";
 postData += "<customer>"+strCPUID+"</customer>";
 postData += "<pwd>"+strPWD+"</pwd>";
 postData += "<service>"+strSvid+"</service>";
 postData += "<srcmobile>"+strMobile+"</srcmobile>";
 postData += "<source>"+strSender+"</source>";
 postData += "<destmobile>"+strMobile+"</destmobile>";
 postData += "<type>17</type>";
 postData += "<message>"+strMsg+"</message>";
 postData += "<pid>0</pid>";
 postData += "<udhi>0</udhi>";
 postData += "<time>0</time>";
 postData += "</linkrich-mt>";
 
 string strUrl = "URL";//接口地址

 byte[] data = encoding.GetBytes(postData);

 // 准备请求...
 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strUrl);
 myRequest.Method = "POST";
 myRequest.ContentType="application/x-www-form-urlencoded";
 myRequest.ContentLength = data.Length;
 Stream newStream=myRequest.GetRequestStream();
 // 发送数据
 newStream.Write(data,0,data.Length);

 HttpWebResponse myHttpWebResponse = (HttpWebResponse)myRequest.GetResponse();
 Stream receiveStream = myHttpWebResponse.GetResponseStream();

 Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
 StreamReader readStream = new StreamReader(receiveStream, encode );

 strContent=readStream.ReadToEnd();
 strContent=strContent.Substring((strContent.IndexOf("<status>")+8),1);
 StreamWriter fs=new StreamWriter("E:/HttpLog/log.txt",true,System.Text.Encoding.Default);
 fs.WriteLine(DateTime.Now+":"+strMobile+"   "+strContent);
 fs.Close();
 newStream.Close();
    
    }   

    catch(Exception e1)
    {
 string strFileName="E:/HttpLog/errorlog.txt";
 StreamWriter fs=new StreamWriter(strFileName,true,System.Text.Encoding.Default);
 fs.WriteLine(DateTime.Now+":"+strMobile+"   "+e1.ToString());
 fs.WriteLine("");
 fs.Close();
    }
}
上面是发送短信的代码
接受的方法就只要用Request.QueryString[""]得到就可以了
不过要根据SP提供的文档而定

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:30858次
    • 积分:429
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:10篇
    • 译文:0篇
    • 评论:2条
    文章分类
    文章存档