[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 728
  • Last Modified:

BinaryRead and inserting into SafeArray

Hi,
Here's my code:

     Set oXMLHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP.4.0")
     oXMLHTTP.Open "POST", "http://mysite.com/mypage.asp", False

     vntPostedData = Request.BinaryRead (Request.TotalBytes)
     oXMLHTTP.Send vntPostedData

I want to add an extra variable "Total" = "$100" to vntPostedData SafeArray before it gets posted to mypage.asp so that "Total" looks like any other Form variable.
Is this possible? Any ideas as to how I could late-add a Form post variable?
Thanks
manny
0
pwerstreak
Asked:
pwerstreak
1 Solution
 
sybeCommented:
Why do you use Request.BinaryRead ?? The only reason would be that it contains binary code (from fileupload).

If not, then just use
Request.Form & "&Total=" & Server.URLEncode($100)

Else you have to do something like:

Request.BinaryRead & StringToByte("&Total=" & Server.URLEncode($100))

Where StringToByte is a userdefined function to convert a string to a byte.

Simplest Function for that is:

Function String2Byte(s)
    Dim sChar, i
    For i = 1 to Len(s)
        sChar = Mid(s, i, 1)
        String2Byte = String2Byte & ChrB(Asc(sChar))
    Next
End Function

(But use it only for small strings, for large strings it is very slow, and there are faster functions then)
0
 
pwerstreakAuthor Commented:
xmlhttp was used so to post to asp pages in different servers and for later expansion, and for load balancing etc... I'm not at a liberty to change it.

oXMLHTTP.Send Request.BinaryRead & StringToByte("&Total=" & Server.URLEncode($100)) results in an error "type mismatch" as Send() expects an HTTPRequest (in the form of a safearray??)

manny
0
 
pwerstreakAuthor Commented:
xmlhttp was used so to post to asp pages in different servers and for later expansion, and for load balancing etc... I'm not at a liberty to change it.

oXMLHTTP.Send Request.BinaryRead & StringToByte("&Total=" & Server.URLEncode($100)) results in an error "type mismatch" as Send() expects an HTTPRequest (in the form of a safearray??)

manny
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
pwerstreakAuthor Commented:
xmlhttp was used so to post to asp pages in different servers and for later expansion, and for load balancing etc... I'm not at a liberty to change it.

oXMLHTTP.Send Request.BinaryRead & StringToByte("&Total=" & Server.URLEncode($100)) results in an error "type mismatch" as Send() expects an HTTPRequest (in the form of a safearray??)

manny
0
 
sybeCommented:
don't refresh this page: your comments get repeated !!

Now I remember a bit more:
If forms are posted with multipart/form-data, then you also need a seperator. The seperator is different everytime. You'll have to understand the mechanism quite well to add something to it.

0
 
pwerstreakAuthor Commented:
'----mypage1.asp---------

    Set oXMLHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP.4.0")
    oXMLHTTP.Open "POST", "http://mysite.com/mypage2.asp", False
    vntPostedData = Request.BinaryRead (Request.TotalBytes)
    oXMLHTTP.Send vntPostedData
'-------------------------
here again, I need to add one more variable to the Request in "mypage1" before I post to "mypage2" via xmlhttp... any way I could do this?

thanks
manjil
0
 
sybeCommented:
It would be something like this, i have not tested it, but basically this is how it works.


Set oXMLHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP.4.0")
oXMLHTTP.Open "POST", "http://mysite.com/mypage2.asp", False
vntPostedData = Request.BinaryRead (Request.TotalBytes)

iEndPos = InstrB(1,vntPostedData,String2Byte(CHR(13)))
sBoundary = Byte2String(MidB(vntPostedData,1,iEndPos-1))
sFieldname = "Total"
sValue = "$100"

btOldValues = LeftB(vntPostedData,LenB(vntPostedData)-4)
btNewValues = btOldValues & String2Byte(CHR(13) & CHR(10) & "Content-Disposition: form-data; name=""" &_
    sFieldname & """" & CHR(13) & CHR(10) & CHR(13) & CHR(10) & sValue & CHR(13) & CHR(10) &_
    sBoundary & "--" & CHR(13) & CHR(10))


oXMLHTTP.Send RSString2Byte(btNewValues)


Function RSString2Byte(ByVal s)
    Dim iLenString, oRS
    Set oRS = CreateObject("ADODB.Recordset")
    iLenString = LenB(s)
    If iLenString > 0 Then
        oRS.Fields.Append "mBinary", 205, iLenString
        oRS.Open
        oRS.AddNew
        oRS("mBinary").AppendChunk s & ChrB(0)
        oRS.Update
        RSString2Byte = oRS("mBinary").GetChunk(iLenString)
        oRS.Close
    End If
    Set oRS = Nothing
End Function

Function Byte2String(s)
    Dim i
    For i = 1 to LenB(s)
        Byte2String = Byte2String & CHR(AscB(MidB(s,i,1)))
    Next
End Function

Function String2Byte(s)
    Dim sChar, i
    For i = 1 to Len(s)
        sChar = Mid(s, i, 1)
        String2Byte = String2Byte & ChrB(AscB(sChar))
    Next
End Function


0
 
sybeCommented:
but now that I think of it. It would be a lot easier to add your extra values to the querystring:


sUrlToPostTo = "http://mysite.com/mypage2.asp?Total=" & Server.URLEncode("$100")

Set oXMLHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP.4.0")
oXMLHTTP.Open "POST", sUrlToPostTo, False
vntPostedData = Request.BinaryRead (Request.TotalBytes)
oXMLHTTP.Send vntPostedData

and then read the request.querystring on the receiving site
0
 
CleanupPingCommented:
pwerstreak:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
GaryCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept Answer by sybe

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
GaryC123
EE Cleanup Volunteer
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now