简体   繁体   English

如何通过VB.NET发送短信

[英]how to Sending SMS through VB.NET

I tried to send sms我试着发短信
it was using textlocal.in它正在使用 textlocal.in
this is the code i tried这是我试过的代码

Public Function SendSms(sender As Object, e As EventArgs) Handles Button1.Click
    Dim apikey = txtAPI.Text
    Dim message = txtMsg.Text
    Dim numbers = txtNum.Text
    Dim strPOST As String
    Dim senderName = txtSend.Text
    Dim url As String = "https://api.textlocal.in/send/?"

    strPOST = url + "apikey=" + apikey _
    + "&numbers=" + numbers _
    + "&message=" + WebUtility.UrlEncode(message) _
    + "&sender=" + sender

    Dim request As WebRequest = WebRequest.Create(strPOST)
    request.Method = "POST"
    Dim byteArray As Byte() = Encoding.UTF8.GetBytes(strPOST)
    request.ContentType = "application/x-www-form-urlencoded"
    request.ContentLength = byteArray.Length
    Dim dataStream As Stream = request.GetRequestStream()
    dataStream.Write(byteArray, 0, byteArray.Length)
    dataStream.Close()

    Dim response As WebResponse = request.GetResponse()
    dataStream = response.GetResponseStream()
    Dim reader As New StreamReader(dataStream)
    Dim responseFromServer As String = reader.ReadToEnd()
    Console.WriteLine(responseFromServer)
    Console.ReadLine()

    reader.Close()
    dataStream.Close()
    response.Close()

    If responseFromServer.Length > 0 Then
        Return responseFromServer
    Else
        Return CType(response, HttpWebResponse).StatusDescription
    End If
End Function

it is saying Operator '+' is not defined for string " https://api.textlocal.in/send/?a " and type 'Button它是说没有为字符串“ https://api.textlocal.in/send/?a ”定义运算符“+”并键入“按钮”

In this code sender is the Button that raises the event:在此代码中, sender是引发事件的按钮:

Public Function SendSms(sender As Object
                        ^^^^^^

It is not the phone number of the person sending the message.它不是发送消息的人的电话号码。 Replace sender with senderName on this line:在这一行用senderName替换sender

strPOST = url + "apikey=" + apikey _
+ "&numbers=" + numbers _
+ "&message=" + WebUtility.UrlEncode(message) _
+ "&sender=" + sender
               ^^^^^^^

Do not use + to concatenate strings in vb unless you know exactly what it is doing不要在 vb 中使用+连接字符串,除非您确切地知道它在做什么

Use & to concatenate strings, but in this case I would recommend you use string formatting使用&连接字符串,但在这种情况下,我建议您使用字符串格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM