无法解析 XML:需要空格
Failed to parse XML: Whitespace expected
我有一个简单的 VBA 函数,可以将 POST 发送到远程服务器。 post 正文是一个 XML 文档。
Sub testmessage()
Dim sXML As String, sURL As String, sResponse As String
Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("MSXML2.ServerXMLHTTP")
sXML = "<?xml version=""1.0"" encoding=""ISO-8859-1""?>"
sXML = sXML & "<!DOCTYPE pnet_imessage_send PUBLIC ""-//PeopleNet//pnet_imessage_send"""
sXML = sXML & " ""http://open.peoplenetonline.com/dtd/pnet_imessage_send.dtd""> "
sXML = sXML & "<pnet_imessage_send>"
sXML = sXML & " <cid>20</cid>"
sXML = sXML & " <pw>password</pw>"
sXML = sXML & " <vehicle_number>123</vehicle_number>"
sXML = sXML & " <deliver>now</deliver>"
sXML = sXML & " <freeform_message>This is Tim's test message.</freeform_message>"
sXML = sXML & "</pnet_imessage_send>"
'Test url'
'sURL = "https://open.pfmlogin.com/scripts/postp.dll"
'Live url
sURL = "https://oi.pfmlogin.com/scripts/open.dll"
WinHttpReq.Open "POST", sURL, False
WinHttpReq.setRequestHeader "Content-Type", "application/x-www-form-urlencoded;Charset=ISO-8859-1"
WinHttpReq.Send sXML
sResponse = WinHttpReq.responseText
Debug.Print sResponse
End Sub
sXML 包含这个:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE pnet_imessage_send PUBLIC "-//PeopleNet//pnet_imessage_send"
"http://open.peoplenetonline.com/dtd/pnet_imessage_send.dtd">
<pnet_imessage_send>
<cid>20</cid>
<pw>password</pw>
<vehicle_number>123</vehicle_number>
<deliver>now</deliver>
<freeform_message>This is Tim's test message.</freeform_message>
</pnet_imessage_send>
如果我使用 Chrome 的 Postman plugin 发送它,它会毫无问题地处理。当 post 使用 VBA 代码时,它 returns 一个错误 XML failed to parse. The reported error was: File: . Line: 1 Col: 19 Error: Whitespace expected at Line: 1 Position: 19
。
如果我 post 测试 url 的数据(无论得到什么 posted 都会反弹),它显示如下:
<?xml version="1.0"
encoding="ISO-8859-1"
><!DOCTYPE pnet_imessage_send PUBLIC "-//PeopleNet//pnet_imessage_send"
http://open.peoplenetonline.com/dtd/pnet_imessage_send.dtd
我相信服务器没有将换行符视为白色space,导致XML解析器看起来像这样:<?xml version="1.0"encoding="ISO-8859-1">
(没有space在“1.0”和编码之间)。使用 Postman 发布的帖子没有意外的换行符。如何在不 VBA 分解序言的情况下 post 此数据?
此答案归功于@TimWilliams。找到他的 post 之一,如果对您有帮助,请给它 +1。
正如他和@Chillin 指出的那样,需要对表单数据进行 url 编码(或以保留空白的方式进行编码...HTML 编码也可能有效,但我没试过)。简单地将请求 header 设置为 application/xml
并没有保留空格。
我使用了此 SO post 中的以下代码:How can I URL encode a string in Excel VBA?(如果对您有帮助,也请给它 +1)
Public Function URLEncode( _
StringVal As String, _
Optional SpaceAsPlus As Boolean = False _
) As String
Dim StringLen As Long: StringLen = Len(StringVal)
If StringLen > 0 Then
ReDim result(StringLen) As String
Dim i As Long, CharCode As Integer
Dim Char As String, Space As String
If SpaceAsPlus Then Space = "+" Else Space = "%20"
For i = 1 To StringLen
Char = Mid$(StringVal, i, 1)
CharCode = Asc(Char)
Select Case CharCode
Case 97 To 122, 65 To 90, 48 To 57, 45, 46, 95, 126
result(i) = Char
Case 32
result(i) = Space
Case 0 To 15
result(i) = "%0" & Hex(CharCode)
Case Else
result(i) = "%" & Hex(CharCode)
End Select
Next i
URLEncode = Join(result, "")
End If
End Function
然后我在像这样连接我的 sXML 字符串后调用此函数:
sXML = sXML & " <freeform_message>This is Tim's test message.</freeform_message>"
sXML = sXML & "</pnet_imessage_send>"
sXML = "service=imessage_send&xml=" & URLEncode(sXML, False)
它就像一个魅力!
我有一个简单的 VBA 函数,可以将 POST 发送到远程服务器。 post 正文是一个 XML 文档。
Sub testmessage()
Dim sXML As String, sURL As String, sResponse As String
Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("MSXML2.ServerXMLHTTP")
sXML = "<?xml version=""1.0"" encoding=""ISO-8859-1""?>"
sXML = sXML & "<!DOCTYPE pnet_imessage_send PUBLIC ""-//PeopleNet//pnet_imessage_send"""
sXML = sXML & " ""http://open.peoplenetonline.com/dtd/pnet_imessage_send.dtd""> "
sXML = sXML & "<pnet_imessage_send>"
sXML = sXML & " <cid>20</cid>"
sXML = sXML & " <pw>password</pw>"
sXML = sXML & " <vehicle_number>123</vehicle_number>"
sXML = sXML & " <deliver>now</deliver>"
sXML = sXML & " <freeform_message>This is Tim's test message.</freeform_message>"
sXML = sXML & "</pnet_imessage_send>"
'Test url'
'sURL = "https://open.pfmlogin.com/scripts/postp.dll"
'Live url
sURL = "https://oi.pfmlogin.com/scripts/open.dll"
WinHttpReq.Open "POST", sURL, False
WinHttpReq.setRequestHeader "Content-Type", "application/x-www-form-urlencoded;Charset=ISO-8859-1"
WinHttpReq.Send sXML
sResponse = WinHttpReq.responseText
Debug.Print sResponse
End Sub
sXML 包含这个:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE pnet_imessage_send PUBLIC "-//PeopleNet//pnet_imessage_send"
"http://open.peoplenetonline.com/dtd/pnet_imessage_send.dtd">
<pnet_imessage_send>
<cid>20</cid>
<pw>password</pw>
<vehicle_number>123</vehicle_number>
<deliver>now</deliver>
<freeform_message>This is Tim's test message.</freeform_message>
</pnet_imessage_send>
如果我使用 Chrome 的 Postman plugin 发送它,它会毫无问题地处理。当 post 使用 VBA 代码时,它 returns 一个错误 XML failed to parse. The reported error was: File: . Line: 1 Col: 19 Error: Whitespace expected at Line: 1 Position: 19
。
如果我 post 测试 url 的数据(无论得到什么 posted 都会反弹),它显示如下:
<?xml version="1.0"
encoding="ISO-8859-1"
><!DOCTYPE pnet_imessage_send PUBLIC "-//PeopleNet//pnet_imessage_send"
http://open.peoplenetonline.com/dtd/pnet_imessage_send.dtd
我相信服务器没有将换行符视为白色space,导致XML解析器看起来像这样:<?xml version="1.0"encoding="ISO-8859-1">
(没有space在“1.0”和编码之间)。使用 Postman 发布的帖子没有意外的换行符。如何在不 VBA 分解序言的情况下 post 此数据?
此答案归功于@TimWilliams。找到他的 post 之一,如果对您有帮助,请给它 +1。
正如他和@Chillin 指出的那样,需要对表单数据进行 url 编码(或以保留空白的方式进行编码...HTML 编码也可能有效,但我没试过)。简单地将请求 header 设置为 application/xml
并没有保留空格。
我使用了此 SO post 中的以下代码:How can I URL encode a string in Excel VBA?(如果对您有帮助,也请给它 +1)
Public Function URLEncode( _
StringVal As String, _
Optional SpaceAsPlus As Boolean = False _
) As String
Dim StringLen As Long: StringLen = Len(StringVal)
If StringLen > 0 Then
ReDim result(StringLen) As String
Dim i As Long, CharCode As Integer
Dim Char As String, Space As String
If SpaceAsPlus Then Space = "+" Else Space = "%20"
For i = 1 To StringLen
Char = Mid$(StringVal, i, 1)
CharCode = Asc(Char)
Select Case CharCode
Case 97 To 122, 65 To 90, 48 To 57, 45, 46, 95, 126
result(i) = Char
Case 32
result(i) = Space
Case 0 To 15
result(i) = "%0" & Hex(CharCode)
Case Else
result(i) = "%" & Hex(CharCode)
End Select
Next i
URLEncode = Join(result, "")
End If
End Function
然后我在像这样连接我的 sXML 字符串后调用此函数:
sXML = sXML & " <freeform_message>This is Tim's test message.</freeform_message>"
sXML = sXML & "</pnet_imessage_send>"
sXML = "service=imessage_send&xml=" & URLEncode(sXML, False)
它就像一个魅力!