pass to the send() method the stream. But if it's multipart/form-data format, aside of the binary data the POST body should contain also boundaries and other metadata.
So, we have to store in a stream text data then binary then again some text.
It can be done by using the ADODB.Stream object.
It`s type (binary or text) can be converted dynamically, during the writing.
But! Before the setting the new type, the position has to be set to 0.
It can be then restored, after the type conversion to value taken from the stream.Size property.
option explicit
dim stream, file_loader, file_name, bnd
set stream = CreateObject("ADODB.Stream")
set file_loader = CreateObject("ADODB.Stream")
const adTypeBinary = 1
const adTypeText = 2
stream.Open
stream.Type = adTypeText
stream.CharSet = "iso-8859-1"
bnd
=
"boundary"
file_name = "test.png"
stream.WriteText( "--" & bnd & vbCrLf & _
"Content-Disposition: form-data; name=""the_file""; filename=""" & file_name & """" & vbCrLf & _
"Content-Type: application/octet-stream" & vbCrLf & vbCrLf )
stream.Position = 0
stream.Type = adTypeBinary
stream.Position = stream.Size
WScript.Echo "Position before file loading: " & stream.Position
file_loader.Open
file_loader.Type = adTypeBinary
file_loader.LoadFromFile(
file_name
)
file_loader.CopyTo stream
stream.Position = 0
stream.Type = adTypeText
stream.CharSet = "iso-8859-1"
stream.Position = stream.Size
WScript.Echo "Position after file loading: " & stream.Position
stream.WriteText( vbCrLf & "--" & bnd & "--" & vbCrLf )
stream.Position = 0
stream.Type = adTypeBinary
dim http_req
set http_req = CreateObject("WinHTTP.WinHTTPRequest.5.1")
'WinHTTP.WinHTTPRequest.5.1 better than Msxml2.XMLHTTP because it lets you override the http headers
call http_req.SetProxy( 2, "127.0.0.1:8888" ) ' for Fiddler proxy
call http_req.open( "POST", "http://127.0.0.1/", false )
call http_req.send( stream )
No comments:
Post a Comment