dulus
08-20-2008, 03:26 AM
I have VB6 application and need send file to https://somewhere.com/receive.php in same way as this html does:
<form enctype="multipart/form-data" name="formular" method="POST" action="https://somewhere.com/receive.php">
Login: <input type="text" name="login" value="scott">
Pass: <input type="text" name="pass" value="tiger">
File: <input name="userfile" type="file" size="40">
<input type="submit" value="Send File">
</form>
So question is how to do POST sending with two text fields (login, pass) and one file in multipart/form-data mode from VB6 app?
Uncle Gizmo
08-20-2008, 10:00 AM
Are you capable of building that string in your VB6 application?
dulus
08-21-2008, 04:41 AM
So far I read some documentation and samples, and made these functions to handle upload. Form data "login" and "pass" are passed to webpage, but I'm still unable to pass file. I'm not sure about that conversion of strBody to byte array before posting ? And also i'm not sure about "Content-Transfer-Encoding: binary" ?
Function for upload:
Function UploadXML(strFileName As String, Optional strUserName As String, Optional strPassword As String) As String
Const HTTPREQUEST_SETCREDENTIALS_FOR_SERVER = 0
Const HTTPREQUEST_SETCREDENTIALS_FOR_PROXY = 1
Dim WinHttpReq As WinHttp.WinHttpRequest
Dim strBody As String
Dim strFile As String
Dim aPostBody() As Byte
Dim bound As String
Dim boundSeparator As String
Dim boundFooter As String
bound = "AaB03x"
boundSeparator = "--" & bound & vbCrLf
boundFooter = "--" & bound & "--" & vbCrLf
Set WinHttpReq = New WinHttpRequest
WinHttpReq.Open "POST", "https://somewhere.com/receive.php", False
If strUserName <> "" And strPassword <> "" Then
WinHttpReq.SetCredentials strUserName, strPassword, HTTPREQUEST_SETCREDENTIALS_FOR_SERVER
End If
WinHttpReq.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & bound
strBody = boundSeparator
strBody = strBody & "Content-Disposition: form-data; name=""" & "login" & """" & vbCrLf & vbCrLf & "scott"
strBody = strBody & vbCrLf & boundSeparator
strBody = strBody & "Content-Disposition: form-data; name=""" & "pass" & """" & vbCrLf & vbCrLf & "tiger"
strBody = strBody & vbCrLf & boundSeparator
strFile = getFile("D:\path\" & strFileName)
strBody = strBody & "Content-Disposition: form-data; name=""" & "userfile" & """; filename=""" & strFileName & """ " & vbCrLf & _
"Content-Type: file" & vbCrLf & _
"Content-Transfer-Encoding: binary" & vbCrLf & vbCrLf & strFile & vbCrLf
strBody = strBody & boundFooter
'convert to byte array
aPostBody = StrConv(strBody, vbFromUnicode)
WinHttpReq.send aPostBody
UploadXML = WinHttpReq.responseText
Set WinHttpReq = Nothing
End Function
Function to load file to upload:
Function getFile(strFileName As String) As String
Dim strFile As String
Dim nFile
' Grap the file
nFile = FreeFile
Open strFileName For Binary As #nFile
strFile = String(LOF(nFile), " ")
Get #nFile, , strFile
Close #nFile
getFile = strFile
End Function
strBody contains something like this:
--AaB03x
Content-Disposition: form-data; name="login"
scott
--AaB03x
Content-Disposition: form-data; name="pass"
tiger
--AaB03x
Content-Disposition: form-data; name="userfile"; filename="sample.xml"
Content-Type: file
Content-Transfer-Encoding: binary
<?xml version="1.0" encoding="UTF-8"?>
<OE xsi:noNamespaceSchemaLocation="oe_request.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
..removed tags..
</OE>
--AaB03x--
dulus
08-25-2008, 03:58 AM
After sniffing http traffic mystery solved! There shouldn't be any spaces after filename="xxx" string, also binary transfer encoding is not needed. So here is working function:
Function UploadXML(strFileName As String, Optional strUserName As String, Optional strPassword As String) As String
Const HTTPREQUEST_SETCREDENTIALS_FOR_SERVER = 0
Const HTTPREQUEST_SETCREDENTIALS_FOR_PROXY = 1
Dim WinHttpReq As WinHttp.WinHttpRequest
Dim strBody As String
Dim strFile As String
Dim aPostBody() As Byte
Dim bound As String
Dim boundSeparator As String
Dim boundFooter As String
bound = "AaB03x"
boundSeparator = "--" & bound & vbCrLf
boundFooter = "--" & bound & "--" & vbCrLf
Set WinHttpReq = New WinHttpRequest
WinHttpReq.Open "POST", "https://somewhere.com/receive.php", False
If strUserName <> "" And strPassword <> "" Then
WinHttpReq.SetCredentials strUserName, strPassword, HTTPREQUEST_SETCREDENTIALS_FOR_SERVER
End If
WinHttpReq.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & bound
strBody = boundSeparator
strBody = strBody & "Content-Disposition: form-data; name=""" & "login" & """" & vbCrLf & vbCrLf & "scott"
strBody = strBody & vbCrLf & boundSeparator
strBody = strBody & "Content-Disposition: form-data; name=""" & "pass" & """" & vbCrLf & vbCrLf & "tiger"
strBody = strBody & vbCrLf & boundSeparator
strFile = getFile("D:\path\" & strFileName)
strBody = strBody & "Content-Disposition: form-data; name=""" & "userfile" & """; filename=""" & strFileName & """" & vbCrLf & _
"Content-Type: text/xml" & vbCrLf & vbCrLf & strFile & vbCrLf
strBody = strBody & boundFooter
'convert to byte array
aPostBody = StrConv(strBody, vbFromUnicode)
WinHttpReq.send aPostBody
Do Until WinHttpReq.status = 200
DoEvents
Loop
UploadXML = WinHttpReq.responseText
Set WinHttpReq = Nothing
End Function