我在VB.net中使用HTTPListener来处理来自新Twilio SMS的初始联系。除了我收到检索错误外,一切正常。文档要求我发送回空的TwiML响应标记或200状态。我尝试了十几种不同的组合,包括:
Dim buffer = System.Text.Encoding.[Default].GetBytes("http://twimlets.com/echo?Twiml=%3CResponse%3E%3C%2FResponse%3E")
context.Response.OutputStream.Write(buffer, 0, buffer.Length)
context.Response.StatusCode = CInt(HttpStatusCode.OK)
context.Response.OutputStream.Close()
。
Dim buffer = System.Text.Encoding.[Default].GetBytes("<Response></Response")
context.Response.OutputStream.Write(buffer, 0, buffer.Length)
context.Response.StatusCode = CInt(HttpStatusCode.OK)
context.Response.OutputStream.Close()
Dim buffer = System.Text.Encoding.[Default].GetBytes("<?xml version="1.0" encoding="UTF-8" ?><Response></Response>")
context.Response.OutputStream.Write(buffer, 0, buffer.Length)
context.Response.StatusCode = CInt(HttpStatusCode.OK)
context.Response.OutputStream.Close()
我把我的FallbackURL的php代码用作.php文件,并将其作为字符串:
Dim buffer = System.Text.Encoding.[Default].GetBytes("<?php" & vbCrLf & "header(""content-type Text/ Xml"");" & vbCrLf & "echo ""<?xml version=\""1.0\"" encoding=\""UTF-8\""?>\n"";" & vbCrLf & "?>" & vbCrLf & "<Response></Response>")
context.Response.OutputStream.Write(buffer, 0, buffer.Length)
context.Response.StatusCode = CInt(HttpStatusCode.OK)
context.Response.OutputStream.Close()
我尝试使用我用于测试的实际HTML页面。所有这些都会产生收货失败错误。
在VB中确认收到回复的正确格式是什么?
以下是整个功能代码供参考:
Dim ws = New SimpleWebServer("http://*:8081/Orders/")
AddHandler ws.RequestReceived, Function(sender, context)
Dim Content As String = String.Empty
Dim request As HttpListenerRequest = context.Request
Dim body As Stream = request.InputStream
Dim encoding1 As System.Text.Encoding = request.ContentEncoding
Dim reader1 As New StreamReader(body, encoding1)
Content = reader1.ReadToEnd
Try
Dim TextParameterParse() As String = Content.Split("&")
Dim TextKeyValue As New Dictionary(Of String, String)
For Each Parameter As String In TextParameterParse
Dim KeyValueParse() As String = Parameter.Split("=")
TextKeyValue.Add(KeyValueParse(0), KeyValueParse(1))
Next
ActionIncoming(Long.Parse(HttpUtility.UrlDecode(TextKeyValue.Item("From"))), HttpUtility.UrlDecode(TextKeyValue.Item("Body")))
Catch ex As exception
End Try
'This is where the response code above is placed.
Return 1
End Function