0

最近、vb.netを使用してSMSプロバイダーAPIに接続しました

グループテーブルを作成し、このグループにすべての番号を挿入してから、各行に到達し、APIを送信して送信を処理します。SMSは、すべてのグループメンバーに届くわけではなく、グループ内の最初の携帯電話番号にのみ正常に配信されます。

この問題を解決する方法は?私は各送信の間に遅延を設定する必要があると思います、そして私は無駄にやりました。私のコードは以下の通りです:

  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() 次に、以下のコメントアウトされたコード (自分の電話番号 3 つを含む) を使用して、テスト用に設定します。

    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サブルーチンは機能するはずです。あなたの行に注意してください。

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

何もしていないようです。遅延を設定する必要がある場合は、送信された確認をループSendSMSに返す関数に変更することをお勧めします。btnSendほとんどのテキスト API は、各テキスト メッセージの応答を待つのではなく、番号のリストを処理できます。結局、それらは最後にキューに追加されるだけです。

于 2013-02-08T16:23:23.653 に答える