.NET Framework Bookmark and Share   
 index > Network Class Library (System.Net) > Uploading files via HTTPWebRequest class
 

Uploading files via HTTPWebRequest class

I am using HttpWebRequest class in a desktop app for communication with a website. I need to upload file to a page. Is it possible to do it with this class??
  • Moved bynobugzMVPTuesday, February 03, 2009 9:28 AMsys.net q (Moved from .NET Base Class Library to Network Class Library (System.Net))
  •  
Rahul Singla

What you probably want is to upload files the same way as would a web browser. This protocol is actually defined not in the HTTP standard, but in the HTML one. SeeRFC 1867 for details. This protocol is built on top of HTTP POST, which is provided by the HttpWebRequest class. However, you will need to handle the details of multipart-encoding the request body yourself. The HTTP protocol simply considers the POST body to be an arbitrary stream of bytes which the application can use however it would like.

-dave

Dave Murray [NCL]
Yes it is possible. You can set the Method Property to "POST". You can take a look at the example on:
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.method.aspx
Jovana Florus
The example on that page demonstrates sending data in body via Post requests. That can easily be adapted to send the contents of a file in the Post request.

However, browsers are able to send multiple files together with form data in a single post request that servers can read independently of the platform. The browsers obviously use some demarker between different files, and between the files & form data. I believe that is a part of the HTTP standard, as servers can parse the data on any platform.

So, how do I do it in .NET? I could not find any other class explicitly for HTTP requests, and this class does not provide properties or methods to send multiple files?
Rahul Singla
I am pretty sure you should be able to. Hold on, I'll send your questiontoour triage alias so the experts in the area can answer

Mariya
Mariya Atanasova [NCL]

What you probably want is to upload files the same way as would a web browser. This protocol is actually defined not in the HTTP standard, but in the HTML one. SeeRFC 1867 for details. This protocol is built on top of HTTP POST, which is provided by the HttpWebRequest class. However, you will need to handle the details of multipart-encoding the request body yourself. The HTTP protocol simply considers the POST body to be an arbitrary stream of bytes which the application can use however it would like.

-dave

Dave Murray [NCL]
I almost expected that I would need to handle the encoding manually.
Rahul Singla

You can use google to search for other answers

Custom Search

More Threads

• HTTPwebrequest problems
• Does WebProxy support other proxy settings besides IE proxy settings
• Server not responding to new clients .. connecting to only one client
• Java HTTPRequest to C#
• Can TCP segments be broken up and delivered seperately?
• SSL and Server Certificates
• How to use WebrequestMethods.FTP.MakeDirectory
• Asp.Net Routing
• problem downloading binary file
• Contradictory MSDN documentation?