HTTP Post通过Excel VBA提供帮助

时间:2015-07-20 14:41:12

标签: excel vba excel-vba http

感谢您提供的任何帮助!我似乎遇到了阻塞点。 我正在尝试将Excel电子表格中的数据字符串发送到给定的HTTP位置。最终我希望在字符串中发送实际数据字段,但是现在我只是硬编码才能让POC正常工作。

在研究了同一主题的其他帖子之后,我提出了下面的代码示例,但是在尝试建立连接时,我收到了一个" 400 Bad Request"网络错误。

有人能在我的语法中发现我做错了吗?

Sub HTTPpost()

Dim result As String
Dim myURL As String
Dim postData As String
Dim winHttpReq As Object

Set winHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")

myURL = "Insert URL Location"  
postData = "Insert Data String"

winHttpReq.Open "POST", myURL, False
winHttpReq.SetCredentials "Insert ID", "Insert Pwd", 0
winHttpReq.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
winHttpReq.Send (postData)

result = winHttpReq.ResponseText
Debug.Print vbCrLf & "Results:" & vbCrLf & result

End Sub

谢谢!!!

1 个答案:

答案 0 :(得分:0)

我能够弄清楚这一点。根据接收应用程序,UserID和Pwd需要在DATA字符串内,而不是单独的凭证。 所以最终的结果是这样的:

Sub HTTPpost()

Dim result As String
Dim myURL As String
Dim postData As String
Dim winHttpReq As Object

Set winHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")

myURL = "Insert URL Location"  
postData = "Insert ID & Insert Pwd & Insert Data String"

winHttpReq.Open "POST", myURL, False
winHttpReq.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
winHttpReq.Send (postData)

result = winHttpReq.ResponseText
Debug.Print vbCrLf & "Results:" & vbCrLf & result

End Sub