将json字符串作为post请求发送时,String参数错误

时间:2015-12-02 07:55:46

标签: apache vba excel-vba tomcat winhttp

我在excel中使用此代码作为VBA中的宏:

Sub Macro1()

Dim URL As String, JSONString As String, objHTTP As Object
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
URL = "xxxxx"
objHTTP.Open "POST", URL, False
objHTTP.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
JSONString = "{""name"": ""long"",""startDate"": 12345,""endDate"": 67890,""status"": ""in progress""}"
objHTTP.send JSONString
Debug.Print objHTTP.Status
Debug.Print objHTTP.responseText

End Sub

在即时窗口中,我收到了此错误:

  

400 Apache Tomcat / 8.0.28 - 错误   reportH1   {字体-family:宋体,Arial字体,无衬线;颜色:白色;背景色:#525D76;字体大小:的22px;}   H2   {字体-family:宋体,Arial字体,无衬线;颜色:白色;背景色:#525D76;字体大小:16px的;}   H3   {字体-family:宋体,Arial字体,无衬线;颜色:白色;背景色:#525D76;字体大小:14px的;}   身体   {字体-family:宋体,Arial字体,无衬线;颜色:黑色;背景色:白色;}   乙   {font-family:Tahoma,Arial,sans-serif;颜色:白色;背景颜色:#525D76;} P   {字体-family:宋体,Arial字体,无衬线;背景:白色;颜色:黑色;字体大小:12px的;}甲   {color:black;} A.name {color:black;}。line {height:1px;   background-color:#525D76; border:none;}   

HTTP状态400 - 必需的字符串参数' name'是   不存在

类型状态   报告

消息必填字符串参数' name'不是   现在

描述客户端发送的请求   语法不正确。

Apache   的Tomcat / 8.0.28

我不知道如何解决,请帮助我!

1 个答案:

答案 0 :(得分:0)

I solved this problem:

Sub Macro1()
Dim URL As String, JSONString As String, objHTTP As Object
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
URL = "xxxxx?name=long&startDate=12345&endDate=67890&status=in progress"
objHTTP.Open "POST", URL, False
objHTTP.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"

objHTTP.send
Debug.Print objHTTP.Status
Debug.Print objHTTP.responseText
End Sub