0

我可以通过调用以下代码轻松完成 GET:

Dim theURL As String, theRequest As Object
theURL = "http://localhost/myapp/encrypt.jsp?str=" & _
    encodeStr(range("I10").Value) & "&user=myUser&pass=myPass"
Set theRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
theRequest.Open "GET", theURL 
theRequest.Send
MsgBox theRequest.ResponseText

但是,我在发布帖子时遇到了更多麻烦。我试图模拟的表单的 html 是:

<form method="post" enctype="multipart/form-data" action="managefile?operation=upload">
  <table>
    <tr>
        <h3 align="center">Upload File</h3>
    </tr>
    <tr>
        <td>File:</td>
        <td><input type="file" size="80" name="fname"></td>
        <td><input type="Submit" value="Upload"></td>
    </tr>
  </table>
</form>

get API 通常期望用户名和密码作为参数传递。由于我认为我不能传递带有参数的 POST,因此我尝试使用我在各个页面(包括此处)上找到的技术发送带有按边界分割的数据的帖子。这些都没有奏效。

这是我尝试过的一些代码的示例,但它不起作用:

Dim theURL As String, theRequest As Object, theBody As String, theBoundary As String
theBoundary = "-----------------13850284693" ' Sample, generating from timestamp        
theBody = theBoundary & vbCrLf
theBody = theBody & "Content-Disposition: form-data" & vbCrLf & vbCrLf
theBody = theBody & "user=myUser&pass=MyPass" & vbCrLf ' I am wondering if this also needs to include action
theBody = theBody & theBoundary & vbCrLf
theBody = theBody & "Content-Disposition: form-data; name=""fname""; filename=""update.xml""" & vbCrLf
theBody = theBody & "Content-Type: text/xml" & vbCrLf & vbCrLf
theBody = theBody & "<MyXML><Node1>ff</Node1></MyXML>"
theBody = theBody & vbCrLf
theBody = theBody & theBoundary & "--" & vbCrLf

theURL = "http://localhost/myapp/managefile?operation=upload"
Set theRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
theRequest.Open "POST", theURL, False
theRequest.setRequestHeader "Cache-Control", "no-cache"
theRequest.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & theBoundary
theRequest.setRequestHeader "Content-Length", Len(theBody)    
theRequest.Send theBody

我从这个例子返回的页面是我没有登录,所以它似乎没有正确传递我的参数。

我还尝试将用户名和密码作为 URL 的一部分传递,例如

theURL = "http://localhost/myapp/managefile?operation=upload&user=myUser&pass=myPass"

从这里我得到一个错误,说它(API)期望

<MyXML>...</MyXML>

它得到:

-----------------13850284693..(continues)..<MyXML><Node1>ff</Node1></MyXML>.

这使我相信它可以正常登录,但不能正确发布帖子。

我尝试做同样的事情,只是发送 XML,但这也没有用。这对我来说没有意义,因为 API 不知道将 XML 关联到哪个参数。

我尝试的最后一件事是运行一个简单的 get,它让用户登录并返回 cookie,然后将其传回。这似乎与上述相同,但 POST 仍然存在问题。

这是我尝试设置cookie。它似乎与我传递参数时的工作方式相同——看起来登录有效,但无法正确发布(特别是我尝试发送的 xml):

Dim theLoginRequest, thePostRequest
Set theLoginRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
theLoginRequest.Open "GET", "localhost/myapp/login?user=myUser&pass=myPass"
theLoginRequest.Send
Set thePostRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
thePostRequest.Open "POST, "<post request>", False
thePostRequest.setRequestHeader "Cookie", theLoginRequest.getResponseHeader("Set-Cookie")

...ETC。


我在那里的字符串与我可以毫无问题地上传到表单的文件内容相同。

有任何想法吗?

4

1 回答 1

0

该站点使用基于 cookie 的登录。因此,您需要使用 GET 登录,获取返回的 cookie,然后在 POST 请求的标头中包含相同的 cookie。

于 2014-12-09T01:43:44.963 回答