473,388 Members | 1,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

winhttprequest posting byte() and string in multipart message. Howto

Hi,

I am near to desperation as I have a million things to get a solution
for my problem. I have to post a multipart message to a url that
consists of a xml file and an binary file (pdf). Seperately the
posting words fine but when I want to create one multipart message
with both then things go wrong.

The binary file is converted and of datatype byte()
The xml file is just a string.

I don't know how to merge these two into the multipart message. I have
tried converting the binary to string and then concatenate but that
doesn't work as it seems to leave off the last part of the file and
the boundary.

Any help or pointer would be greatly appreciated.
I am using classic asp.

Thanks.

Fnoppie

Jun 8 '07 #1
6 10519
fn*****@gmail.com wrote:
Hi,

I am near to desperation as I have a million things to get a solution
for my problem. I have to post a multipart message to a url that
consists of a xml file and an binary file (pdf).
Huh? The url consists of a "xml file ..."?
Oh wait. I get it. The multipart message contains those things.
Carry on.
Seperately the
posting words fine but when I want to create one multipart message
with both then things go wrong.

The binary file is converted and of datatype byte()
The xml file is just a string.

I don't know how to merge these two into the multipart message. I have
tried converting the binary to string and then concatenate but that
doesn't work as it seems to leave off the last part of the file and
the boundary.
Show us what you tried so we can see where you went wrong.
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jun 8 '07 #2
Hello Bob,

Below is a condensed version of the source code.

Posting 'FormDataCv' as the send parameter works fine. Posting
'FormDataXml' as send parameter works fine as well. The problem is
pasting them together. Running the code below will generates a 'data
stream is interrupted unexpectedly' because of the Word file not
getting the completely with boundary and all.

Thanks for your time in advance.

Fnoppie
<%
Dim baseUrl
Dim loginUrl
Dim userName
Dim passWord
Dim account
Dim processUrl

Const WinHttpRequestOption_EnableRedirects = 6
account = "account"
userName = "username"
passWord = "password"
baseUrl = "some/url"
loginUrl = "some/url"
processUrl = "some/url"

Function BinaryToString(Binary)
'Antonin Foller, http://www.motobit.com
'Optimized version of a simple BinaryToString algorithm.

Dim cl1, cl2, cl3, pl1, pl2, pl3
Dim L
cl1 = 1
cl2 = 1
cl3 = 1
L = LenB(Binary)

Do While cl1<=L
pl3 = pl3 & Chr(AscB(MidB(Binary,cl1,1)))
cl1 = cl1 + 1
cl3 = cl3 + 1
If cl3>300 Then
pl2 = pl2 & pl3
pl3 = ""
cl3 = 1
cl2 = cl2 + 1
If cl2>200 Then
pl1 = pl1 & pl2
pl2 = ""
cl2 = 1
End If
End If
Loop
BinaryToString = pl1 & pl2 & pl3
End Function

'---------------------------------------------------------------------

Function CreateTrxml()
strXml = "<?xml version=""1.0"" encoding=""ISO-8859-1""?>"
strXml = strXml & "<Result user=""1"" xmlns='http://
www.someurl.nl'>"
strXml = strXml & "<Document lang=""dutch""/>"
strXml = strXml & "<DocumentStructure>"
strXml = strXml & "<ItemGroup count=""1"" key=""lastname"">"
strXml = strXml & "<Item index=""0"">"
strXml = strXml & "<Field key=""lastname"">"
strXml = strXml & "<Value>opberg</Value>"
strXml = strXml & "</Field>"
strXml = strXml & "</Item>"
strXml = strXml & "</ItemGroup>"
strXml = strXml & "</DocumentStructure>"
strXml = strXml & "</Result>"
CreateTrxml = strXml
End Function

'---------------------------------------------------------------------------
Sub do_vbsUpload(FileName)
DestURL= processUrl
FieldName = "uplfile"

UploadFile DestURL, FileName, FieldName
End Sub

'Upload file using input type=file
Sub UploadFile(DestURL, FileName, FieldName)
'Boundary of fields.
'Be sure this string is Not In the source file
Const Boundary = "0123456789012"

Dim FileContents, FormData
'Get source file As a binary data.
FileContents = GetFile(FileName)

'Build multipart/form-data document
FormDataCv = BuildFormData(FileContents, Boundary, FileName,
FieldName)
FormDataXml = BuildFormDataXml(CreateTrxml(), Boundary,
"trxml.xml", "trxml")
FormData = FormDataXml + cstr(FormDataCv)
'Post the data To the destination URL
IEPostBinaryRequest DestURL, FormData, Boundary
End Sub

'Build multipart/form-data document with file contents And header
info
Function BuildFormData(FileContents, Boundary, FileName, FieldName)
Dim FormData, Pre, Po
Const ContentType = "application/msword"

'The two parts around file contents In the multipart-form data.
'Pre = "--" + Boundary + vbCrLf + mpFields(FieldName, FileName,
ContentType)
Pre = mpFields(FieldName, FileName, ContentType)
Po = vbCrLf + "--" + Boundary + "--" + vbCrLf

'Build form data using recordset binary field
Const adLongVarBinary = 205
Dim RS: Set RS = CreateObject("ADODB.Recordset")
RS.Fields.Append "b", adLongVarBinary, Len(Pre) + LenB(FileContents)
+ Len(Po)
RS.Open
RS.AddNew
Dim LenData
'Convert Pre string value To a binary data
LenData = Len(Pre)
RS("b").AppendChunk (StringToMB(Pre) & ChrB(0))
Pre = RS("b").GetChunk(LenData)
RS("b") = ""

'Convert Po string value To a binary data
LenData = Len(Po)
RS("b").AppendChunk (StringToMB(Po) & ChrB(0))
Po = RS("b").GetChunk(LenData)
RS("b") = ""

'Join Pre + FileContents + Po binary data
RS("b").AppendChunk (Pre)
RS("b").AppendChunk (FileContents)
RS("b").AppendChunk (Po)
RS.Update
FormData = RS("b")
RS.Close
BuildFormData = FormData
End Function

Function BuildFormDataxml(FileContents, Boundary, FileName,
FieldName)
'response.write filecontents
Dim FormData, Pre, Po
Const ContentType = "text/xml"

'The two parts around file contents In the multipart-form data.
Pre = "--" + Boundary + vbCrLf + mpFields(FieldName, FileName,
ContentType)
Po = vbCrLf + "--" + Boundary + vbCrLf

FormData = Pre & FileContents & Po

BuildFormDataxml = FormData
End Function

'sends multipart/form-data To the URL
Function IEPostBinaryRequest(URL, FormData, Boundary)
set req = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
req.option(WinHttpRequestOption_EnableRedirects) = False
response.write "<p>url: " & URL & "</p>"
req.open "POST", URL, False
req.setRequestHeader "Content-Type", "multipart/form-data;
boundary=" & boundary
req.send FormData
response.write "<h1>req.getAllResponseHeaders: " &
req.getAllResponseHeaders() & "</h1>"
response.write "<h1>status: " & req.status & "</h1>"
response.write "<h1>statustext: " & req.statusText & "</h1>"
response.write req.responseText
End Function

'Information In form field header.
Function mpFields(FieldName, FileName, ContentType)
Dim MPTemplate 'template For multipart header
MPTemplate = "Content-Disposition: form-data; name=""{field}"";" + _
" filename=""{file}""" + vbCrLf + _
"Content-Type: {ct}" + vbCrLf + vbCrLf
Dim Out
Out = Replace(MPTemplate, "{field}", FieldName)
Out = Replace(Out, "{file}", FileName)
mpFields = Replace(Out, "{ct}", ContentType)
End Function

'Makes File Binary
Function GetFile(FileName)
Dim Stream: Set Stream = CreateObject("ADODB.Stream")
Stream.Type = 1 'Binary
Stream.Open
Stream.LoadFromFile FileName
GetFile = Stream.Read
Stream.Close
End Function

'Converts OLE string To multibyte string
Function StringToMB(S)
Dim I, B
For I = 1 To Len(S)
B = B & ChrB(Asc(Mid(S, I, 1)))
Next
StringToMB = B
End Function

do_vbsUpload upl.form("file")

%>

Jun 8 '07 #3
Anyone, please? Any minor tip would be greatly appreciated.

Thanks a lot.

Fnoppie.

Jun 9 '07 #4

<fn*****@gmail.comwrote in message
news:11**********************@h2g2000hsg.googlegro ups.com...
Anyone, please? Any minor tip would be greatly appreciated.

Thanks a lot.

Fnoppie.
The boundaries don't look right to me. Since each BuildFormDataXXX
functions adds the boundary twice. There should only be one boundary per
part with a final boundary having the suffix --<CR><LF><CR><LF>

In cases such as this I find the fiddler tool invaluable.
www.fiddlertool.com (its free). Its a debugging proxy which allows you to
see the HTTP traffic.

It seems you are trying to emulate what happens when a HTML Form is posted
when that form contains two file inputs.

Build such a HTML form then with fiddler capturing use it in IE to post an
XML and a PDF file.

You can then use fiddler on the machine where your ASP is running (not a
production machine mind you). Now compare the headers and encoding used by
the IE post with what you code is producing and tweak you code accordingly.

Note for fiddler to trap WinHTTP traffic you will need the command

proxycfg -u

after fiddler is capturing and before you terminate capture in fiddler use:-

proxycfg. -d


Jun 10 '07 #5
Hello Anthony,

Thanks for your response. I have tried fiddler and it works like a
charm.

However, I see that when I try to send the xml and binary file at the
same time, that the xml file is put in the multipart file just fine.
The binary file however is truncated when I convert it to a string
with the binarytostring function. That is why the binary file is never
posted to the remote url.

This is part of the fiddler log. As you can see that the binary file
only consists of a question mark. If I send the binary file alone in
the send paramater than I see the complete binary file with boundaries
and all.

-----------------------------------------------------

Content-Disposition: form-data; name="xml"; filename="xml.xml"
Content-Type: text/xml

<?xml version="1.0" encoding="ISO-8859-1"?><TextractorResult user="1"
xmlns='someurl'><Document lang="dutch"/><DocumentStructure><ItemGroup
count="1" key="lastname"><Item index="0"><Field
key="lastname"><Value>lastname</Value></Field></Item></
ItemGroup><ItemGroup count="1" key="givenname"><Item index="0"><Field
key="givenname"><Value>Firstname</Value></Field></Item></ItemGroup></
DocumentStructure></TextractorResult>
--0123456789012
Content-Disposition: form-data; name="uploaded_file"; filename="f:\data
\cv\textkernel\sa753.tmp"
Content-Type: application/msword

 

-------------------------------------

Can anybody help me out with this problem?

Thanks a lot.

Pascal

Jun 11 '07 #6
>>>>>>>>>>>>>>
<fn*****@gmail.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
Hello Anthony,

Thanks for your response. I have tried fiddler and it works like a
charm.

However, I see that when I try to send the xml and binary file at the
same time, that the xml file is put in the multipart file just fine.
The binary file however is truncated when I convert it to a string
with the binarytostring function. That is why the binary file is never
posted to the remote url.

This is part of the fiddler log. As you can see that the binary file
only consists of a question mark. If I send the binary file alone in
the send paramater than I see the complete binary file with boundaries
and all.

-----------------------------------------------------

Content-Disposition: form-data; name="xml"; filename="xml.xml"
Content-Type: text/xml

<?xml version="1.0" encoding="ISO-8859-1"?><TextractorResult user="1"
xmlns='someurl'><Document lang="dutch"/><DocumentStructure><ItemGroup
count="1" key="lastname"><Item index="0"><Field
key="lastname"><Value>lastname</Value></Field></Item></
ItemGroup><ItemGroup count="1" key="givenname"><Item index="0"><Field
key="givenname"><Value>Firstname</Value></Field></Item></ItemGroup></
DocumentStructure></TextractorResult>
--0123456789012
Content-Disposition: form-data; name="uploaded_file"; filename="f:\data
\cv\textkernel\sa753.tmp"
Content-Type: application/msword

 

-------------------------------------

Can anybody help me out with this problem?

Thanks a lot.

Pascal
<<<<<<<<<<<<<<<<

Fiddler doesn't show binary content well in Raw view. Did you try Hex view?
I think you will find the content is probably there.

Is the above the complete Raw request? If not can you post it but cut the
actual content itself (just leave a small place marker for it)?

Jun 11 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Francis | last post by:
I have the following codes in an asp file. This page can receive the XML string send from client. ================================================== Dim objReceive Set objReceive =...
4
by: DraguVaso | last post by:
Hi, In my application I receive a Byte Stream (Dim bytFile() As Byte) which contains a jpeg-picture, which I want to display in a picturebox. I want to display it directly from the bytfile()...
1
by: Joey Chömpff | last post by:
Hello, When I'm uploading an file to a JRun WebServer (third party) with a Windows Forms application, l always get an TimeOut while uploading, all other request who doesn't request an post are...
3
by: Manuel | last post by:
I have an asp page ("test.asp") that presents the data it receives from a post.When I try the following code, test.asp doesn't return the values (supposedly) posted to it. If I make a web page with...
7
by: Mark Waser | last post by:
Hi all, I'm trying to post multipart/form-data to a web page but seem to have run into a wall. I'm familiar with RFC 1867 and have done this before (with AOLServer and Tcl) but just can't seem...
0
by: Jerry T. | last post by:
I have the following code (using VB.NET 2003 with Framework 1.1): Protected Shared Sub RetrieveURLData() Dim HttpReq As WinHttp.WinHttpRequest Dim strText As String Try ' Create the...
0
by: Solius | last post by:
I have been struggling for 4 days to write a connection to an XML REST API. I can't figure out what is wrong with my code that it won't connect propertly. The goal is to make a web service that...
1
by: Praveen | last post by:
got a form in html <form style="display: inline" enctype="multipart/form-data"> <input name='inputXML' > assinging an xml string to inputXML and posting the form but when trying to access the...
1
by: starter08 | last post by:
Hi, I have a C++ routine(client-side) which uploads an xml file to a web server by making a socket connection and sending all the post request through that socket. On the server side I have a cgi...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.