2013-02-07 79 views
0

發送大量短信到一羣我最近連接到SMS提供API使用vb.net用vb.net

我創建了一個組表,並插入到這個組中的所有數字,然後到達每一行和發送觸發的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 
+0

什麼行'Dim ExecArrayList As ArrayList ExecArrayList = SendSMS(toMobile,txtMsg,smstype)'do?看來你正在分配一個不返回值給數組列表 – SubtleStu

+0

你有什麼建議? – Dan

回答

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子應該工作,如果你的代碼的其餘部分沒問題。注意你的線路。

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

看起來沒有做任何事情。 如果您需要設置延遲,則最好將SendSMS轉換爲一個函數,該函數會將已發送的確認返回給您的btnSend循環。大多數短信API可以處理數字列表,而不是等待每個文本消息的響應。畢竟他們只會在最後加入隊列。