0

我最近使用 vb.net 连接到 SMS 提供程序 API

我创建了一个组表并在该组中插入了所有数字,然后到达每一行并发送触发 API 以处理发送。短信不是发给所有群成员的,它只成功发送到群中的第一个手机号码。

如何解决这个问题呢 ?我想我必须在每次发送之间设置一个延迟,但我没有用。我的代码如下:

  Function GetGroupsMobileNumbers() As ArrayList
    Dim MobileNumbersArrayList As New ArrayList


    For Each Contact As FilsPayComponent.ContactAddress In FilsPayComponent.ContactAddress.GetAllContactAddressByGroupId(ddlGroup.SelectedValue)
        MobileNumbersArrayList.Add(Contact.Mobile)
    Next



    Return MobileNumbersArrayList
End Function
Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click

    If ddlGroup.SelectedValue = 0 Then
        lbResult.Text = "No groups selected"
        Exit Sub
    End If

    Dim MobileNumbersArrayList As ArrayList
    MobileNumbersArrayList = GetGroupsMobileNumbers()

    If MobileNumbersArrayList.Count = 0 Then

        lbResult.Text = "Group doesnt contain numbers"


        Exit Sub
    End If

    Dim TotalNo As Integer = FilsPayComponent.ContactAddress.AddressContactsCount(ddlGroup.SelectedValue)
    If MobileNumbersArrayList.Count * messagecount.Value <= FilsPayComponent.SmSUser.GetSmSUserByUserId(Context.User.Identity.Name).Balance Then



        Dim txtMsg As String
        Dim smstype As Integer

        If hidUnicode.Value <> "1" Then
            txtMsg = txtMessage.Text
            smstype = 1
        Else
            txtMsg = ConvertTextToUnicode(txtMessage.Text)

            smstype = 2
        End If
        Dim x As Integer
        'For Each Contact As FilsPayComponent.ContactAddress In FilsPayComponent.ContactAddress.GetAllContactAddressByGroupId(ddlGroup.SelectedValue)
        For Each Contact In MobileNumbersArrayList.ToArray

            Dim toMobile As String = Contact.Mobile

            If toMobile.Length > 10 Then

                Dim ExecArrayList As ArrayList
                ExecArrayList = SendSMS(toMobile, txtMsg, smstype)

                '-- give the excution more time
                If ExecArrayList.Count < 1 Then
                    Threading.Thread.Sleep(1000)
                End If
                '-- give the excution more time
                If ExecArrayList.Count < 1 Then
                    Threading.Thread.Sleep(1000)
                End If
                '-- give the excution more time
                If ExecArrayList.Count < 1 Then
                    Threading.Thread.Sleep(1000)
                End If

                x = x + 1



                '  lbresult.Text = "Sent Successfully"
            End If

        Next

        FilsPayComponent.SmSUser.RemoveSmsCredit(Context.User.Identity.Name, messagecount.Value * x)

        Dim NewsmsarchiveItem As New FilsPayComponent.smsarchive
        NewsmsarchiveItem.FromMobile = txtSenderID.Text
        NewsmsarchiveItem.ToMobile = "0"
        NewsmsarchiveItem.GroupId = ddlGroup.SelectedValue
        NewsmsarchiveItem.DateSent = DateTime.Now
        NewsmsarchiveItem.Msg = txtMessage.Text
        NewsmsarchiveItem.GroupCount = x
        NewsmsarchiveItem.Optional1 = Context.User.Identity.Name
        NewsmsarchiveItem.Optional2 = "1"


        NewsmsarchiveItem.MessageNo = messagecount.Value

        Try
            NewsmsarchiveItem.Addsmsarchive()
            lbResult.Text = "Message sent successfully"
            btnSend.Visible = False

        Catch ex As Exception
            lbResult.Text = ex.Message
        End Try

    Else

        lbResult.Text = "Not enough credit, please refill "

    End If
End Sub

Sub SendSMS(ByVal toMobile As String, ByVal txtMsg As String, ByVal smstype As Integer)
    Dim hwReq As HttpWebRequest
    Dim hwRes As HttpWebResponse

    Dim smsUser As String = "xxxxxx"
    Dim smsPassword As String = "xxxxxx"
    Dim smsSender As String = "xxxxxx"




    Dim strPostData As String = String.Format("username={0}&password={1}&destination={2}&message={3}&type={4}&dlr=1&source={5}", Server.UrlEncode(smsUser), Server.UrlEncode(smsPassword), Server.UrlEncode(toMobile), Server.UrlEncode(txtMsg), Server.UrlEncode(smstype), Server.UrlEncode(smsSender))
    Dim strResult As String = ""
    Try

        hwReq = DirectCast(WebRequest.Create("http://xxxxx:8080/bulksms/bulksms?"), HttpWebRequest)

        hwReq.Method = "POST"
        hwReq.ContentType = "application/x-www-form-urlencoded"
        hwReq.ContentLength = strPostData.Length

        Dim arrByteData As Byte() = ASCIIEncoding.ASCII.GetBytes(strPostData)
        hwReq.GetRequestStream().Write(arrByteData, 0, arrByteData.Length)

        hwRes = DirectCast(hwReq.GetResponse(), HttpWebResponse)
        If hwRes.StatusCode = HttpStatusCode.OK Then
            Dim srdrResponse As New StreamReader(hwRes.GetResponseStream(), Encoding.UTF8)
            Dim strResponse As String = srdrResponse.ReadToEnd().Trim()
            Select Case strResponse
                Case "01"
                    strResult = "success"

                    Exit Select
                Case Else
                    strResult = "Error: " + strResponse
                    Exit Select


            End Select
        End If
    Catch wex As WebException
        strResult = "Error, " + wex.Message
    Catch ex As Exception
        strResult = "Error, " + ex.Message
    Finally
        hwReq = Nothing
        hwRes = Nothing
    End Try
End Sub
4

1 回答 1

0

如果function GetGroupsMobileNumbers()不返回数字数组列表(作为字符串),则注释掉。MobileNumbersArrayList = GetGroupsMobileNumbers() 然后使用下面注释掉的代码(使用您自己的三个电话号码)将其设置为测试。

    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
    If ddlGroup.SelectedValue = 0 Then
        lbResult.Text = "No groups selected"
        Exit Sub
    End If

    Dim MobileNumbersArrayList As New ArrayList
    MobileNumbersArrayList = GetGroupsMobileNumbers()

    'MobileNumbersArrayList.Add("07702123456")
    'MobileNumbersArrayList.Add("07702123457")
    'MobileNumbersArrayList.Add("07702123458")



    If MobileNumbersArrayList.Count = 0 Then

        lbResult.Text = "Group doesnt contain numbers"
        Exit Sub
    End If

    Dim TotalNo As Integer = FilsPayComponent.ContactAddress.AddressContactsCount(ddlGroup.SelectedValue)

    If MobileNumbersArrayList.Count * messagecount.Value <= FilsPayComponent.SmSUser.GetSmSUserByUserId(Context.User.Identity.Name).Balance Then
        Dim txtMsg As String
        Dim smstype As Integer

        If hidUnicode.Value <> "1" Then
            txtMsg = txtMessage.Text
            smstype = 1
        Else
            txtMsg = ConvertTextToUnicode(txtMessage.Text)
            smstype = 2
        End If

        Dim x As Integer
        For Each Contact In MobileNumbersArrayList
            If Contact.Length > 10 Then
                SendSMS(Contact, txtMsg, smstype)
                x = x + 1
            End If

        Next

        FilsPayComponent.SmSUser.RemoveSmsCredit(Context.User.Identity.Name, messagecount.Value * x)

        Dim NewsmsarchiveItem As New FilsPayComponent.smsarchive
        NewsmsarchiveItem.FromMobile = txtSenderID.Text
        NewsmsarchiveItem.ToMobile = "0"
        NewsmsarchiveItem.GroupId = ddlGroup.SelectedValue
        NewsmsarchiveItem.DateSent = DateTime.Now
        NewsmsarchiveItem.Msg = txtMessage.Text
        NewsmsarchiveItem.GroupCount = x
        NewsmsarchiveItem.Optional1 = Context.User.Identity.Name
        NewsmsarchiveItem.Optional2 = "1"


        NewsmsarchiveItem.MessageNo = messagecount.Value

        Try
            NewsmsarchiveItem.Addsmsarchive()
            lbResult.Text = "Message sent successfully"
            btnSend.Visible = False

        Catch ex As Exception
            lbResult.Text = ex.Message
        End Try

    Else

        lbResult.Text = "Not enough credit, please refill "

    End If
End Sub

btnSend如果您的其余代码没问题,这个sub 应该可以工作。注意你的线路。

Dim TotalNo As Integer = FilsPayComponent.ContactAddress.AddressContactsCount(ddlGroup.SelectedValue)

似乎什么也没做。如果您需要设置延迟,最好将其转换为向循环SendSMS返回已发送确认的函数。btnSend大多数短信 API 可以处理数字列表,而不是等待每条短信的响应。毕竟,他们只会在最后被添加到队列中。

于 2013-02-08T16:23:23.653 回答