-
July 23rd, 2010, 04:25 PM
#1
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.
-
July 23rd, 2010, 04:45 PM
#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.
-
July 23rd, 2010, 05:06 PM
#3
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.
-
July 23rd, 2010, 05:53 PM
#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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|