在asp.net程式透過另一台server的簡訊服務來發送簡訊,
因為不希望讓user知道發送簡訊服務的網址來源
因此在程式後端透過WebRequest的方式來傳遞訊息
並且取得回應的訊息並Show回我們的介面。

Private Function SendMMS(ByVal tel_num As String, ByVal mms_msg As String) As String

        If tel_num.Trim.Length = 0 Then Return "簡訊傳送失敗,查無手機號碼!!"
        If tel_num.Trim.Length <> 10 Then Return "簡訊傳送失敗,手機號碼需為10碼!!"
        If tel_num.Substring(0, 2) <> "09" Then Return "簡訊傳送失敗,手機號碼格式錯誤!!"
        
        '  簡訊發送內容
        Dim args As StringString.Empty
        '  請記得將中文訊息Encode成big5格式,否則收到的簡訊有可能會是亂碼
        System.Web.HttpContext.Current.Response.ContentEncoding = _ 
                System.Text.Encoding.GetEncoding("big5")
        args = System.Web.HttpContext.Current.Server.UrlEncode(mms_msg)

        ' 提供簡訊服務程式的URL 網址
        Dim url As String = String.Format("http://xxx.xxx.xxx?tel={0}&mms={1}", tel_num, args)
        Dim reqt As System.Net.WebRequest = System.Net.WebRequest.Create(url)

        '  取得網頁資訊(網頁的Response訊息)
        Dim resp As System.Net.WebResponse = reqt.GetResponse()
        ' 宣告StreamReader讀取回應資料 
        Dim str As System.IO.StreamReader = New _
                System.IO.StreamReader (resp.GetResponseStream(),System.Text.Encoding.Default)
        ' 讀取回應資料
        Dim ret_content As String = str.ReadToEnd()
        str.Close()
        resp.Close()
        Return ret_content 

End Function

註:
其實大家應該可以知道這程式的關鍵不在於發送簡訊
而是WebRequest,WebRequest這類別將透過Create方法
取得該URI所對應Web 網頁內容,可以試著將上面程式碼中的url設定成yahoo的網址
可以看出真正的含意
arrow
arrow
    全站熱搜

    湯瑪的吳 發表在 痞客邦 留言(4) 人氣()