Connecting Tech Pros Worldwide Help | Site Map

Parsing MIME Response - Splitting base64 String into an Array

  #1  
Old June 16th, 2009, 03:38 PM
Newbie
 
Join Date: Jun 2009
Posts: 4
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.

Expand|Select|Wrap|Line Numbers
  1. 'boundary to split on
  2. Dim boundary as string = _
  3. "boundarystring"
  4.  
  5. 'read the HTTP response into byte array
  6. Dim byteArray() as Byte = memoryStream.ToArray()
  7.  
  8. 'convert the byte array into base64 string
  9. Dim base64String as String = _
  10. System.Convert.ToBase64String(byteArray, 0 byteArray.Length)
  11.  
  12. 'attempt to split base64 string by base64 version
  13. 'of boundary to retrieve individual photos
  14. Dim base64Array() as String = _
  15. base64String.Split(System.Convert.ToBase64String( _
  16. System.Convert.FromBase64String(boundary)))
  17.  
  18. 'then convert each base64 string back to image....
  19.  
  #2  
Old June 16th, 2009, 04:48 PM
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,690
Provided Answers: 3

re: Parsing MIME Response - Splitting base64 String into an Array


Quote:
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...
  #3  
Old June 16th, 2009, 09:09 PM
Newbie
 
Join Date: Jun 2009
Posts: 4

re: Parsing MIME Response - Splitting base64 String into an Array


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.
  #4  
Old June 16th, 2009, 11:11 PM
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,690
Provided Answers: 3

re: Parsing MIME Response - Splitting base64 String into an Array


Quote:
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
  #5  
Old June 17th, 2009, 03:07 PM
Newbie
 
Join Date: Jun 2009
Posts: 4

re: Parsing MIME Response - Splitting base64 String into an Array


Ok unfortunately it does appear as though it can't locate the boundary; as I am receiving a response of -1.
  #6  
Old June 17th, 2009, 04:33 PM
Newbie
 
Join Date: Jun 2009
Posts: 4

re: Parsing MIME Response - Splitting base64 String into an Array


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:
c2ltcGxlIGJvdW5kYXJ5AA==

Response:
DQotLXNpbXBsZSBib3VuZGFyeQ0KQ29udGVudC1JRDoyOTIzOD cxDQpPYmplY3QtSUQ6MQ0KQ29udGVudC1EZXNjcmlwdGlvbjoN CkNvbnRlbnQtVHlwZTppbWFnZS9qcGVnDQ==
------------------------------------------------------------------
Reply