CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2002
    Posts
    212

    Parsing HTTP POST with StreamReader (multipart/form-data with file)

    I am attempting to parse an HTTP POST (from a NetworkStream) that is multipart/form-data with a file input field.

    The problem is that I need to parse the stream with a StreamReader in order to parse the header/boundary strings, but the file itself is raw binary data in the middle of this string. I cannot figure out how to mix StreamReader and raw memory access to write this file out to the disk, because the ASCII encoding/decoding is trashing information in the binary data.

    I need a way to mix text parsing with raw memory access. My best attempt is to copy the POST to a MemoryStream, use the stream reader, and remember the stream position when the boundarys are found. However, the StreamReader reads in 1024 byte increments, so this is not useful. I have not found a way to find the precise offset within the buffer that StreamReader is located.

    Any ideas on how to parse this data?

    I am using .NET 3.0 with Visual C# 2008.

  2. #2

    Re: Parsing HTTP POST with StreamReader (multipart/form-data with file)

    Code:
                using (BinaryReader breader = new BinaryReader(stream))
                {
                      ///find tags etc here
                    using (StringReader sreader = new StringReader(breader.ReadString()))
                    {
                        //Parse this as a string
                    }
                }
    --------------------------------------------------------------------------------------------------------------------------
    Disclaimer - Most likely any code I have posted as an answer was most likely written free hand and may have some minor compile errors, and is merely intended to give you the idea.

  3. #3
    Join Date
    Aug 2002
    Posts
    212

    Re: Parsing HTTP POST with StreamReader (multipart/form-data with file)

    That does not seem very useful because BinaryReader.ReadString needs a special format of string to know how many characters to read. This data is coming from a web client that I have no control over, I cannot rely on special formats like that.

  4. #4

    Re: Parsing HTTP POST with StreamReader (multipart/form-data with file)

    Then I suppose you are going to have to do it the hard way, by reading each character as a char and adding it into a StringBuilder. Then you can use the StringReader class.
    --------------------------------------------------------------------------------------------------------------------------
    Disclaimer - Most likely any code I have posted as an answer was most likely written free hand and may have some minor compile errors, and is merely intended to give you the idea.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured