短信发送

本文介绍了使用中国短信网提供的接口实现短信发送的方法。通过具体的代码示例展示了如何构造发送请求,并采用POST方式发送短信及获取响应结果。

中国短信网的发送接口:

 

    /// <summary>
    /// 发送短信并返回结果
    /// </summary>
    /// <param name="hm">号码</param>
    /// <param name="nr">内容</param>
    /// <returns></returns>
    public static String fs(string hm, string nr)
    {
        StringBuilder sbTemp = new StringBuilder();

        sbTemp.Append("uid=*****&pwd=*****&mobile=" + hm + "&content=" + nr);
        byte[] bTemp = System.Text.Encoding.GetEncoding("GBK").GetBytes(sbTemp.ToString());
        String postReturn = Define.doPostRequest("http://http.c123.com/tx/", bTemp);
        return postReturn;
    }
    //POST方式发送得结果
    public static String doPostRequest(string url, byte[] bData)
    {
        System.Net.HttpWebRequest hwRequest;
        System.Net.HttpWebResponse hwResponse;

        string strResult = string.Empty;
        try
        {
            hwRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            hwRequest.Timeout = 5000;
            hwRequest.Method = "POST";
            hwRequest.ContentType = "application/x-www-form-urlencoded";
            hwRequest.ContentLength = bData.Length;

            System.IO.Stream smWrite = hwRequest.GetRequestStream();
            smWrite.Write(bData, 0, bData.Length);
            smWrite.Close();
        }
        catch (System.Exception err)
        {
            WriteErrLog(err.ToString());
            return strResult;
        }

        //get response
        try
        {
            hwResponse = (HttpWebResponse)hwRequest.GetResponse();
            StreamReader srReader = new StreamReader(hwResponse.GetResponseStream(), Encoding.ASCII);
            strResult = srReader.ReadToEnd();
            srReader.Close();
            hwResponse.Close();
        }
        catch (System.Exception err)
        {
            WriteErrLog(err.ToString());
        }

        return strResult;
    }
    public static void WriteErrLog(string strErr)
    {
        Console.WriteLine(strErr);
        System.Diagnostics.Trace.WriteLine(strErr);
    }

 这个网站只能接收到别人回复的短信,不能设置接收的号码,跟我的需求不同,就没继续使用下去,过几天我用其他的接口实现了短信查询功能后,再贴出来…

转载于:https://www.cnblogs.com/_zjl/archive/2011/04/20/2021873.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值