C#调用第三方飞信接口发送短信

由于项目需要用短信作为告警通知,所以写了个简单的C#调用第三方接口发送短信的程序,在网上找了好多第三方的飞信接口,但是大都不能用了,也许是飞信官方的接口有所改变吧,所以造成以前的飞信接口不生效了。找好久才找到一个可用的接口

http://blbear.com/api/fetion/fetion.php?&username=XXX&password=XXX&to=XXX&message=%C4%E3%BA%C3

飞信API发布

网页:
http://blbear.com/api/fetion/

API接口:

http://blbear.com/api/fetion/fetion.php?username=飞信手机号码&password=飞信手机密码&to=接收人号码&message=短信内容

或者

http://blbear.com/api/fetion/fetion.php?u=飞信手机号码&p=飞信手机密码&t=接收人号码&m=短信内容

支持: POST  、GET


private void button_send_Click(object sender, EventArgs e)
        {
            string fno = textBox_fno.Text;
            string fp = textBox_fp.Text;
            string fto = textBox_fto.Text;
            string fm = textBox_fm.Text;
            fm = UrlEncode(fm);
            string url = "http://blbear.com/api/fetion/fetion.php?&username="+fno+"&password="+fp+"&to="+fto+"&message="+fm;

            string res = getContent(url);
            //MessageBox.Show(res);
        }

        public static string UrlEncode(string str)
        {
            StringBuilder sb = new StringBuilder();
            byte[] byStr = System.Text.Encoding.Default.GetBytes(str); //默认是System.Text.Encoding.Default.GetBytes(str)
            for (int i = 0; i < byStr.Length; i++)
            {
                sb.Append(@"%" + Convert.ToString(byStr[i], 16));
            }

            return (sb.ToString());
        }

        private static string getContent(string Url)
        {
            string strResult = "";
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                //声明一个HttpWebRequest请求  
                request.Timeout = 30000;
                //设置连接超时时间  
                request.Headers.Set("Pragma", "no-cache");
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream streamReceive = response.GetResponseStream();
                Encoding encoding = Encoding.GetEncoding("GB2312");
                StreamReader streamReader = new StreamReader(streamReceive, encoding);
                strResult = streamReader.ReadToEnd();
                streamReader.Close();
            }
            catch
            {
                throw;
            }
            return strResult;
        }


你可能感兴趣的:(String,C#,url,手机,textbox,encoding)