【发布时间】:2018-03-01 02:23:34
【问题描述】:
我正在开发一个 vb.net 应用程序,它需要在应用程序中运行 SMS。 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
提供者 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"
【问题讨论】:
-
en.wikipedia.org/wiki/List_of_HTTP_status_codes#2xx_Success 201 = "请求已完成,导致创建新资源"
-
是,但未发布数据。根据文档。输出应为:5134842646923e183d000075 注意:输出将是请求 ID,它是字母数字,包含 24 个字符,如上所述。使用此请求 ID,可以查看交付报告。如果请求未成功发送,您将收到相应的错误消息