HttpWebRequest
-
Hi, How can i send a FILE with HttpWebRequest I opened a StreamWriter with GetRequestStream() But i have no ideia how to send a file :\ because in the page have a FileField ;\ Thanks.
Take a look at RFC 1867[^] which describes file uploads using HTTP. For example, if you only need to upload a single plain text file, you cn set the
HttpWebRequest.ContentType
to "multipart/form-data; boundary=ABC123". You then get the request stream usingHttpWebRequest.GetRequestStream
and, to make things easier, pass that to aStreamWriter
with whateverEncoding
is appropriate (should be the same as the file encoding you're uploading, though multiple parts can have different encodings that would go into the "Content-type" header as "charset". You would write to the request stream writer like so:--ABC123
Content-type: application/x-www-form-urlencoded
param1=value1¶m2=value2
--ABC123
Content-disposition: form-data; name="FileField"
Content-type: text/plain
This content is from a text document.
--ABC123--Notice the intentional line breaks and how the last line ends with -- (the end of the MIME boundary). Learning this is crucial to understanding how to HTTP and MIME works, and the RFC is actually quite easy to read (compared to many others I've read).
Microsoft MVP, Visual C# My Articles