简体   繁体   English

有人可以解释通过 smsgateway.me api 发送短信时的错误吗?

[英]Can somebody explain the errors while sending sms through smsgateway.me api?

I am trying to post request through vb.net.我正在尝试通过 vb.net 发布请求。

Here is my code:这是我的代码:

Imports System.IO
Imports System.Net
Imports System.Text

Public Class TestSMS
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
     Try
            Dim webAddr As String = "https://smsgateway.me/api/v4/message/send"
            Dim httpWebRequest = CType(WebRequest.Create(webAddr), HttpWebRequest)
            httpWebRequest.ContentType = "application/json"
            httpWebRequest.Method = "POST"
            Dim header As WebHeaderCollection = New WebHeaderCollection()
            header.Add("Authorization", "** Key Hidden for security **")
            httpWebRequest.Headers = header
            Using streamWriter = New StreamWriter(httpWebRequest.GetRequestStream())
                Dim json As String = "{'phone_number':'123456789','message':'Hello World','device_id':116}"
                streamWriter.Write(json)
                streamWriter.Flush()
            End Using
            Dim httpResponse = CType(httpWebRequest.GetResponse(), HttpWebResponse)
            Using streamReader = New StreamReader(httpResponse.GetResponseStream())
                Dim responseText = streamReader.ReadToEnd()
                MsgBox(responseText)
            End Using
        Catch ex As WebException
            MsgBox(ex.Message)
        End Try
    End Sub
End Class

I got我有

400 Bad request error. 400 错误请求错误。

Here is an example of code https://smsgateway.me/sms-api-documentation/messages/sending-a-sms-message这是代码https://smsgateway.me/sms-api-documentation/messages/sending-a-sms-message的示例

but it is in PHP.但它在 PHP 中。

Here is the solution这是解决方案

Imports System.IO
Imports System.Net
Imports System.Text
Imports Newtonsoft.Json


Public Class TestSMS
    Public Class Messageparam
        Public Property phone_number As String
        Public Property message As String
        Public Property device_id As Integer
    End Class
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim messageParam As New Messageparam
        messageParam.phone_number = "123456789"
        messageParam.message = "Hello World!"
        messageParam.device_id = 116
        Dim webAddr As String = "https://smsgateway.me/api/v4/message/send"
        Dim httpWebRequest = CType(WebRequest.Create(webAddr), HttpWebRequest)
        httpWebRequest.ContentType = "application/json"
        httpWebRequest.Method = "POST"
        Dim header As WebHeaderCollection = New WebHeaderCollection()
        header.Add("Authorization", "Hidden API Key")
        httpWebRequest.Headers = header
        Using streamWriter = New StreamWriter(httpWebRequest.GetRequestStream())
            Dim json As String = "[" & JsonConvert.SerializeObject(messageParam) & "]"
            streamWriter.Write(json)
            streamWriter.Flush()
        End Using
        Dim httpResponse = CType(httpWebRequest.GetResponse(), HttpWebResponse)
        Using streamReader = New StreamReader(httpResponse.GetResponseStream())
            Dim responseText = streamReader.ReadToEnd()
        End Using
    End Sub
End Class

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

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