asp.netvb.netinfobip

how to substitute fixed value t to variable name into request. addparameter statement


I need to change the sender and 238045366373 to variable strings such that that can assume different values ata different times

request.AddParameter("application/json", "{""messages"":[{""from"":""sender"",""to"":[""238045366373""],""text"":""May the God be with you Jolaoluwa!""}]}", ParameterType.RequestBody)

Solution

  • If you are sending to only a single number you could use this function.

    Private Function SendInfoBip(smsHeader As String, mobileNumber As String, message As String) As String
    
        Dim client = New WebClient()
    
        Dim message2 As String = HttpUtility.UrlEncode(message.Trim())
    
    
        client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 1.1.4322; .NET CLR 3.5.20404)")
    
        client.QueryString.Add("user", "YOUR_USERNAME") 'Replace YOUR_USERNAME with your actual username
    
        client.QueryString.Add("password", "YOUR_PASSWORD") 'Replace YOUR_PASSWORD with your actual password
    
        client.QueryString.Add("GSM", mobileNumber)
    
        client.QueryString.Add("SMSText", message2)
    
        client.QueryString.Add("sender", smsHeader)
    
        Dim baseurl As String = "http://api2.infobip.com/api/sendsms/plain"
    
        Dim s As String = client.DownloadString(baseurl)
    
    
        'Dim temp = s.Split(New String() {Environment.NewLine}, StringSplitOptions.None)
    
    
    
        'Dim flag As Boolean = Long.Parse(temp(0)) > 0
    
        'Return flag
    
        Return s
    
    End Function
    

    All you'll need to do is call SendInfoBip as shown below.

        Dim sender = "Scartag"
        Dim mobileNumber = "2348023061555"
        Dim message = "My brand new message"
        Dim result As Boolean = SendInfoBip(sender, mobileNumber, message)
    
        'result now holds the value you want to output to the screen
    

    UPDATE

    If you require the correct way of using variables instead of the hardcoded values as you requested early see below.

    Dim header = "Sender"
    Dim mobileNumber = "2348023061555"
    Dim mobileNumber2 = "23480230619085"
    Dim message = "Test message"
    
    request.AddParameter("application/json", "{""messages"":[{""from"":""" + header + """,""to"":[""" + mobileNumber + """, """ + mobileNumber2 + """ ],""text"":""" + message + """}]}", ParameterType.RequestBody)