我正在开发一个需要SMS功能到应用程序的vb.net应用程序。 SMS服务提供商API要求使用XML API发送大量消息。同样使用XML API,我们可以为每个号码定制不同的消息。
Sample XML format as per SMS Provider:
<MESSAGE>
<AUTHKEY>Authentication Key </AUTHKEY>
<SENDER>SenderID</SENDER>
<ROUTE>Template</ROUTE>
<CAMPAIGN>XML API</CAMPAIGN>
<COUNTRY>country code</COUNTRY>
<SMS TEXT="message1" >
<ADDRESS TO="number1"></ADDRESS>
</SMS>
<SMS TEXT="hi test message" >
<ADDRESS TO="number2"></ADDRESS>
</SMS>
</MESSAGE>
Post your request with above format in data variable.
http://api.msg91.com/api/postsms.php
Provider dnt有VB.NET的任何示例代码所以经过大量搜索后终于得到了一些关于在VB中使用HttpWebRequest的信息。并提出代码,但输出“代码:201”
Imports System.Data
Imports System.Data.OleDb
Imports System.Globalization
Imports System.Text
Imports System.IO
Imports System.Net
Imports System.Web
Imports System.Xml
Dim authKey As String
Dim mobile As String
Dim senderId As String
Dim route As String
Dim URLXML As String = "http://api.msg91.com/api/postsms.php?data="
'Set these variables
authKey = "XXXXXXXXXXX"
mobile = String.Empty
'Sender ID, While using route4 sender id should be 6 characters long.
senderId = "XXXXXX"
'Define route
route = "X"
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
If (DataGridView2.Rows.Count > 0) Then
Dim xml As System.Text.StringBuilder = New System.Text.StringBuilder()
xml.Append("<MESSAGE>" & Environment.NewLine)
xml.Append("<AUTHKEY>" & authKey & "</AUTHKEY>" & Environment.NewLine)
xml.Append("<SENDER>" & senderId.ToString & "</SENDER>" & Environment.NewLine)
xml.Append("<ROUTE>" & route.ToString & "</ROUTE>" & Environment.NewLine)
xml.Append("<COUNTRY>91</COUNTRY>" & Environment.NewLine)
'MOBILE & MESSAGE FIELDS LOADED FROM DATAGRIDVIEW ROWS
For i As Integer = 0 To DataGridView2.Rows.Count - 1
xml.Append("<SMS TEXT='" & URLEncode(DataGridView2.Rows(i).Cells("MESSAGE").Value.ToString) & "'>" & Environment.NewLine)
xml.Append("<ADDRESS TO='" & DataGridView2.Rows(i).Cells("MOBILE").Value.ToString & "'></ADDRESS>")
xml.Append("</SMS>" & Environment.NewLine)
Next
xml.Append("</MESSAGE>")
'URLEncode Whole input String as told by the SMS Provider
Dim xmlData As String = URLEncode(xml.ToString)
' Create POST data and convert it to a byte array.
Dim encoding As New UTF8Encoding
Dim bytes As Byte() = encoding.GetBytes(xmlData)
Try
Dim req As HttpWebRequest = DirectCast(WebRequest.Create(URLXML), HttpWebRequest)
req.Method = "POST"
' Set the ContentType property of the WebRequest.
req.ContentType = "application/x-www-form-urlencoded"
' Set the ContentLength property of the WebRequest.
req.ContentLength = bytes.Length
' Get the request stream.
Using dataStream As Stream = req.GetRequestStream()
dataStream.Write(bytes, 0, bytes.Length)
End Using
' Get the response.
'Dim response As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
Dim response As HttpWebResponse = req.GetResponse()
If (response.StatusCode = HttpStatusCode.OK) Then
' Display the status.
' Get the stream containing content returned by the server.
Dim dStream As Stream = response.GetResponseStream()
' Open the stream using a StreamReader for easy access.
Dim reader As New StreamReader(dStream, True)
' Read the content
Dim responseFromServer As String = reader.ReadToEnd()
' Display the content.
MsgBox(responseFromServer.ToString)
reader.Close()
dStream.Close()
End If
' Clean up & close
response.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End If
End Sub
Public Function URLEncode(ByVal Text As String) As String
Dim i As Integer
Dim acode As Integer
'Dim chars As String
URLEncode = Text
For i = Len(URLEncode) To 1 Step -1
acode = Asc(Mid$(URLEncode, i, 1))
Select Case acode
Case 10
'replace line break to "0A"
Mid$(URLEncode, i, 1) = "0A"
Case 47 To 57, 65 To 90, 97 To 122
' don't touch alphanumeric chars
Case 32
' replace space with "+"
Mid$(URLEncode, i, 1) = "+"
Case Else
' replace punctuation chars with "%hex"
URLEncode = Left$(URLEncode, i - 1) & "%" & Hex$(acode) & Mid$ _
(URLEncode, i + 1)
End Select
Next
End Function
出现第一次运行错误消息:
System.Net.WebException: The remote name could not be resolved": 'api.msg91.com' at System.Net.http.WebRequest.GetRequestStream
&第二次输出是代码:201 1错误消息也出现在即时窗口中“System.dll中出现'System.Net.WebException'类型的第一次机会异常”。
由于SMS提供商dnt有任何VB.NET代码示例,他们发给我这个link。然后我根据代码做了一些更改:
1)整个XML字符串上没有URLENCODE 2)更改内容类型:text / plain 3)添加了req.timeout 4)使用StreamWriter代替流。
Dim req As HttpWebRequest = WebRequest.Create(URLXML)
req.Method = WebRequestMethods.Http.Post
' Set the ContentType property of the WebRequest.
'req.ContentType = "application/x-www-form-urlencoded"
req.ContentType = "text/plain"
' Set the ContentLength property of the WebRequest.
req.ContentLength = xml.Length
req.Timeout = 1000000
' Get the request stream.
Dim sw As New StreamWriter(req.GetRequestStream)
sw.Write(xml.ToString)
sw.Close()
使用此代码没有WebException或不可避免的窗口错误,但得到相同的输出代码:201。根据文件。输出应为:5134842646923e183d000075。输出将是请求ID,它是字母数字,包含24个字符,如上所述。使用此请求ID,可以查看交付报告。如果请求未成功发送,您将收到相应的错误消息
用于向所有用户发送相同文本消息的提供者的other method正在工作,但是使用循环运行代码需要大量时间,如果我们必须发送大量请求,则应使用XML API。无法理解XML方法未发布数据的原因。什么是代码中的错误或错误,请帮助/指导我纠正。谢谢。
编辑:还尝试更改内容类型属性和ASCIIEncoding但相同的输出:
req.ContentType = "application/x-www-form-urlencoded"
在我尝试使用以下代码获得Succesful的相同问题一天后,我只使用了msg91服务
Imports System
Imports System.IO
Imports System.Net
Imports System.Text
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Create a request using a URL that can receive a post.
Dim request As WebRequest = WebRequest.Create("http://sms.orreryhim.com/api/postsms.php")
' Set the Method property of the request to POST.
request.Method = "POST"
' Create POST data and convert it to a byte array.
Dim postData As String = "<MESSAGE>
<AUTHKEY>your authkey here</AUTHKEY>
<ROUTE>route code</ROUTE>
<CAMPAIGN>campaign mode</CAMPAIGN>
<COUNTRY>country code</COUNTRY>
<SENDER>sender id</SENDER>
<SMS TEXT='"messges'">
<ADDRESS TO="'mobile number'"></ADDRESS>
</SMS>
</MESSAGE>"
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
' Set the ContentType property of the WebRequest.
request.ContentType = "application/xml"
' Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length
' Get the request stream.
Dim dataStream As Stream = request.GetRequestStream()
' Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length)
' Close the Stream object.
dataStream.Close()
' Get the response.
Dim response As WebResponse = request.GetResponse()
' Display the status.
'Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
Label1.Text = CType(response, HttpWebResponse).StatusDescription
' Get the stream containing content returned by the server.
dataStream = response.GetResponseStream()
' Open the stream using a StreamReader for easy access.
Dim reader As New StreamReader(dataStream)
' Read the content.
Dim responseFromServer As String = reader.ReadToEnd()
' Display the content.
Console.WriteLine(responseFromServer)
Label2.Text = responseFromServer
' Clean up the streams.
reader.Close()
dataStream.Close()
response.Close()
End Sub
End Class