簡體   English   中英

從C#Windows應用程序發送短信?

[英]Sending sms from c# windows app?

可能這不是一個真正的問題,但是我需要幫助,因為我厭倦了這樣做。 我想從我的C#Windows應用程序中發送短信,我讀了很多博客,還發現了一些代碼,但是那是行不通的。 我引用此鏈接以使用API

http://ubaid.tk/api-usage/

我的代碼是:

public void send(string uid, string password, string message, string no)
    {
        try
        {
            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://ubaid.tk/sms/sms.aspx?uid=" + uid + "&pwd=" + password + "&msg=" + message + "&phone=" + no + "&provider=fullonsms");
            HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse();
            System.IO.StreamReader respStreamReader = new System.IO.StreamReader(myResp.GetResponseStream());
            string responseString = respStreamReader.ReadToEnd();
            respStreamReader.Close();
            myResp.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
            string msg = ex.ToString();
        }
    }

但這給了我這個錯誤:

System.Net.WebException: The operation has timed out at system.Net.HttpWebRequest.GetResponse()

我不知道如何解決此錯誤。我需要幫助,如果有人有其他更好的鏈接,請分享。

在此先感謝您的幫助。

因此,查看短信站點時 ,它表示其服務已被IP阻止。 它沒有指定IP塊是來自小區公司還是來自ISP,因此可能會出現問題。 它還表示他們經歷了停機時間,因此可能是他們的服務器停機了。 也可能是他們的服務器速度慢,並且您的程序超時太快。

    SMS API CLASS::

    #region Namespaces
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data;
    using System.Data.SqlClient;
    using System.Data.Sql;
    using System.Data.SqlTypes;
    using System.Configuration;
    using System.Net;
    using System.IO;
    using SendBirthdaySMS;
    #endregion

    /// <summary>
    /// Summary description for SMSAPI
    /// </summary>
    public class SMSAPI
    {

        MyConnection con = new MyConnection();
        public DataSet ds = new DataSet();
        public SqlDataAdapter da = new SqlDataAdapter();
        public SMSAPI()
        {
            //
            // TODO: Add constructor logic here
            //
        }


        /*Get The SMS API*/
        public string GETSMSAPI()
        {

            con.Open();
            string QuerySMS, StringSMS, APISMS, UserName, Password, Sender, Priority;
            QuerySMS = "SP_SMSAPIMaster_GetDetail";
            StringSMS = APISMS = UserName = Password = Sender = Priority = "";
            con.cmd.CommandText = QuerySMS;
            try
            {
                con.Open();
                con.cmd.ExecuteNonQuery();
                SqlDataAdapter da = new SqlDataAdapter(con.cmd);
                da.Fill(ds, "tbl_SMSAPIMaster");

                if (ds.Tables["tbl_SMSAPIMaster"].Rows.Count > 0)
                {
                    APISMS = ds.Tables["tbl_SMSAPIMaster"].Rows[0]["SMSAPI"].ToString();
                    UserName = ds.Tables["tbl_SMSAPIMaster"].Rows[0]["UserName"].ToString();
                    Password = ds.Tables["tbl_SMSAPIMaster"].Rows[0]["Password"].ToString();
                    Sender = ds.Tables["tbl_SMSAPIMaster"].Rows[0]["Sender"].ToString();
                    Priority = ds.Tables["tbl_SMSAPIMaster"].Rows[0]["Priority"].ToString();
                    StringSMS = APISMS.Replace("uid", UserName).Replace("psd", Password).Replace("sndid", Sender).Replace("prt", Priority);
                }
                else
                {
                    StringSMS = "";
                }
            }
            catch
            {
            }
            finally
            {
                con.Close();
            }

            return StringSMS;

        }
        /*Call The SMS API*/
        public string GetAPICALL(string url)
        {
            HttpWebRequest httpreq = (HttpWebRequest)WebRequest.Create(url);
            try
            {
                HttpWebResponse httpres = (HttpWebResponse)httpreq.GetResponse();
                StreamReader sr = new StreamReader(httpres.GetResponseStream());
                string results = sr.ReadToEnd();
                sr.Close();
                return results;
            }
            catch
            {
                return "0";
            }
        }

    }



 private void SendBirthdaySMS(string lbl_MobileNo)
        {
            #region For Employee Message Content..!!
            string Msgs, SMSString;
            Msgs = SMSString = "";
            SMSString = SMSAPI.GETSMSAPI();
            Msgs = "Hello";
            lblMessageContent.Text = Msgs;
            SMSString = SMSString.Replace("num", lbl_MobileNo).Replace("fedmesge", Msgs);
            string Result = SMSAPI.GetAPICALL(SMSString);
            #endregion


        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM