Parsing MIME Response - Splitting base64 String into an Array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • smclellan86
    New Member
    • Jun 2009
    • 4

    Parsing MIME Response - Splitting base64 String into an Array

    Hi Everybody,

    I've been working on this challenge for a while now without much luck. What I'm attempting to do is split a MIME byte response into its individual images. I am given the boundary to split on in the HTTP header but when I attempt the following code it doesnt work. If anybody has some suggestions on what I should attempt here I would greatly appreciate it.

    Code:
    'boundary to split on
    Dim boundary as string = _
    "boundarystring"
    
    'read the HTTP response into byte array
    Dim byteArray() as Byte = memoryStream.ToArray()
    
    'convert the byte array into base64 string
    Dim base64String as String = _
    System.Convert.ToBase64String(byteArray, 0 byteArray.Length)
    
    'attempt to split base64 string by base64 version
    'of boundary to retrieve individual photos
    Dim base64Array() as String = _
    base64String.Split(System.Convert.ToBase64String( _
    System.Convert.FromBase64String(boundary)))
    
    'then convert each base64 string back to image....
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    but when I attempt the following code it doesnt work.
    Can you be more detailed about what 'doesn't work' means? Exceptions... first image is good, rest are junk... none are good...

    Comment

    • smclellan86
      New Member
      • Jun 2009
      • 4

      #3
      Sorry I suppose that might be helpful... The problem is when I attempt to split the base64 response with the base64 version of the boundary string it doesnt create an array. Its as though it cant locate the boundary within the response.

      In the past I have tried converting the byte array into a string then splitting on the boundary as is. This actually works but when I attempt to convert each string element of the array back to a byte array and save it as an image it ends up corrupt.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Its as though it cant locate the boundary within the response.
        Then the next step is to confirm your theory.
        Before actually doing the split do a find for the boundary. You should get an index of it's location. If you get a -1 then it didn't find it

        Comment

        • smclellan86
          New Member
          • Jun 2009
          • 4

          #5
          Ok unfortunately it does appear as though it can't locate the boundary; as I am receiving a response of -1.

          Comment

          • smclellan86
            New Member
            • Jun 2009
            • 4

            #6
            Here's an example of the boundary/response in string and base64 formats, I have only included the header portion of the response:

            String
            Boundary:
            --simple boundary

            Response:

            --simple boundary
            Content-ID:2923871
            Object-ID:1
            Content-Description:
            Content-Type:image/jpeg

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

            Base64
            Boundary:
            c2ltcGxlIGJvdW5 kYXJ5AA==

            Response:
            DQotLXNpbXBsZSB ib3VuZGFyeQ0KQ2 9udGVudC1JRDoyO TIzODcxDQpPYmpl Y3QtSUQ6MQ0KQ29 udGVudC1EZXNjcm lwdGlvbjoNCkNvb nRlbnQtVHlwZTpp bWFnZS9qcGVnDQ= =
            ------------------------------------------------------------------

            Comment

            Working...