代码改变世界

HttpWebRequest的经典实例

2009-03-14 18:15  苏飞  阅读(3439)  评论(0编辑  收藏  举报

这是一个用来群发短信的例子,Xml文件自己写好我这里不能贴出来,因为呵呵,

大家都知道里面有用户名和密码吗,呵呵,自己用的时候自己写一下就行了。

代码里有注释不用我多说了

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.IO;
using System.Xml;

public partial class PersonnelManagement_admin_Function_Send_Message : System.Web.UI.Page
{
    /*编写时间:2009年3月7号
     *编 写 人:苏飞
     *地    点:河南诺特科贸有限公司
     *摘    要:完成发送信息邀请的功能
    */

    //加载时执行
    protected void Page_Load(object sender, EventArgs e)
    {
        //在界面加载时要执行的代码
    }

    //更新XMl文件
    private string  updateXml(string Tellnumber, string Messages)
    {
        try
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(Server.MapPath(".\\Xml\\Messages.xml"));
            //获取dbGuest节点的所有子节点查询修改手机号码
            xmlDoc.SelectSingleNode("//REQ//Body//REQParams//REQParam[Name='MSISDN']").ChildNodes.Item(1).InnerText =Tellnumber;
            //获取dbGuest节点的所有子节点查询修改信息内容
            xmlDoc.SelectSingleNode("//REQ//Body//REQParams//REQParam[Name='msg']").ChildNodes.Item(1).InnerText =Messages;
            xmlDoc.Save(Server.MapPath(".\\Xml\\Messages.xml"));
            //取出所有的Xml文件的内容
            string strXml = File.ReadAllText(Server.MapPath(".\\Xml\\Messages.xml")).ToString();
            Response.Write(ConfigurationManager.ConnectionStrings["Messgaes"].ConnectionString);
            Response.Write(strXml);
            return strXml;
        }
        catch (Exception ex)
        {
            lblMessage.Visible = true;
            lblMessage.Text = ex.Message.ToString().Trim();
            return null;
        }
    }

    //Xml文件形式的发送
    private void BtnGoMessages(string Tellnumber, string Messages)
    {
        try
        {
            //创建连接
            HttpWebRequest mHttpRequest = (HttpWebRequest)HttpWebRequest.Create(ConfigurationManager.ConnectionStrings["Messgaes"].ConnectionString);
            //超时间毫秒为单位
            mHttpRequest.Timeout = 1800;
            //发送请求的方式
            mHttpRequest.Method = "POST";
            //发送的协议
            mHttpRequest.Accept = "HTTP";
            //字节范围
            mHttpRequest.AddRange(100, 10000);
            //是否和请求一起发送
            mHttpRequest.UseDefaultCredentials = true;
            //写数据信息的流对象
            string strXmll= updateXml(Tellnumber, Messages);
            StreamWriter swMessages = new StreamWriter(mHttpRequest.GetRequestStream());
            //写入的流以XMl格式写入
            swMessages.Write(strXmll);
            //关闭写入流
            swMessages.Close();
            //创建一个响应对象
            HttpWebResponse mHttpResponse = (HttpWebResponse)mHttpRequest.GetResponse();
            if (mHttpResponse.StatusDescription == "OK")
            {
                lblMessage.Visible = true;
                lblMessage.Text = "短信已下发";
            }
            else
            {
                lblMessage.Visible = true;
                lblMessage.Text = "短信发送失败";
            }
            mHttpResponse.Close();
          
        }
        catch (WebException ex)
        {
            lblMessage.Visible = true;
            lblMessage.Text = ex.Message.ToString().Trim();
        }
        catch (Exception ex)
        {
            lblMessage.Visible = true;
            lblMessage.Text = ex.Message.ToString().Trim();
        }
    }

    //发送信息
    protected void btnGo_Click(object sender, EventArgs e)
    {
        try
        {
            //手机号码的字符串
            string strNumber = txtNumber.Text.ToString().Trim();
            //先发送第一个电话号码的
            BtnGoMessages(strNumber.Substring(0, 11).ToString().Trim(), txtMessages.Text.ToString().Trim());
            ////输出到前台用来测试
            //Response.Write(strNumber.Substring(0, 11).ToString().Trim() + "<br />");
            for (int i = 11; i < strNumber.Length; i++)
            {
                if (strNumber.Substring(i, 1) == "," || strNumber.Substring(i, 1) == ",")
                {
                    //发送第I条信息
                    BtnGoMessages(strNumber.Substring(i + 1, 11).ToString().Trim(), txtMessages.Text.ToString().Trim());
                }
            }
        }
        catch (Exception ex)
        {
            lblMessage.Visible = true;
            lblMessage.Text = ex.Message.ToString().Trim();
        }
    }
}